Events calling twice error on DataGridView

137 Views Asked by At

I have developed a customDataGridView for my project. It is declared as follows.

public partial class CustomControl1 : DataGridView
{
    public CustomControl1()
    {
        this.KeyDown += new KeyEventHandler(CustomDataGridView_KeyDown);
       
        InitializeComponent();
    }
  protected override void OnEnter(EventArgs e)
    {
        base.OnEnter(e);
        this.CurrentCell = this.Rows[0].Cells[0];
        this.BeginEdit(true);
    }

now the form where I am using.

 public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();         
    }

    private void Form2_Load(object sender, EventArgs e)
    {
      customControl11.Focus();
 }
    private void customControl11_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {            
        if (customControl11.CurrentCell.ColumnIndex == 0) 
        {
           TextBox textBox = e.Control as TextBox;
            if (textBox != null)
            {
                textBox.TextChanged += TextBox_TextChanged; 
                textBox.KeyDown += TextBox_KeyDown; 
                textBox.Enter += TextBox_Enter;                 

            }
        }
    }

now the problem is that when I run the debugger, I found out that the code in OnEnter is running twice. what I mean is I code starts from OnEnter method, then to EditingControlShowing and then the OnEnter method runs again. I can't understand the logic behind it. If any respected member catches my error, please suggest the way to stop it.

0

There are 0 best solutions below