Comobo Box选择索引问题 [英] Comobo Box selected index problem

查看:82
本文介绍了Comobo Box选择索引问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我设置组合框选择的索引时,在Windows应用程序中自动触发该组合框选择的索引更改事件,我如何避免这种情况,

Hi,

while i set Combo Box selected index, automatically that combo box selected index change event fired in windows application, how i can avoid this,

推荐答案

最好的方法是退订事件中的组合框,完成您的工作并重新订阅,就像这样

The best way is to unsubscribe the combo box from the event , do your stuff and re subscribe, something like this

comboBox1.SelectedIndexChanged -= new EventHandler(comboBox1_SelectedIndexChanged);
           //Do some stuff
           comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);



或者,您也可以订阅SelectionChangedCommitted事件,该事件仅在用户更改组合框选择时触发.


希望这对您有帮助



Either that or you can subscribe to the SelectionChangedCommitted event, which only fires when a user changes the combobox selection.


Hope this helps


我假设您正在设置组合框的选定索引,以使其与某个默认值匹配,例如编辑某种记录.

最简单的方法是在更改之前删除事件处理程序,然后在更改之后添加.

假设组合框名为comboBox1,事件处理程序名为comboBox1_SelectedIndexChanged,则此操作即可完成.

I am assuming that you are setting the selected index of the combo box so that it matches some default value, e.g. editing some sort of record.

The easiest way is to remove the event handler before the change and add if back afterwards.

Assuming that the combobox is called comboBox1 and that the event handler is called comboBox1_SelectedIndexChanged then this should do the job.

using System.Collections.Generic;
using System.Windows.Forms;

namespace sample
{
    public partial class Form1 : Form
    {

        // possible values
        private List<string> _items = new List<string>() { "One", "Two" };

        public Form1()
        {
            InitializeComponent();

            DataBind();
        }

        private void DataBind()
        {
            // disable event handler
            comboBox1.SelectedIndexChanged -= this.comboBox1_SelectedIndexChanged;

            // bind possible values
            comboBox1.DataSource = _items;

            //your logic to get this number
            int selectedIndex = 1;

            //set default value - 
            comboBox1.SelectedIndex = selectedIndex;

            // enable event handler
            comboBox1.SelectedIndexChanged += this.comboBox1_SelectedIndexChanged;
        }

        private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            // normal logic to handle selection chnage
            MessageBox.Show("fired");
        }
    }
}</string></string>


需要执行此操作时,我将创建一个名为m_initialized的变量,并将其设置为false的初始值.在Load事件处理程序的末尾,我将其设置为true.然后在控件的SelectionChanged事件处理程序中,检查m_initialized是否为true,如果是,则处理该事件.否则,我退出处理程序.
When I need to do that, I create a variable called m_initialized, and set it to an initial value of false. At the end of the Load event handler, I set it to true. Then in the SelectionChanged event handler for the control, I check to see if m_initialized is true, and if so, I handle the event. Otherwise, I exit the handler.


这篇关于Comobo Box选择索引问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆