How do you paste text into a TextBox
at the current cursor position in Windows Forms?
Not textbox1 += string
First, get the current position of cursor with the help of property named as selectionStart on textarea/inputbox. To insert the text at the given position we will use slice function to break the string into two parts and then we will append both parts to the text(text_to_insert) in front and end of the text.
When you click a button you move the focused element so there is no cursor in the text anymore. So you would have to track the last location of the cursor and then just set the value of the text area to be the current value + the extra text at the location of the last know cursor position.
Select where you want a new section to begin. Go to Layout > Breaks, and then choose the type of section break you want.
Step 1: Create a windows form. Step 2: Drag the TextBox control from the ToolBox and Drop it on the windows form. You can place TextBox anywhere on the windows form according to your need. Step 3: After drag and drop you will go to the properties of the TextBox control to set the Text property of the TextBox.
A much easier way would be to use the Paste
method:
textbox1.Paste("text to insert");
I've done this using .NET 4.0
var insertText = "Text"; var selectionIndex = textBox1.SelectionStart; textBox1.Text = textBox1.Text.Insert(selectionIndex, insertText); textBox1.SelectionStart = selectionIndex + insertText.Length;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With