C# - Adding values to combo box items.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hollywood115
    New Member
    • Feb 2008
    • 16

    C# - Adding values to combo box items.

    Hey guys. I have a quick question... its very simple but i cant find a way to do it..
    I need to add items to a combobox in a windows form... i need to have a name, and a value to go with it so they're associated with one another.
    whats the easiest way i can do this? i thought of creating a new class to handle it, but im sure c# has a way of doing that already....

    thanks for your help.
  • todashah
    New Member
    • Feb 2008
    • 26

    #2
    Hi ! I have assume that u want to assign roll no to name of student which are
    store in combo-box whenever user select name of student. Write following code
    to do this.

    First declare int[] j; in class defination of form.

    then write following coding in page load event of form.
    j = new int[comboBox1.Items .Count];
    for (int i = 0; i < j.Length; i++)
    {
    j[i] = 0;
    }

    Write down following coding in selected index changed event of combo-box.

    int id = comboBox1.Selec tedIndex;
    if (id == 0 && j[id]==0)
    {
    comboBox1.Items .Insert(id, comboBox1.Selec tedItem + "-001");
    comboBox1.Items .RemoveAt(++id) ;
    j[--id] = 1;
    }
    else if (id == 1 && j[id] == 0)
    {
    comboBox1.Items .Insert(id, comboBox1.Selec tedItem + "-002");
    comboBox1.Items .RemoveAt(++id) ;
    j[--id] = 1;
    }

    Note: I have written else if block for first two items of combo-box. Now when u select these items from combo-box then number is associated with name.
    Make appropriate changes as per your requirement.

    Bye...Enjoy Coding

    Comment

    Working...