Pyromancer
Pyromancer

Reputation: 2509

creating shortcut key to all forms

I am planning to create a shortcut key to all my forms, I put this code to all my forms

public Form1()
        {
            InitializeComponent();
            this.KeyDown +=new KeyEventHandler(Form1_KeyDown);
        }

private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Button1.PerformClick();
            }
            else if (e.KeyCode == Keys.Escape)
            {
                button2.PerformClick();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
        }

is there any way, so that I will not repeat this codes in all my forms, thanks :)

Upvotes: 0

Views: 938

Answers (1)

andy
andy

Reputation: 6079

Use Form Base Class ... there you can write the code and inherit all the forms from that Base Form.

Raise Base Class Events in Derived Classes (C# Programming Guide)

Upvotes: 2

Related Questions