开发者

How to use a function for every C# WinForm instead of pasting

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 windows form by esc. I really want to implement this to every windows form. I try to create a new abstract class which inherit from Form and another windows form will inherit from this one . But it doesn't work this way .

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 reading this :)


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
}


I would suggest not doing this in the Form but instead implement an 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());


If you want to use the designer, 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.

Apart from that, I don't think that HoaDonBanSach does not inherit your implementation of ProcessCmdKey. Except you've overriden it in another file (HoaDonBanSach is partial after all)


In the general sense where you wish to apply the same action to all forms, you can use an IMessageFilter as suggested by @Josh Einstein.

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

For example, in the forms designer, add two buttons to a form (button1, button2). Now go to the OnClick event handler field in the properties for button1, 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 property window, type in the name button1_Click, and button 2 will now share the same event handler for its clicks.

You can do the same thing in code. Open the designer-generated code and find the definition of the button2. You'll see that it now has a binding to the Click event 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 you could link multiple forms to the same event handler, for instance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜