我在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)
private TextBox focusedControl;
private void TextBox_GotFocus(object sender, EventArgs e)
{
focusedControl = (TextBox)sender;
}
private void button1_Click(object sender, EventArgs e)
{
if (focusedControl != null)
{
focusedControl.Text += "1";
}
}
Run Code Online (Sandbox Code Playgroud)
您只需使用TextBox_GotFocus作为两个文本框的EventHandler.