c#将keydown从表单传递给usercontrol

时间:2016-03-31 23:24:11

标签: c#

我有一个计算器usercontrol,usercontrol被添加到我的表单中的面板:

enter image description here

在我的主要表格上我有:

 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
        }

在我的Calculator.cs中,我有:

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;
        }
  

有任何线索吗?

2 个答案:

答案 0 :(得分:0)

您可以将开关块移动到控件中的新公共方法,例如:

if ((currentUser != nil)) {

        if (PFFacebookUtils.isLinkedWithUser(currentUser!)) {
            proccessCurrent(currentUser!)

        } else {
            PFFacebookUtils.linkUserInBackground(currentUser!, withReadPermissions: self.facebookReadPermissions, block: {(success, error) in
                if (success) {
                    self.proccessCurrent(currentUser!)

                } else {
                    self.presentWalkThroughViewController()
                }
            })


        }
    } else {
        //var currentToken: FBSDKAccessToken = FBSDKAccessToken.currentAccessToken()

        if (FBSDKAccessToken.currentAccessToken() == nil) {
            self.presentWalkThroughViewController()

        } else {
            var currentToken: FBSDKAccessToken = FBSDKAccessToken.currentAccessToken()
                PFFacebookUtils.logInInBackgroundWithAccessToken(currentToken, block: {(user, error) in
                if (error == nil) {
                    user?.pinInBackgroundWithBlock({(success, error) in
                        if (success) {
                            self.proccessCurrent(user!)
                        } else {
                           self.presentWalkThroughViewController()
                        }

                    })
                } else {
                    self.presentWalkThroughViewController()
                }
            })
        }
    }

可以从表单(以及现有的控件事件处理程序,如果需要保留它)调用此方法。

作为旁注,如果将表单的KeyPreview-property设置为true,则不会出现焦点控件错误地获取事件的问题。有关此属性的详细信息,请参阅MSDN

答案 1 :(得分:0)

我这样做的一种方法是在用户控件上创建一个事件,并在实际事件发生时将其引发。所以,在用户控件中我会做类似的事情......

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);
        }
    }
}

然后在Main表单上,您将订阅该事件并处理它..

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