Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Combo Boxes in C#

Options
  • 01-06-2007 3:02pm
    #1
    Registered Users Posts: 10,351 ✭✭✭✭


    Hey, i've a few combo boxes in my program, problem is that i can't seem to set what the default selected item is (as in what is selected when the program runs first)

    What I've done is this in the designer mode:
    ComboBox myComboBox = new ComboBox();
    myComboBox.Items.AddRange(new object[] {"Item #1", "Item #2", "Item #3"});
    myComboBox.setSelectedIndex = 0;
    

    What happens is that when the program is compiled & run, the "Item #1" is selected already. However, if I continue to edit the class the last line will bizarrely delete itself for some reason.

    Anybody hear of this problem? Or is there an alternative I'm unaware of?


Comments

  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    I'm not sure what you mean when you say the last line will delete itself. Is this the last line of your code? Or the last item in your combobox?

    Can you post the entire method that is adding the items to the combobox?


  • Registered Users Posts: 10,351 ✭✭✭✭dulpit


    Evil Phil wrote:
    I'm not sure what you mean when you say the last line will delete itself. Is this the last line of your code? Or the last item in your combobox?

    Can you post the entire method that is adding the items to the combobox?


    The last line of the my code (i.e. the comboBox.setSelectedIndex() method)

    Anyway, this is the automatically generated code to create 1 comboBox:

    this.myComboBox.FormattingEnabled = true;
    this.myComboBox.Items.AddRange(new object[] {
                "Item # 1",
                "Item # 2",
                "Item # 3"});
    this.myComboBox.Location = new System.Drawing.Point(464, 313);
    this.myComboBox.Name = "myComboBox";
    this.myComboBox.Size = new System.Drawing.Size(144, 24);
    this.myComboBox.TabIndex = 22;
    [B]this.myComboBox.SelectedIndex = 0;[/B]
    

    The line in bold is what i added myself... That's the line that dissapears:(


  • Registered Users Posts: 2,145 ✭✭✭dazberry


    The reason the line is disappearing is exactly that the code is being auto-generated as you mentioned. Move the line you've specified somewhere else, for instance in the Form Constructor after the InitializeComponents() call.

    D.


  • Registered Users Posts: 10,351 ✭✭✭✭dulpit


    dazberry wrote:
    The reason the line is disappearing is exactly that the code is being auto-generated as you mentioned. Move the line you've specified somewhere else, for instance in the Form Constructor after the InitializeComponents() call.

    D.

    That's the job, work's perfect now :)


Advertisement