How to paste text in textbox current cursor?

前端 未结 9 866
灰色年华
灰色年华 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:37

    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");
            }
    

提交回复
热议问题