How to paste text in textbox current cursor?

Viewed 41205

How do you paste text into a TextBox at the current cursor position in Windows Forms?

Not textbox1 += string

9 Answers

This ensures that the cursor is at some position within the textbox, then inserts the text wherever the cursor is located.

        if (textBox1.CaretIndex <= 0)
        {

               textBox1.Focus();
     textBox1.Text = textBox1.Text.Insert(
                textBox1.CaretIndex, "Whatever");
        }
        else
        {
            textBox1.Text = textBox1.Text.Insert(
                textBox1.CaretIndex, "Whatever");
        }
Related