How to use a function for each C # WinForm instead of insert

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            {
                if (keyData == Keys.Escape) this.Close();
                return base.ProcessCmdKey(ref msg, keyData);
            }
        }

      

I discovered this snippet to close the esc form window. I really want to implement this for every window form. I am trying to create a new abstract class that inherits from form and another window form inherits from that. But it doesn't work.

abstract class AbsForm: Form {
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            {
                if (keyData == Keys.Escape) this.Close();
                return base.ProcessCmdKey(ref msg, keyData);
            }
        }
    }
    public partial class HoaDonBanSach : AbsForm
    {
        public HoaDonBanSach()
        {
            InitializeComponent();
        }

      

Thanks for this:)

+2


a source to share


4 answers


I would suggest not doing this on the form, but instead implement the IMessageFilter and add it using Application.AddMessageFilter. Something like the following:



public class CloseWindowBehavior : IMessageFilter {

    const int WM_KEYDOWN = 0x100;
    const int VK_ESCAPE = 0x1B;

    bool IMessageFilter.PreFilterMessage(ref Message m) {
        if (m.Msg == WM_KEYDOWN && (int)m.WParam == VK_ESCAPE) {
            if (Form.ActiveForm != null) {
                Form.ActiveForm.Close();
            }
            return true;
        }
        return false;
    }

}

Application.AddMessageFilter(new CloseWindowBehavior());

      

+2


a source


Try this instead:



class CustomForm : Form 
{
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == Keys.Escape)
        {
            this.Close();
        }

        return base.ProcessCmdKey(ref msg, keyData);
    }
}

class InheritedForm : CustomForm
{
    // this form now has the functionality from CustomForm
}

      

+3


a source


If you want to use a constructor, you cannot mark your base class as abstract.

The designer will instantiate the base class of the form you are designing, which cannot be done with abstract classes.

Also, I don't think HoaDonBanSach doesn't inherit your ProcessCmdKey implementation. Except you have overridden it in another file (HoaDonBanSach is partial after all)

0


a source


In a general sense, when you want to apply the same action to all forms, you can use the IMessageFilter as suggested by @Josh Einstein.

If you want to share one event handler for multiple controls / forms (but not all), then another approach is to direct them all to use the same event handler method.

For example, in the form designer, add two buttons to the form (button1, button2). Now go to the OnClick event handler field in the properties of the button1 button and double click. This creates a button1_Click event handler method for you. Now go to Button 2 and in the Click event handler field in the Properties window enter the name button1_Click and Button 2 will now use the same event handler for its clicks.

You can do the same in code. Open the code generated by the designer and find the definition of button 2. You will see that it now has a Click event binding for the event handler method:

this.button2.Click += new System.EventHandler(this.button1_Click);

      

You can add bindings as code for any event in the same way so that you can bind multiple forms to the same event handler eg.

0


a source







All Articles