简体   繁体   中英

c# pass keydown from form to usercontrol

I have a calculator usercontrol, the usercontrol is added to a panel in my form:

在此处输入图片说明

On my main Form I have:

 private void frmPOS_KeyDown(object sender, KeyEventArgs e)
        {
            // here I want to pass the keydown captured to my calculator
            // usercontrol so the KeyDown event is fired in my usercontrol
        }

In my Calculator.cs I have:

private void Calculator_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.NumPad0:
                    // one
                    lblResult.Text = "0";
                    break;
                case Keys.NumPad1:
                    // one
                    lblResult.Text = "1";
                    break;
                case Keys.NumPad2:
                    // two
                    lblResult.Text = "2";
                    break;
                // .. etc
                case Keys.Add:
                    // Plus
                    break;
                default:
                    // Avoid setting e.Handled to                 
                    return;
            }
            e.Handled = true;
        }

Any clue?

You can move your switch block to a new public method in the control, eg.:

public void HandleKeyEvent(KeyEventArgs e) { ... }

This method can be invoked from your form (and your existing control event handler if you need to keep it).

As a side note, if you set the form's KeyPreview-property to true you won't get problems with a focused control incorrectly getting the event. See MSDN for more information about this property.

One way I have done this is by creating an event on the user control and raise it when the actual event happens. So, in the user control I would do something like...

public partial class Calculator : UserControl
{
    //create an event
    public event EventHandler<KeyPressEventArgs> OnKeyPressed;

    public Calculator()
    {
        InitializeComponent();
    }

    private void Calculator_KeyPress(object sender, KeyPressEventArgs e)
    {
        //raise the event when the key press happens and someone is listening
        if (OnKeyPressed != null)
        {
            OnKeyPressed(sender, e);
        }
    }
}

and then on the Main form you would subscribe to the event and handle it..

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        //subscribe to the event we create on the user control
        calculator1.OnKeyPressed += new EventHandler<KeyPressEventArgs>(MyKeyPressHandlerInMainForm);
    }

    private void MyKeyPressHandlerInMainForm(object sender, KeyPressEventArgs e)
    {
        //Handle the event. Here you would write your logic.
        //Since you have keypressEventArgs coming in as a parameter you would be able to 
        //do determine what key was pressed and all that.
    }
}

enter code here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM