【问题标题】:KeyPress event equivalent in WPFWPF 中等效的 KeyPress 事件
【发布时间】:2020-11-23 16:54:20
【问题描述】:

我在 WPA 中有以下代码,我正在尝试将其转换为 WPF。我尝试了 Keydown 而不是 Keypress 并更改了,例如,

(e.keyChar == '-') to (e.key == e.Subtract):
  1. 它的工作方式不一样
  2. 我在 e.key 中找不到等号

第一个代码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        foreach (TextBox tb in this.Controls.OfType<TextBox>())
        {
            tb.Enter += textBox_Enter;
        }
    }

    void textBox_Enter(object sender, EventArgs e)
    {
        focusedTextbox = (TextBox)sender;
    }

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {

        if (e.KeyChar == '+')
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 1;
            e.Handled = true;
        }
        else if (e.KeyChar == '-')
        {

            if (focusedTextbox != null)
            {
                if (focusedTextbox.Text == "")
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                    operand1.Real = getOperand.Real;
                    operand1.Imag = getOperand.Imag;
                    flag1 = 2;
                }
            }

        }
        else if (e.KeyChar == '*')
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 3;
            e.Handled = true;
        }
        else if (e.KeyChar == '/')
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 4;
            e.Handled = true;
        }
        else if (e.KeyChar == '=')
        {
            e.Handled = true;
            operand2.Real = getOperand.Real;
            operand2.Imag = getOperand.Imag;

            switch (flag1)
            {
                case 1:
                    operand1 = operand1 + operand2;
                    break;
                case 2: operand1 = operand1 - operand2;
                    break;
                case 3:
                    operand1 = operand1 * operand2;
                    break;
                case 4:
                    if (operand2.Magnitude == 0)
                    {
                        textBox1.Clear();
                        textBox2.Clear();
                        MessageBox.Show("Cannot divide by a number whose magnitude is zero");
                        operand1 = new Complex();
                        operand2 = new Complex();
                        listBox1.ClearSelected();

                    }
                    else
                    operand1 = operand1 / operand2;
                    break;
            }
            string s = operand1.ToString();
            if (flag == 1)
            {
                string[] s1 = s.Split(' ');

                if (s1[1] == "-")
                {
                    textBox1.Text = s1[0];
                    textBox2.Text = "-" + s1[3];
                }
                else
                {
                    textBox1.Text = s1[0];
                    textBox2.Text = s1[3];
                }
            }
            else if (flag == 2)
            {
                string[] s1 = s.Split('@');
                textBox1.Text = s1[0].Trim();
                textBox2.Text = s1[1].Trim();
            }

            listBox1.Items.Add(operand1);
        }

    }

第二个代码:

private void win_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Add)
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 1;
            e.Handled = true;

        }
        else if (e.Key == Key.Subtract)
        {

            if (textBox2.Text == "")
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
                operand1.Real = getOperand.Real;
                operand1.Imag = getOperand.Imag;
                flag1 = 2;
            }

        }
        else if (e.Key == Key.Multiply)
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 3;
            e.Handled = true;
        }
        else if (e.Key == Key.Divide)
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 4;
            e.Handled = true;
        }
        else if (e.Key == Key.Enter)
        {
            e.Handled = true;
            operand2.Real = getOperand.Real;
            operand2.Imag = getOperand.Imag;

            switch (flag1)
            {
                case 1:
                    operand1 = operand1 + operand2;
                    break;
                case 2: operand1 = operand1 - operand2;
                    break;
                case 3:
                    operand1 = operand1 * operand2;
                    break;
                case 4:
                    if (operand2.Magnitude == 0)
                    {
                        textBox1.Clear();
                        textBox2.Clear();
                        MessageBox.Show("Cannot divide by a number whose magnitude is zero");
                        operand1 = new Complex();
                        operand2 = new Complex();
                        listBox1.UnselectAll();
                    }
                    else
                        operand1 = operand1 / operand2;
                    break;
            }
            string s = operand1.ToString();
            if (flag == 1)
            {
                string[] s1 = s.Split(' ');

                if (s1[1] == "-")
                {
                    textBox1.Text = s1[0];
                    textBox2.Text = "-" + s1[3];
                }
                else
                {
                    textBox1.Text = s1[0];
                    textBox2.Text = s1[3];
                }
            }
            else if (flag == 2)
            {
                string[] s1 = s.Split('@');
                textBox1.Text = s1[0].Trim();
                textBox2.Text = s1[1].Trim();
            }


            listBox1.Items.Add(operand1);
        }
    }

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    非常相似 - 但您将 e.Key 与 Key 枚举进行比较。

    在某处注册您的事件处理程序(例如构造函数或 window_loaded):

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.KeyDown += new KeyEventHandler(MainWindow_KeyDown);
    }
    

    然后在事件处理程序中:

    void MainWindow_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Subtract)
        {
            // Do something
        }
    }
    

    【讨论】:

    • 我刚刚发现没有任何效果,,,,我的意思是。我有 2 个 texchanged 事件和 keyDown 事件,并且只有第一个 textchanged 正在工作,另一个根本没有被执行..
    • Window_Loaded 是在设计器中双击 WPF 窗口时为您生成的方法。它在 WPF 窗口完成加载时运行。听起来您已经重新输入了该方法,而不仅仅是在已经存在的事件处理程序中注册了事件处理程序。尝试制作一个全新的 WPF 应用程序并让我的示例在那里工作
    • 现在我正在使用具有相同代码的 textbox1_keyDown ,我做了以下 private void textBox1_KeyDown(object sender, KeyEventArgs e) { // MessageBox.Show("msg"); if (e.Key == Key.Add) { //MessageBox.Show("msg2");操作数1.Real = getOperand.Real;操作数1.Imag = getOperand.Imag;标志1 = 1; e.Handled = true; {....} 我可以看到 msg1 任何时候我按下任何东西,但如果我按下“+”,即使在关闭 msg1 的消息框后我也看不到 msg2。
    • 如果 (e.Key == Key.Add)----- 它对所有符号都一样,而不仅仅是 +
    • ------------------------------------------ -----------------
    【解决方案2】:

    您正在寻找TextInput 事件,或者可能是PreviewTextInput

    EventArgs 类型是TextCompositionEventArgs;我相信您想要这里的 Text 属性,但我完全不确定。

    【讨论】:

    • 谢谢,正是我需要的。您会以字符串形式获得任何可打印的密钥。避免执行依赖于区域的巨大 switch 语句。
    【解决方案3】:
    <TextBox x:Name="txtbx" KeyDown="OnKeyDownHandler" Height="23" Width="250">
    </TextBox>
    
    private void OnKeyDownHandler(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Return)
        {
            txtbx.Text = "You Entered: " + txtbx.Text;
        }
    }
    

    【讨论】:

    • 很高兴看到有人为没有经验的人添加了 XML 部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 2012-09-17
    相关资源
    最近更新 更多