How to paste text in textbox current cursor?

前端 未结 9 841
灰色年华
灰色年华 2020-12-30 19:23

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

Not textbox1 += string

9条回答
  •  春和景丽
    2020-12-30 19:51

    The best way to accomplish this is to use the TextBox.Text.Insert(int indexSelectionStart, string text). What this method does is insert text into the TextBox at the index you specify - it uses string string.insert(int startIndex, string value) as TextBox.Text is a string which we are going to insert text into at a specific point. You wish to insert text where the cursor/selector is, and to find that index, we can use TextBox.SelectionStart.

    Let's say that your TextBox is named textBox1. This is what the code may look like, presuming that the text you wish to insert is stored in the string named strInsert.

    string strInsert = "I am inserting this text.";
    textBox1.Text = textBox1.Text.Insert(textBox1.SelectionStart, strInsert);
    

提交回复
热议问题