我在WPA中有以下代码,我正在尝试将其转换为WPF.我试过Keydown而不是Keypress并改变了,例如,
(e.keyChar == '-') to (e.key == e.Subtract):
Run Code Online (Sandbox Code Playgroud)
第一个代码:
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 …Run Code Online (Sandbox Code Playgroud) 我在C#中做一个复杂的计算器.第一个文本框接受真实部分,第二个文本框接受虚构部分.我也希望能够使用鼠标输入值.因此,如果我单击button1,它将"1"连接到焦点所在的文本框中的值.我无法确定哪个文本框是关注的.我尝试了一些人发布的东西,比如使用GotFocus作为例子,并且没有用过..
private Control focusedControl;
private void TextBox_GotFocus(object sender, EventArgs e)
{
focusedControl = (Control)sender;
}
private void button1_Click(object sender, EventArgs e)
{
if (focusedControl != null)
{
focusedControl.Focus();
SendKeys.Send("1");
}
}
Run Code Online (Sandbox Code Playgroud)