gpt4 book ai didi

c# - WPF 中等效的 KeyPress 事件

转载 作者:IT王子 更新时间:2023-10-29 04:47:06 26 4
gpt4 key购买 nike

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

最佳答案

它非常相似 - 但您将 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
}
}

关于c# - WPF 中等效的 KeyPress 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13587270/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com