C# WPF中的按键事件等价物
我在WPA中有以下代码,我正在尝试将其转换为WPF。我试着用Keydown而不是Keypress来改变,比如C# WPF中的按键事件等价物,c#,wpf,C#,Wpf,我在WPA中有以下代码,我正在尝试将其转换为WPF。我试着用Keydown而不是Keypress来改变,比如 (e.keyChar == '-') to (e.key == e.Subtract): 它的工作原理不一样 我在e键中找不到等号 第一个代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); foreach (TextBox tb i
(e.keyChar == '-') to (e.key == e.Subtract):
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);
}
}
这非常相似-但是您可以将e.Key与Key枚举进行比较 在某处注册事件处理程序(如构造函数或加载的窗口): 然后在事件处理程序中:
void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Subtract)
{
// Do something
}
}
你正在寻找这个事件,或者可能
EventArgs类型为;我相信您想要这里的文本
属性,但我一点也不确定。
private void OnKeyDownHandler(对象发送方,KeyEventArgs e)
{
if(e.Key==Key.Return)
{
txtbx.Text=“您输入:”+txtbx.Text;
}
}
我刚发现什么都没用,,,,我是说。我有2个textchanged事件和keyDown事件,只有第一个textchanged起作用,另一个根本没有执行。Window_Loaded是在设计器中双击WPF窗口时为您生成的方法。它在WPF窗口完成加载后运行。听起来您重新键入了该方法,而不仅仅是在已经存在的方法中注册事件处理程序。尝试创建一个全新的WPF应用程序,并让我的示例在其中运行现在我使用的textbox1_keyDown具有相同的代码,,,我做了以下私有的void textbox1_keyDown(object sender,KeyEventArgs e){//MessageBox.Show(“msg”);if(e.Key==Key.Add){//MessageBox.Show(“msg2”);operan1.Real=getOperand.Real;operan1.Imag=getOperand.Imag;flag1=1;e.Handled=true;{……}无论何时按下任何键,我都可以看到msg1,但如果按下“+”键,即使在关闭msg1的消息框后,我也看不到msg2。问题是否仍然存在于if(e.Key==Key.Add)中------它同样适用于所有符号,不仅仅适用于+----------------------------------------------------------------很高兴看到有人为没有经验的人提供XML部分。
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
}
}