Unsubscribe the event handler of textbox inside DatagridView And add again

100 Views Asked by At

I am dealing with a problem in which i got struck , which I asked at Events calling twice error on DataGridView . Now a respected member suggest me to remove the eventhandlers of the textbox inside of the datagridview before I add them again . But I cannot get the concept as i am just a beginner . Can someone please suggest me how to do it . The actual problem was that the OnEnter event of the customdatagridview was firing twice .The code was

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;                 
            }
        }
    }
1

There are 1 best solutions below

1
Bartosz On

As said above you should unsubscribe from the event before adding new one. You should think that events have defined some kind of list of methods, which are being called when the event occurs. So when you keep subscribing events, the list will grow. That may cause that your function will fire many times (not just twice).

To unsubscribe from event, you just change += (that stands for subscribing to an event) to -= (that stands for unsubscribing).

You could also check if your delegate (event method) is already added.

It is recommended to do it with checking, what was already described in another ticket: How to determine if an event is already subscribed

To help you understand the problem, i wrote a simpliest fix for multiple method calls:

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

private void Form2_Load(object sender, EventArgs e)
{
  customControl11.Focus();
  }
   private void customControl11_EditingControlShowing(object sender, 
  Data

GridViewEditingControlShowingEventArgs 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; 
        textBox.TextChanged += TextBox_TextChanged; 
        textBox.KeyDown += TextBox_KeyDown; 
        textBox.Enter += TextBox_Enter;                 

    }
  }
}

Now your events should execute your delegates only once.

PS. Its a very bad idea to do it in eventEditingControlShowing since it will execute everytime that edit control is about to be shown.