-1

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;                 
            }
        }
    }
Roe
  • 633
  • 2
  • 14
ankit goel
  • 43
  • 6
  • Have you tried finding out through setting a breakpoint when they get called? – Roe May 30 '23 at 06:53
  • @Roe , obviously YES . – ankit goel May 30 '23 at 07:07
  • If that's all what you have in the `OnEnter` method override, then let it to be called twice. What bothers the rest of your code? You'll have the first cell selected and in the edit mode. And yes, you should `-=` before `+=`. In other words, what happens when the method is called twice? What is the problem? – dr.null May 30 '23 at 16:21
  • @dr.null , it is just sample code . it would have more code over time and calling twice would create problems later on . Please suggest which events should i be looking at . – ankit goel May 31 '23 at 05:52
  • Add a condition to enter an execution block then. ex. After `base.OnEnter(e);` add `if (RowCount > 0 && !this[0, 0].IsInEditMode) { CurrentCell = this[0, 0]; BeginEdit(true); // the rest... }`. Also, you can override the `OnEditingControlShowing` method in your custom grid instead of handling the invoked event in your implementation. Don't forget to `-=` before `+=`. – dr.null May 31 '23 at 10:36

1 Answers1

0

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.

Bartosz Olchowik
  • 1,129
  • 8
  • 22
  • I already tried the same thing but it didn't helped me . Also In which event did you suggest me to paste the code . – ankit goel May 30 '23 at 07:04