base class event of customcontrol

ankit goel 746 Reputation points
2023-05-13T13:56:08.6566667+00:00

Hi , I have created a customcontrol textbox as below

  public partial class customtextbox : TextBox
    {
       // private Caret caret;
        public customtextbox()
        {
InitializeComponent();
        }

 protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                SendKeys.Send("{TAB}");

                //disable beep 
                e.Handled = e.SuppressKeyPress = true;
            }
            if ((e.KeyCode == Keys.Back || e.KeyCode == Keys.Up) && this.Text.Length == 0)
            {
                SendKeys.Send("+{TAB}");
            }
        }

// form2.cs

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

 private void customtextbox3_KeyDown(object sender, KeyEventArgs e)
        {

            //code for results up and down in popup datagridview
           
            if (dataGridView1.CurrentCell == null)
            {
                return;
            }
            else
            {
                int rpos = dataGridView1.CurrentCell.RowIndex;
                int cpos = dataGridView1.CurrentCell.ColumnIndex;
                switch (e.KeyCode)
                {
                    case Keys.Up:

                        rpos--;
                        if (rpos >= 0) dataGridView1.CurrentCell = dataGridView1.Rows[rpos].Cells[cpos];
                        e.Handled = true;
                        break;
                    case Keys.Down:
                        //int rpos1 = dgvUserTypes.CurrentCell.RowIndex;
                        //int cpos1 = dgvUserTypes.CurrentCell.ColumnIndex;
                        rpos++;
                        if (rpos < dataGridView1.Rows.Count) dataGridView1.CurrentCell = dataGridView1.Rows[rpos].Cells[cpos];
                        e.Handled = true;
                        break;
                }
            }
        }

now i have used several of these textboxes in my form2 . the problem is for a particular customtextbox on this form2.cs , i don't want the base class's OnKeyDown implementation to be called as in my form2.cs , as i have used to same functionality along with some extra checks for this particular customtextbox . and at the same time i found out that the just after customtextbox3_KeyDown is called , the OnKeyDown is also called which disturbs the execution flow of my program . Please suggest me a way

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,873 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 114.7K Reputation points
    2023-05-13T16:31:31.67+00:00

    To control the features, try adding some properties to customtextbox, for example:

    public bool DisableFeatures { get; set; }
    
    protected override void OnKeyDown( KeyEventArgs e )
    {
        base.OnKeyDown( e );
    
        if( !DisableFeatures )
        {
            . . . .
    
        }
    }
    

    The property can be set in Form Designer for corresponding controls, or programmatically.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful