Avoid repeatation of code

Shaifali jain 420 Reputation points
2023-09-23T07:16:21.7033333+00:00

Hi ,

i have a project with 4 windows forms all of doing everything separate work . but they all a common thing in them .

  protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {

            if (keyData == (Keys.F2))
            {
                // call date form 
                    return true;
               
                
            }

            return base.ProcessCmdKey(ref msg, keyData);

        }


how can make sure that all the forms have this functionality but choose not to write same code 4 times . also i cannot use inheritance as all these forms already inherits from some other parent forms .

Developer technologies Windows Forms
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. KOZ6.0 6,655 Reputation points
    2023-09-23T08:08:41.41+00:00

    create another inheritance form in between.

    BaseForm → FormA, FormB, FormC, FormD

    BaseForm → BaseForm2 → FormA, FormB, FormC, FormD

    0 comments No comments

0 additional answers

Sort by: Most helpful

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.