简体   繁体   English

当我设置其他组合框的数据集时,`SelectedIndexChanged`将触发事件

[英]`SelectedIndexChanged` fires event when I set dataset of a different combobox

This is a problem from Windows Mobile 6.5 (.net compact edition)in Windows Forms. 这是Windows Forms中Windows Mobile 6.5(.net精简版)的问题。 I have created a form with two usercontrols, each containing one combobox. 我创建了一个包含两个用户控件的窗体,每个控件包含一个组合框。
When I load the second usercontrol, I fill the combobox and fire _SelectedIndexChanged of the combobox of the first usercontrol. 当我加载第二个用户控件时,我填充了组合框并触发了第一个用户控件的组合框的_SelectedIndexChanged This error only happen when in the first usercontrol there's a control datagrid. 仅当第一个用户控件中有控件数据网格时,才会发生此错误。 if u delete datagrid all events are corrects. 如果您删除datagrid,则所有事件均正确。

 public partial class Form1 : Form
{
    static void Main(string[] args)
    {
        Application.Run(new Form1());
    }

    private UserControl1 userControl1 = null;
    private UserControl2 userControl2 = null;

    public Form1()
    {
        InitializeComponent();
        InitializeUserControl();
    }

    private void InitializeUserControl()
    {
        InitiliazeTab1();
        InitiliazeTab2();
    }

    private void InitiliazeTab1()
    {
        userControl1 = new UserControl1();
        userControl1.Dock = DockStyle.Fill;

        TabPage oTabpage1 = new TabPage();
        oTabpage1.Name = "tabSeleccionTipo1";
        oTabpage1.Text = "Tipo1 ";
        oTabpage1.Controls.Add(userControl1);
        tabControl1.TabPages.Add(oTabpage1);

        userControl1.LoadComponent();
    }

    private void InitiliazeTab2()
    {
        userControl2 = new UserControl2();
        userControl2.Dock = DockStyle.Fill;

        TabPage oTabpage2 = new TabPage();
        oTabpage2.Name = "tabSeleccionTipo2";
        oTabpage2.Text = "Tipo2 ";
        oTabpage2.Controls.Add(userControl2);
        tabControl1.TabPages.Add(oTabpage2);

        userControl2.LoadComponent();
    }
}

partial class Form1
{
    /// <summary>
    /// Variable del diseñador requerida.
    /// </summary>
    private System.ComponentModel.IContainer components = null;
    private System.Windows.Forms.MainMenu mainMenu1;

    /// <summary>
    /// Limpiar los recursos que se estén usando.
    /// </summary>
    /// <param name="disposing">true si los recursos administrados se deben eliminar; en caso contrario, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Código generado por el Diseñador de Windows Forms

    /// <summary>
    /// Método necesario para admitir el Diseñador. No se puede modificar
    /// el contenido del método con el editor de código.
    /// </summary>
    private void InitializeComponent()
    {
        this.mainMenu1 = new System.Windows.Forms.MainMenu();
        this.tabControl1 = new System.Windows.Forms.TabControl();
        this.SuspendLayout();
        // 
        // tabControl1
        // 
        this.tabControl1.Location = new System.Drawing.Point(0, 0);
        this.tabControl1.Name = "tabControl1";
        this.tabControl1.SelectedIndex = 0;
        this.tabControl1.Size = new System.Drawing.Size(240, 265);
        this.tabControl1.TabIndex = 0;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
        this.AutoScroll = true;
        this.ClientSize = new System.Drawing.Size(240, 268);
        this.Controls.Add(this.tabControl1);
        this.Menu = this.mainMenu1;
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.TabControl tabControl1;
}

Usercontrols are UserControl1 and UserControl2 . UserControl1 UserControl2UserControl1UserControl2

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    public void LoadComponent()
    {
        this.comboBox1.ValueMember = "Clave";
        this.comboBox1.DisplayMember = "Description";
        comboBox1.DataSource = getList();         
    }

    private List<Item> getList()
    {
        List<Item> list = new List<Item>();

        list.Add(new Item("0", "zero"));
        list.Add(new Item("1", "uno"));
        list.Add(new Item("2", "dos"));
        list.Add(new Item("3", "tres"));
        list.Add(new Item("4", "cuatro"));

        return new List<Item>(list);
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        MessageBox.Show("Listo 1 ^^ SelectedIndexChanged ");    
    }
}

partial class UserControl1
{
    /// <summary> 
    /// Variable del diseñador requerida.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary> 
    /// Limpiar los recursos que se estén usando.
    /// </summary>
    /// <param name="disposing">true si los recursos administrados se deben eliminar; en caso contrario, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Código generado por el Diseñador de componentes

    /// <summary> 
    /// Método necesario para admitir el Diseñador. No se puede modificar 
    /// el contenido del método con el editor de código.
    /// </summary>
    private void InitializeComponent()
    {
        this.comboBox1 = new System.Windows.Forms.ComboBox();
        this.dataGrid1 = new System.Windows.Forms.DataGrid();
        this.SuspendLayout();
        // 
        // comboBox1
        // 
        this.comboBox1.Location = new System.Drawing.Point(24, 24);
        this.comboBox1.Name = "comboBox1";
        this.comboBox1.Size = new System.Drawing.Size(163, 22);
        this.comboBox1.TabIndex = 6;
        this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
        // 
        // dataGrid1
        // 
        this.dataGrid1.BackgroundColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))));
        this.dataGrid1.Location = new System.Drawing.Point(24, 89);
        this.dataGrid1.Name = "dataGrid1";
        this.dataGrid1.Size = new System.Drawing.Size(163, 134);
        this.dataGrid1.TabIndex = 7;
        // 
        // UserControl1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
        this.Controls.Add(this.dataGrid1);
        this.Controls.Add(this.comboBox1);
        this.Name = "UserControl1";
        this.Size = new System.Drawing.Size(216, 266);
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.ComboBox comboBox1;
    private System.Windows.Forms.DataGrid dataGrid1;

}

public partial class UserControl2 : UserControl
{
    public UserControl2()
    {
        InitializeComponent();
    }

    public void LoadComponent()
    {
        this.comboBox2.ValueMember = "Clave";
        this.comboBox2.DisplayMember = "Description";
        this.comboBox2.DataSource = getList();
    }

    public List<Item> getList()
    {
        List<Item> list = new List<Item>();
        list.Add(new Item("0", "zero"));
        list.Add(new Item("1", "uno"));
        list.Add(new Item("2", "dos"));
        list.Add(new Item("3", "tres"));
        return new List<Item>(list);
    }

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        MessageBox.Show("Listo 2 ^^ SelectedIndexChanged ");      
    }
}

partial class UserControl2
{
    /// <summary> 
    /// Variable del diseñador requerida.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary> 
    /// Limpiar los recursos que se estén usando.
    /// </summary>
    /// <param name="disposing">true si los recursos administrados se deben eliminar; en caso contrario, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Código generado por el Diseñador de componentes

    /// <summary> 
    /// Método necesario para admitir el Diseñador. No se puede modificar 
    /// el contenido del método con el editor de código.
    /// </summary>
    private void InitializeComponent()
    {
        this.comboBox2 = new System.Windows.Forms.ComboBox();
        this.SuspendLayout();
        // 
        // comboBox2
        // 
        this.comboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;
        this.comboBox2.Location = new System.Drawing.Point(21, 27);
        this.comboBox2.Name = "comboBox2";
        this.comboBox2.Size = new System.Drawing.Size(163, 22);
        this.comboBox2.TabIndex = 0;
        this.comboBox2.SelectedIndexChanged += new System.EventHandler(this.comboBox2_SelectedIndexChanged);
        // 
        // UserControl2
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
        this.Controls.Add(this.comboBox2);
        this.Name = "UserControl2";
        this.Size = new System.Drawing.Size(216, 266);
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.ComboBox comboBox2;
}

This is a simplified version of the real code. 这是真实代码的简化版本。 Why does this event fire when I load datasource of combobox of the second usercontrol? 为什么加载第二个用户控件的组合框的数据源时会触发此事件?

call process pool image of pool process of events. 调用进程池事件的映像。

Try subscribing to the SelectionChangeCommitted event instead 尝试订阅SelectionChangeCommitted事件

This way it doesn't fire when the change is made programmatically, which is the case of datasource loading 这样,当以编程方式进行更改时就不会触发,这是数据源加载的情况

First some minor, cosmetic changes: 首先,进行一些小的外观更改:

write

private List<Item> getList()
{
    ...
    return list;
}

instead of 代替

public List<Item> getList()
{
    ...
    return new List<Item>(list);
}

Then move the event handler assignment: 然后移动事件处理程序分配:

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

from designer code to LoadComponent() 从设计人员代码到LoadComponent()

    public void LoadComponent()
    {
        this.comboBox1.ValueMember = "Clave";
        this.comboBox1.DisplayMember = "Description";
        comboBox1.DataSource = getList();
        this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
    }

The issue can be worked around by removing the DataGrid from the first loaded UserControl. 通过从第一个加载的UserControl中删除DataGrid,可以解决此问题。 For example, the issue will not show if the DataGrid is on the second loaded UserControl: 例如,如果DataGrid在第二个加载的UserControl上,则不会显示该问题:

    private void InitializeUserControl()
    {
        InitiliazeTab2();
        InitiliazeTab1();
    }

There is no way to find the root cause, why the ComboBox SelectedIndexChanged event fires when the DataGrid is loaded within the first UserControl. 无法找到根本原因,为什么在第一个UserControl中加载DataGrid时会触发ComboBox SelectedIndexChanged事件。 There is no logical, documented description for that and I know no way to debug the event bubbling of Compact Framework. 没有针对此的逻辑记录说明,并且我不知道调试Compact Framework事件冒泡的方法。

For easy test of others, here is a VS2005 project to play with: https://github.com/hjgode/StackOverflow/tree/master/SmartDeviceProject1 为了便于他人测试,以下是一个可与之配合使用的VS2005项目: https : //github.com/hjgode/StackOverflow/tree/master/SmartDeviceProject1

I have resolved incorporated a datagrid in each usercontrol. 我解决了在每个用户控件中合并一个数据网格的问题。 This error isn't logical, it should be about framework librery error. 此错误不合逻辑,应该与框架库错误有关。

thanks. 谢谢。

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

相关问题 按下字母会激发组合框的selectedindexchanged和selectedvaluechanged事件? - pressing a letter fires combobox's selectedindexchanged and selectedvaluechanged event? ComboBox SelectedIndexChanged 事件未触发 - ComboBox SelectedIndexChanged event not firing 在 DataGridViewComboBoxColumn SelectedIndexChanged 期间触发的事件 - Event that fires during DataGridViewComboBoxColumn SelectedIndexChanged 如果将ComboBox SelectedIndexChanged悬停在下拉菜单上,则会触发两次 - ComboBox SelectedIndexChanged fires twice if hovering the dropdown 为Winform中的组合框选择OwnerDrawFixed模式时,不会触发SelectedIndexChanged事件 - SelectedIndexChanged event is not firing when OwnerDrawFixed mode selected for Combobox in Winform C#组合框SelectedIndexChanged事件 - C# Combobox SelectedIndexChanged event 在 selectedIndexChanged 事件上访问 comboBox 的值 - Access to value of comboBox on selectedIndexChanged event DataGridViw ComboBox列事件SelectedindexChanged - DataGridViw ComboBox Column Event SelectedindexChanged 在DataTable中设置值会触发SelectedIndexChanged事件 - Setting a value in DataTable fires SelectedIndexChanged event ComboBox SelectedIndexChanged并不总是被触发-如何检查它何时出现? - ComboBox SelectedIndexChanged not always fired - how do I check when it is?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM