C# 在c中调用时,手动初始化的combobox不会显示
我刚刚创建了一个包含许多组合框的表单,但它们只使用相同的数据。第一次,我每次点击radiobutton都会加载数据,但这需要一些时间,而且非常烦人。因此,我希望在应用程序启动时只加载一次组合框,而不是每次单击单选按钮时都加载组合框 下面的代码是创建全局组合框,它可以在表单中的任何位置调用C# 在c中调用时,手动初始化的combobox不会显示,c#,combobox,C#,Combobox,我刚刚创建了一个包含许多组合框的表单,但它们只使用相同的数据。第一次,我每次点击radiobutton都会加载数据,但这需要一些时间,而且非常烦人。因此,我希望在应用程序启动时只加载一次组合框,而不是每次单击单选按钮时都加载组合框 下面的代码是创建全局组合框,它可以在表单中的任何位置调用 public partial class Form1 : Form { //code to get the requester below public List&l
public partial class Form1 : Form
{
//code to get the requester below
public List<XMLSoccerCOM.Team> all_team = requester.GetAllTeams();
public List<XMLSoccerCOM.League> all_league = requester.GetAllLeagues();
public ComboBox combo_team = new ComboBox(); //global combo_team
public ComboBox combo_league = new ComboBox(); //global combo_league
//the rest of the code
}
看起来已经完成了,我只需要随时分配它。在radiobutton checkchange事件中,我将其设置为:
private void history_league_CheckedChanged(object sender, EventArgs e)
{
his_combobox = combo_league; // Assign the global league combobox to a local combobox.
his_odds_label.Text = "Choose the league:"; //This label is next to the his_combobox
his_odds_label.Visible = true;
his_combobox.Visible = true; //Show them if it's hidden by another radiobutton
}
private void history_team_CheckedChanged(object sender, EventArgs e)
{
his_combobox = combo_team; // Assign the global team combobox to a local combobox.
his_odds_label.Text = "Choose the team:"; //This label is next to the his_combobox
his_odds_label.Visible = true;
his_combobox.Visible = true; //Show them if it's hidden by another radiobutton
}
但当我按住Ctrl+F5键时,它不会显示,只有标签显示,his_组合框不会显示,就像它永远不会出现一样
请随时询问更多信息。谢谢您的时间。分配数据源非常重要 his_combobox.DataSource=combo_league.DataSource 但我会用更简单的方法来做类似的事情
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
var checkbox = (CheckBox) sender;
comboBox1.DataSource = checkbox.Checked ? all_team : all_league;
comboBox1.Visible = true;
}
通过这种方法,您不必创建额外的组合框,但这取决于您的需要
编辑:
在我的示例中,我将all_team和all_league作为字符串类型,但您可以根据自己的属性进行调整
public List<String> all_team = new List<string>
{
"Team A",
"Team B"
};
public List<String> all_league = new List<string>
{
"League a",
"League b"
};
public ComboBox combo_team = new ComboBox(); //global combo_team
public ComboBox combo_league = new ComboBox(); //global combo_league
public Form1()
{
InitializeComponent();
comboBox1.Visible = false;
}
/// <summary>
/// Method 1
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
var checkbox = (CheckBox) sender;
comboBox1.DataSource = checkbox.Checked ? all_team : all_league;
comboBox1.Visible = true;
}
/// <summary>
/// Method2
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
combo_league.DataSource = all_league;
combo_team.DataSource = all_team;
var checkbox = (CheckBox)sender;
comboBox1.DataSource = checkbox.Checked?combo_league.DataSource:combo_team.DataSource;
comboBox1.Visible = true;
}
最后我从Kalyan那里得到了一些建议。我创建了一个全局变量:
public List<String> teams = new List<string>();
最后,让团队成为数据源:
combobox1.DataSource = teams;
仅此而已:谢谢。看看这里,也许会有帮助:|我不知道怎么了,但这些对我不起作用。第一种方法给我一个空的组合框,第二种较短的方法给我一个这种类型的组合框,比如XMLSoccerCOM.Team或XMLSoccerCOM.league如何只添加一列作为数据源?有什么建议吗?@tranhoi:您是否尝试添加显示成员和值成员,这些值应该是您的属性。检查这个网址谢谢,我从你的理想答复中找到了我的答案。
foreach (XMLSoccerCOM.Team te in all_team)
{
teams.Add(te.Name.ToString());
}
combobox1.DataSource = teams;