0

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.

ankit goel
  • 43
  • 6
  • Check the designer.cs file. You can open the file which is text. It sound like the event is both in your cs file and designer.cs file. You can simply delete from the deisgner.cs. A lot of events will automatically trigger when the form constructor is run and you simply ignore the first event. With controls the row count is -1 when being constructed and zero when columns are added. So ignoring when rowcount =-1 normally ignores the event during constructing. – jdweng May 26 '23 at 11:48
  • @jdweng , no i double checked it. The enter event for customcontrol1 is not there in the form2.cs – ankit goel May 26 '23 at 11:51
  • The issue may just be the event is getting triggered during the constructor. The load is called after the constructor. So you need to check if the extra event occurs before or after the load. – jdweng May 26 '23 at 12:22
  • @jdweng, the customControl11.Focus(); was infact a part of some random textbox_keydown event . I showed here in form_load event for the sake of simplicity . but i still can't understand your point – ankit goel May 26 '23 at 12:42
  • You cannot access the control until the constructor is completed. So any event during the constructor has to be ignored. – jdweng May 26 '23 at 12:48
  • You could add test : if(customControl11.Rows.Count > 0) – jdweng May 26 '23 at 13:08
  • `BeginEdit()` is *re-entrant* (in relation to what you're doing), you move the focus from the DGV to another Control (the `DataGridViewTextBoxEditingControl`), then trigger the event again for host container (your DGV) -- BTW, this: `customControl11.Focus();` doesn't generate an Enter event, `ActiveControl = customControl11;` does -- BTW2, you're adding event handlers to the `DataGridViewTextBoxEditingControl` each time the Control is shown. Remember that it's aways the same Control, it's recycled – Jimi May 26 '23 at 13:49
  • @Jimi , so what's the solution – ankit goel May 27 '23 at 07:14
  • To what problem? If you don't want the code in the OnEnter() override to run twice, for whatever reason, add `if (Rows.Count < 1 || FindForm().ActiveControl.GetType() == typeof(DataGridViewTextBoxEditingControl)) return;` right after `base.OnEnter(e);` -- Your actual problem now is that you're subscribing to the events of that TextBox every time it's shown. So remove the handlers before you add them again (poor man's solution, even though a working one. I'd prefer to create a custom Column or assign the handle of the TextBox to a `NativeWindow` and override its WndProc instead) – Jimi May 27 '23 at 11:00
  • @Jimi, i would be very thankful many times if you could share an example of "I'd prefer to create a custom Column or assign the handle of the TextBox to a NativeWindow and override its WndProc instead) " . i would really learn a lot . Thanks in advance – ankit goel May 27 '23 at 11:31
  • @Jimi , sir I would be very thankful if you please share some time for the above code that i asked you to . – ankit goel May 28 '23 at 07:22
  • Is there no one in this community to help me in this matter – ankit goel May 30 '23 at 12:17

0 Answers0