-4

After Triggering an event, I want to add some more item to the ComboBox in my WinForm.

The following is my sample code, which is not neat at all, also I violated DRY(don't repeat yourself) principle. I am a newbie seeking for a neat code. Please help me out.

Combobox1.Items.Add("aaa")
Combobox2.Items.Add("aaa")
....
Combobox50.Items.Add("aaa")

As you can see, I can do this if there are only 5 to 10 items needed to be added to the Combobox. What if there are 50 of them ? And I want to select certain combobox to add item to it.

I wana find a better way to do this, don't repeat the above routine for too much time. Please help me with a neater code. Thanks!!!

ckpeanut
  • 1
  • 3
  • 1
    Only if you knew how to google. – Mahesh Feb 09 '15 at 07:37
  • 1
    the answers for asp.net and winforms/ vb.net and c# are likely to be different, what are you using? – Sayse Feb 09 '15 at 07:38
  • Populate collection and bind collection to Combobox. Separate binding and data preparation so that if tomorrow data source changes it will not have much impact and may need small tweaking. Also, if you know DRY principle and you think on that how not to repeat then solution will come out easily for you. – Amit Feb 09 '15 at 07:39
  • Also, are you trying to add to every combobox in your form/page? or just a certain selection? – Sayse Feb 09 '15 at 07:40
  • I just wamt to add item to a certain collection. I used to use findcontrol method in Webform, but migrating to winform makes everything so different to me. A method in VB or C# is welcomed, I think i can at least find a VB to C# convert on my own. – ckpeanut Feb 09 '15 at 07:46

4 Answers4

0

Better to Use DataGridView.

The DataGridView actually has a DataGridViewComboBoxColumn

Reference Ref1 Ref2

Dgan
  • 10,077
  • 1
  • 29
  • 51
0

suppose your comboBoxes are in a container1(it can be a groupBox for example), you can do something like:

foreach(ComboBox cbx in container1)
{
   cbx.Items.Add("aaa");
}
chouaib
  • 2,763
  • 5
  • 20
  • 35
0

This is what you can try :

foreach(ComboBox cb in controlpanel1)
{
  foreach(items a in ListItems)
  {
     cb.Items.Add(a);
  }
}  

that's it. hope this will help you

dazzling kumar
  • 584
  • 1
  • 8
  • 17
0

If you have 50 comboboxes on a form or a webpage then first you need to start by looking at redesigning your UI from a user's point of view - its likely to involve too much information for one person to take in at any one time.

If its a selection of controls then you are best to add all your comboboxes to a panel and then iterate over that.

foreach(var cb in this.panelName.Controls.OfType<ComboBox>())
     cb.Items.Add("aaa");

If its all comboboxes you can use this.Controls however this wouldn't check for subcontrols (i.e within panels). To do that you can use the following from an answer given by Jon Skeet

foreach(var cb in this.GetAll<ComboBox>())
     cb.Items.Add("aaa");
Community
  • 1
  • 1
Sayse
  • 42,633
  • 14
  • 77
  • 146
  • 1
    Thanks, the key word "Generic " was not in my searching key words. Thanks for ur useful information. I'll take a look of the related topics. – ckpeanut Feb 09 '15 at 07:56
  • @ckpeanut - No worries, hope it helps. As a pre-warning, if you use any tab controls in your winforms then this won't work until after the tab pages have been visited (its a little "bug" in winforms) (I also think tabs are now predominantly dated and should be avoided if possible but hey-ho) – Sayse Feb 09 '15 at 07:58
  • I was think about any solution like Parameterizing the control name like this for (int i = 1; i < 51; i++){ "combox" + i} using this way to call add method in each combobox. But it seems like I got some more homework to do. – ckpeanut Feb 09 '15 at 08:05
  • @ckpeanut , You shouldn't need to, you should only really need to update similar values and they should all be near each other (same panel). One thing I did forget to mention that you should look into is [`DataBinding`](https://msdn.microsoft.com/en-us/library/ef2xyb33%28v=vs.90%29.aspx), then you just assign all your comboboxes to populate from the same list, and just add to the list once – Sayse Feb 09 '15 at 08:22
  • 1
    Ok, I'll do my homework and add that to my keyword. Thank you for ur guidance. That truly helps. : D – ckpeanut Feb 09 '15 at 08:25