gpt4 book ai didi

c# - 如何确定哪个文本框获得焦点并向其添加文本…………?

转载 作者:太空宇宙 更新时间:2023-11-03 17:17:51 25 4
gpt4 key购买 nike

我正在用 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");

}

}

最佳答案

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";
}

}

您只需使用 TextBox_GotFocus 作为两个文本框的事件处理程序。

关于c# - 如何确定哪个文本框获得焦点并向其添加文本…………?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13043971/

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