0

I just created a form with many combobox but they only use the same data. At the first time, I load the data for every click on the radiobutton, but it takes some time and it's so annoying. So instead of loading the combobox everytime I click a radiobutton, I want it to load just one time when the app start.

The code below is to create the global combobox which can be called anywhere in the form.

    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
    }

And inside of this form, there's a init method:

    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());
        }
    }

So looks like it's done, I just need to assign it whenever I want. In the radiobutton checkchange event I make it:

    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

    }

But it's not showing up when I Ctrl+F5, only the label shows up, the his_combobox doesn't, just like it's never be there.

Feel free to ask for more information. Thank you for your time.

Tran Hoai Nam
  • 1,273
  • 2
  • 18
  • 35
  • Have a look here, maybe it helps: http://stackoverflow.com/questions/2152705/how-to-dynamically-add-combobox-in-windows-formsc-and-bound-it-to-a-column-of | http://stackoverflow.com/questions/4721436/binding-xml-to-combobox – Daniel Dušek Nov 09 '14 at 15:16

2 Answers2

0

assigning datasource is critical.

his_combobox.DataSource = combo_league.DataSource;

but i would go further simpler approach something like this.

private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        var checkbox = (CheckBox) sender;
        comboBox1.DataSource = checkbox.Checked ? all_team : all_league;
        comboBox1.Visible = true;
    }

by this approch you dont have to create additional combo boxes, but it is up to your need.

EDIT: In my example i had all_team and all_league as string type but you can adjust that according to your properties.

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
  • 307
  • 2
  • 14
  • I don't know what is wrong but these didn't work for me. The first one gives me a empty combobox, the second (shorter way) gives me a combobox of the type, like `XMLSoccerCOM.Team` or `XMLSoccerCOM.League` – Tran Hoai Nam Nov 12 '14 at 15:32
  • How do I add just one column as datasource? Any suggestion? – Tran Hoai Nam Nov 12 '14 at 15:47
  • @tranHoai: have you tried adding display member and value member , the values should be your properties. Check this url "http://stackoverflow.com/questions/600869/how-to-bind-a-list-to-a-combobox-winforms" – Kalyan Nov 14 '14 at 14:34
  • Thank you, I found my answer from your ideal reply. – Tran Hoai Nam Nov 14 '14 at 17:29
0

Finally I figured it out with a few suggestions from Kalyan. I created a global variable:

 public List<String> teams = new List<string>();

then fill in this list with the items needed (my all_team has many columns, that's why it won't show up with his_combobox.DataSource = combo_team.DataSource; as he told me)

    foreach (XMLSoccerCOM.Team te in all_team)
        {
            teams.Add(te.Name.ToString());
        }

Then finally make the teams the datasource:

   combobox1.DataSource = teams;

That's all :) Thank you.

Tran Hoai Nam
  • 1,273
  • 2
  • 18
  • 35