gpt4 book ai didi

c# - 如何在C#中设计键盘?

转载 作者:行者123 更新时间:2023-11-30 21:53:45 25 4
gpt4 key购买 nike

我是 C# 的新手,我打算设计自己的键盘,但我不知道如何/从哪里开始。如图所示,我有 4 个文本框和键盘按钮。

我想到的第一个问题是:如何检测光标位置(光标在哪个 textBox 中?)。

例如,如果我只有一个文本框,那么我可以很容易地在 button1 内部编写:textBox1.text = "1"并在 button2 内部编写:textBox1.text = "2"并在 button_A 内部编写:textBox1.text = "A "....等等,但我有 4 个文本框,这很困惑。

能否请您提供一个想法或在每个按钮内写什么以在光标所在的文本框中打印其值。

谢谢专业人士。

Photo

最佳答案

首先,有一个代表被选中的文本框(在子例程之外但在类之内):

TextBox SelectedTextBox = null;

然后让每个 TextBox 的“Click”事件看起来像这样:

private void textBoxNUM_Click(object sender, EventArgs e)
{
SelectedTextBox = sender as TextBox;
}

然后让每个Button的“Click”事件看起来像这样:

private void buttonNUM_Click(object sender, EventArgs e)
{
if (SelectedTextBox != null)
{
SelectedTextBox.Text = buttonNUM.Text;//Or set it to the actual value, whatever.
}
}

或者如果那个不起作用,这个应该。

private void buttonNUM_Click(object sender, EventArgs e)
{
if (SelectedTextBox != null)
{
(SelectedTextBox as TextBox).Text = buttonNUM.Text;//Or set it to the actual value, whatever.
}
}

关于c# - 如何在C#中设计键盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33680680/

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