for loop and combobox add item

I'm trying to create the object name using a for loop.

Page 1 of 1

3 Replies - 11875 Views - Last Post: 24 July 2010 - 11:02 AM Rate Topic: -----

#1 Guest_ChrisGZF*


Reputation:

for loop and combobox add item

Posted 24 July 2010 - 09:36 AM

I will briefly go through what help I need.

In my C# WinForm, I have 7 comboboxes named c_StudentMarks_1, c_StudentMarks_2, and so on until c_StudentMarks_7.

I'm trying to do something like this:
            for (int a = 1; a < 7; a++)
            {
                for (int i = 0; i < 101; i++)
                {
                    "this.c_StudentMark_" + a + ".Items.Add(i)";
                }
            }



This obviously doesn't compile. How do I make it work? Thanks in advance!


Chris

Is This A Good Question/Topic? 0

Replies To: for loop and combobox add item

#2 poncho4all   User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,422
  • Joined: 15-July 09

Re: for loop and combobox add item

Posted 24 July 2010 - 10:13 AM

Are you trying to write an instruction to the code?
Was This Post Helpful? 0
  • +
  • -

#3 Guest_ChrisGZF*


Reputation:

Re: for loop and combobox add item

Posted 24 July 2010 - 10:16 AM

Yes
Was This Post Helpful? 0

#4 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: for loop and combobox add item

Posted 24 July 2010 - 11:02 AM

You can do something like this:
            foreach (Control c in this.Controls) {
                if (c is ComboBox) {
                    ComboBox cb = (ComboBox)c;
                    if (cb.Name.StartsWith("combo")) {
                        for (int i = 0; i < 100; i++) {
                            cb.Items.Add(i);
                        }
                    }
                }
            }


The StartsWith can be used to select ones that begin with a specific name pattern.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1