我刚创建了一个包含许多组合框的表单,但它们只使用相同的数据。第一次,我为radiobutton上的每次点击加载数据,但这需要一些时间,而且非常烦人。因此,每次点击一个单选按钮时,我都不想加载组合框,而是希望它在应用程序启动时只加载一次。
以下代码是创建可在表单中的任何位置调用的全局组合框。
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
}
在这个表单中,有一个init方法:
public Form1()
{
InitializeComponent();
his_combobox.Visible = false; //I hide everything so that I can show them when click on the radiobutton
foreach (XMLSoccerCOM.Team te in all_team) //This one for the first combobox
{
combo_team.Items.Add(te.Name.ToString());
}
foreach (XMLSoccerCOM.League le in all_league) //And the 2nd as well
{
combo_league.Items.Add(le.Name.ToString());
}
}
所以看起来已经完成了,我只需要随时分配它。在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_combobox没有显示,就像它永远不会在那里一样。
随时询问更多信息。谢谢你的时间。
答案 0 :(得分:0)
分配数据源非常重要。
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;
}
通过这个approch你不必创建额外的组合框,但这取决于你的需要。
修改强> 在我的例子中,我将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;
}
答案 1 :(得分:0)
最后,我从卡利安的一些建议中找到了答案。我创建了一个全局变量:
public List<String> teams = new List<string>();
然后用所需的项目填写此列表(我的all_team
有很多列,这就是为什么它不会出现his_combobox.DataSource = combo_team.DataSource;
,因为他告诉我)
foreach (XMLSoccerCOM.Team te in all_team)
{
teams.Add(te.Name.ToString());
}
然后最终使teams
数据源:
combobox1.DataSource = teams;
全部:)谢谢。