如何在输入时保持焦点在文本框上?

4

我正在使用Windows Forms,并有两个按钮和一个文本框。

button1.text = 1,button2.text = 2;

我点击其中一个按钮,它会将其文本输入到文本框中,但是文本框失去了焦点,闪烁的光标消失了,所以我在按钮单击事件中添加了 "textBox1.Focus()" 来解决这个问题,现在我有了闪烁的光标,但是出现了新问题:每次我通过点击按钮输入字母时,整个文本都会被突出显示。

如何在点击按钮时保持闪烁的光标(设置文本框的焦点)并消除突出显示的文本?谢谢。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }



    private void Form1_Load(object sender, EventArgs e)
    {
        ActiveControl = textBox1;
        textBox1.Focus();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        textBox1.Text = textBox1.Text + button1.Text;
        textBox1.Focus();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        textBox1.Text = textBox1.Text + button2.Text;
        textBox1.Focus();
    }
}
1个回答

4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接