Remove duplicate code

ankit goel 766 Reputation points
2023-11-04T08:33:43.81+00:00

Hi ,

i have the following code in the textbox(used from another user) .

  private void textBox4_KeyDown(object sender, KeyEventArgs e)
        {
           
            if (dataGridView1.CurrentCell == null)
            {
                return;
            }
            else
            {
                rpos = dataGridView1.CurrentCell.RowIndex;
                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:

                        rpos++;
                        if (rpos < dataGridView1.Rows.Count) dataGridView1.CurrentCell = dataGridView1.Rows[rpos].Cells[cpos];
                        e.Handled = true;
                        break;


now i also i have a customdatagridview on which i have the below code

   protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
        {
            

            e.Control.KeyPress -= new KeyPressEventHandler(textbox_keypress);
            e.Control.TextChanged -= new EventHandler(TextBox_TextChanged);
            e.Control.KeyDown -= new KeyEventHandler(TextBox_KeyDown);
            e.Control.Enter -= new EventHandler(TextBox_Enter);
            if (this.CurrentCell.ColumnIndex == 1 || this.CurrentCell.ColumnIndex == 2) // 
            {
                TextBox textBox = e.Control as TextBox;
                if (textBox != null)
                {
                    textBox.KeyPress += new KeyPressEventHandler(textbox_keypress);
                }
            }

            else if (this.CurrentCell.ColumnIndex == 0) 
            {
                TextBox textBox = e.Control as TextBox;
                if (textBox != null)
                {
                    textBox.TextChanged += TextBox_TextChanged; // Attach TextChanged event handler
                    textBox.KeyDown += TextBox_KeyDown; // Attach KeyDown event handler
                    textBox.Enter += TextBox_Enter;
                }
            }
            base.OnEditingControlShowing(e);
        }

  private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.CurrentCell.ColumnIndex == 0) // Assuming the first column has an index of 0
            {
                TextBox textBox = (TextBox)sender;

                //code for results up and down in popup datagridview

                if (dgv.CurrentCell == null)
                {
                    return;
                }
                else
                {
                    
                    rpos = dgv.CurrentCell.RowIndex;
                    cpos = dgv.CurrentCell.ColumnIndex;
                    switch (e.KeyCode)
                    {
                        case Keys.Up:

                            rpos--;
                            if (rpos >= 0) dgv.CurrentCell = dgv.Rows[rpos].Cells[cpos];
                            e.Handled = true;
                            break;
                        case Keys.Down:

                            rpos++;
                            if (rpos < dgv.Rows.Count) dgv.CurrentCell = dgv.Rows[rpos].Cells[cpos];
                            e.Handled = true;


                            break;


I want to create a common method which works for both controls as the maximum code is similar so that the duplicacy of the code can be removed .

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,906 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.
11,090 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 47,206 Reputation points Microsoft Vendor
    2023-11-06T06:55:03.66+00:00

    Hi @ankit goel , Welcome to Microsoft Q&A,

    Yes, just extract the same code.

    For example:

    private void customevent(DataGridView dgv, KeyEventArgs e)
    {
        //code for results up and down in popup datagridview
        if (dgv.CurrentCell == null)
        {
            return;
        }
        else
        {
    
            int rpos = dgv.CurrentCell.RowIndex;
            int cpos = dgv.CurrentCell.ColumnIndex;
            switch (e.KeyCode)
            {
                case Keys.Up:
    
                    rpos--;
                    if (rpos >= 0) dgv.CurrentCell = dgv.Rows[rpos].Cells[cpos];
                    e.Handled = true;
                    break;
                case Keys.Down:
    
                    rpos++;
                    if (rpos < dgv.Rows.Count) dgv.CurrentCell = dgv.Rows[rpos].Cells[cpos];
                    e.Handled = true;
                    break;
            }
    
        }
    }
    
    

    Then you can use it in a similar way.

    private void textBox2_KeyDown(object sender, KeyEventArgs e)
    {
        if (dgv.CurrentCell.ColumnIndex == 0) // Assuming the first column has an index of 0
        {
            TextBox textBox = (TextBox)sender;
            customevent(dgv, e);
        }
    }
    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        customevent(dataGridView1, e);
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.