繁体   English   中英

为什么comboBox1_SelectedIndexChanged的eventhandler永远不会在C#中执行

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

我不知道如何为SO记录这个问题。 我在WinForm和C#编程。 在窗口中添加了一个comboBox并为其编写了事件处理程序。 此窗口中的所有其他控件都会生成其点击事件,但不会生成combox? 应该没有任何东西,但无论多少次点击下拉箭头或组合框本身,事件处理程序都不会被执行。

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);
        // 
    }

在此先感谢您的帮助。

通过你的描述,听起来你只是点击下拉菜单,期望它触发事件。 您是否真的在菜单中选择了其他选项,或者只需单击箭头? SelectedIndexChanged仅在您更改菜单中的选定选项时触发。 此外,在您的代码中,您有:

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

它实际上是什么? 那不应该编译。 应该:

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

编辑:

好的,我想我知道发生了什么。 双击设计器中的控件以连接事件的位置? 如果是这样,那将连接该控件的默认事件。 因此,对于按钮,默认为Click事件。 对于组合框,默认值为SelectedIndexChanged事件, 而不是 “Click”事件。 要连接ComboBox上的Click事件,请在设计器中打开表单。 然后在设计器中选择ComboBox,然后按CTRL + W,P。这将打开控件的“属性”窗口。 单击该窗口顶部的闪电图标,它将显示您可以在控件上使用的所有事件。 在列表中找到“Click”并双击它,它将连接事件并将您带到该事件的新处理程序。

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

要不就

this.comboBox1.SelectedIndexChanged += this.comboBox1_SelectedIndexChanged;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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