简体   繁体   English

SelectedIndexChanged不触发ComboBox

[英]SelectedIndexChanged not firing for the ComboBox

I made a combobox with the name FormatComboBox. 我做了一个名为FormatComboBox的组合框。 I populated it with a list of items. 我用项目列表填充它。 I want to trigger an event whenever the user selects an item from the list. 每当用户从列表中选择一个项目时,我都想触发一个事件。 The following is my code. 以下是我的代码。

 private void FormatComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
              /// some code
        }

I put a break point inside the code to see whether it is working and found that it isn't. 我在代码中放置了一个断点,以查看其是否正常工作,并发现无效。 After I tried using 我尝试使用后

private void FormatComboBox_SelectedValueChanged(object sender, EventArgs e)

 private void FormatComboBox_SelectedItemChanged(object sender, EventArgs e)

I am working on c# for the first time and I was following this tutorial 我是第一次使用C#,并且正在关注本教程

http://www.kinectingforwindows.com/2013/04/09/tutorial-kinect-television/ http://www.kinectingforwindows.com/2013/04/09/tutorial-kinect-television/

The one they used was the following 他们使用的是以下

private void OnSelectedFormatChanged(object sender, SelectionChangedEventArgs e)

But even that is not working 但是即使那样也不起作用

Make sure that the event is attached to the FormatComboBox. 确保该事件已附加到FormatComboBox。

By design: 通过设计:

在此处输入图片说明

By Code: 通过代码:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            comboBox1.SelectedIndexChanged +=comboBox1_SelectedIndexChanged;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }

You need to make sure that you are actually adding the event handler properly in your code or in the text box's properties. 您需要确保实际上在代码或文本框的属性中正确添加了事件处理程序。 It should look something like this: 它看起来应该像这样:

    public partial class Form1 : Form
        {
            FormatComboBox fbox = new FormatComboBox();

            // Associate event handler to the combo box.
            fbox.SelectedValueChanged+=FormatComboBox_SelectedValueChanged;

        prviate void FormatComboBox_SelectedIndexChanged(object sender, EventArgs e)
          {
              // do stuff
          }
        }

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

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