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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Combo Boxes in C#

  • 01-06-2007 2:02pm
    #1
    Registered Users, Registered Users 2 Posts: 12,379 ✭✭✭✭


    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, Registered Users 2 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, Registered Users 2 Posts: 12,379 ✭✭✭✭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, Registered Users 2 Posts: 2,152 ✭✭✭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, Registered Users 2 Posts: 12,379 ✭✭✭✭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