简体   繁体   中英

why the eventhandler for a comboBox1_SelectedIndexChanged never gets executed in C#

I don't know how else to document this question for SO. I am programming in WinForm and C#. Added a comboBox to the window and wrote the event handler for it. All the other controls on this window generate their click events but not the combox? There should be nothing to it but the event handler does not get executed no matter how many time I click on the drop down arrow or the combobox itself.

private void InitializeComponent()
    {

        // 
        // comboBox1
        // 
            this.comboBox1.AllowDrop = true;
            this.comboBox1.FormattingEnabled = true;
            resources.ApplyResources(this.comboBox1, "comboBox1");
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.SelectedIndexChanged += new System.EventHan(this.comboBox1_SelectedIndexChanged);
        // 
    }

thanks in advance for your help.

By you description, it sounds like you're just clicking on the drop down, expecting it to trigger the event. Did you actually select a different option in the menu, or just click the arrow? SelectedIndexChanged only fires when you change the selected option in the menu. Also, in your code, you have this:

this.comboBox1.SelectedIndexChanged += new System.EventHan(this.comboBox1_SelectedIndexChanged);

Is that what it actually is? That shouldn't compile. Should be:

this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);

Edit:

Okay, I think I know what's going on. Where you double clicking the controls in the designer to wire up the events? If so, that wires up the default event for that control. So for a button, the default is the Click event. For a combo box, the default is the SelectedIndexChanged event, not the 'Click' event. To wire up the Click event on your ComboBox , open the form in the designer. Then select the ComboBox in the designer, and press CTRL + W, P. This opens the Property window for the control. Click the lightning bolt icon at the top of that window, and it will show all the Events you can use on the control. Find `Click' in the list and double click it, and it will wire up the event and bring you to the new handler for that event.

this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);

要不就

this.comboBox1.SelectedIndexChanged += this.comboBox1_SelectedIndexChanged;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM