Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#将键从表单传递到用户控件_C# - Fatal编程技术网

c#将键从表单传递到用户控件

c#将键从表单传递到用户控件,c#,C#,我有一个计算器usercontrol,usercontrol以我的形式添加到面板中: 在我的主要表格上,我有: 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

我有一个计算器usercontrol,usercontrol以我的形式添加到面板中:

在我的主要表格上,我有:

 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;
        }
有线索吗


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

public void HandleKeyEvent(KeyEventArgs e) { ... }
可以从表单(如果需要保留现有控件事件处理程序,还可以从现有控件事件处理程序)调用此方法


另请注意,如果将窗体的KeyPreview属性设置为true,则不会出现焦点控件错误获取事件的问题。有关此属性的详细信息,请参阅。

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

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);
        }
    }
}
public分部类计算器:UserControl
{
//创建一个事件
公共事件事件处理程序OnKeyPressed;
公共计算器()
{
初始化组件();
}
私有无效计算器\u按键(对象发送器,按键事件参数e)
{
//当按键发生且有人正在收听时引发事件
如果(OnKeyPressed!=null)
{
按下ON键(发送器,e);
}
}
}
然后在主窗体上订阅事件并处理它

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
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
//订阅我们在用户控件上创建的事件
calculator1.OnKeyPressed+=新的事件处理程序(MyKeyPressHandlerInInform);
}
私有void MyKeyPressHandlerInInform(对象发送者,KeyPressEventArgs e)
{
//处理事件。在这里,您可以编写逻辑。
//由于您将keypressEventArgs作为参数输入,因此您可以
//确定按下了什么键以及所有这些。
}
}
在这里输入代码