4
votes

I am using windows forms and I have 2 buttons and a textBox.

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

I click one of the buttons so it types its text into the texbox, but the textbox loses the focus and the blinking cursor disappears so I fixed this by adding "textBox1.Focus()" in button click and now I have the blinking cursor but new issue came which is: the whole text got highlighted whenever I type a letter (by clicking on a button).

How can keep the blinking cursor (set focus the textBox) while clicking the buttons and get rid of the highlighted 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();
    }
}
1

1 Answers

4
votes

TextBoxBase.SelectionStart

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