【问题标题】:How to keep focus on TextBox while typing in it?输入时如何保持对文本框的关注?
【发布时间】:2016-02-15 11:30:46
【问题描述】:

我正在使用 Windows 窗体,我有 2 个按钮和一个文本框。

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

我单击其中一个按钮,将其文本输入到 texbox,但文本框失去焦点,闪烁的光标消失,所以我通过在按钮单击中添加“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();
    }
}

【问题讨论】:

    标签: c#


    【解决方案1】:

    TextBoxBase.SelectionStart

    textBox1.Focus();
    textBox1.SelectionStart = textBox1.Text.Length;
    

    【讨论】:

      猜你喜欢
      • 2011-10-08
      • 1970-01-01
      • 2022-08-17
      • 1970-01-01
      • 1970-01-01
      • 2015-07-09
      • 2016-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多