How to focus on a TextBox while typing into it?

I use windows, and I have 2 buttons and a text box.

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

I press one of the buttons, so it types in the texbox, but the text field loses focus, and the blinking cursor disappears, so I fixed it by adding “textBox1.Focus ()” to the button, and now I have a blinking cursor, but There was a new problem: all the text was highlighted whenever I type a letter (by clicking on the button).

How to save a blinking cursor (set the focus of the text field) by clicking on the buttons and get rid of the selected text? thank you

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();
    }
}
+4
source share
1 answer

TextBoxBase.SelectionStart

textBox1.Focus();
textBox1.SelectionStart = textBox1.Text.Length;
+4
source

Source: https://habr.com/ru/post/1615815/


All Articles