How do you paste text into a TextBox
at the current cursor position in Windows Forms?
Not textbox1 += string
try this code:
string insertText = "Text";
textBox1.Text = textBox1.Text+ insertText;
textBox1.SelectionStart = textBox1.Text.Length +1;
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);
I realize this is an old post but i hope this collection of methods for TextBox will help others struggling with manipulating this control.
public static class InputExtensions
{
public static void InsertText(this TextBox textbox, string strippedText)
{
int start = textbox.SelectionStart;
string newTxt = textbox.Text;
newTxt = newTxt.Remove(textbox.SelectionStart, textbox.SelectionLength);
newTxt = newTxt.Insert(textbox.SelectionStart, strippedText);
textbox.Text = newTxt;
textbox.SelectionStart = start + strippedText.Length;
}
public static void Delete(this TextBox textbox)
{
var startLength = textbox.Text.Length;
if (textbox.Text.Length == 0) return;
var isSelection = textbox.SelectionLength > 0;
var length = Math.Max(!isSelection ? 1 : textbox.SelectionLength, 0);
int start = textbox.SelectionStart;
string newTxt = textbox.Text;
if (length == 0 || start + length > startLength) return;
newTxt = newTxt.Remove(start, length);
textbox.Text = newTxt;
textbox.SelectionStart = start;
}
public static void Backspace(this TextBox textbox)
{
var startLength = textbox.Text.Length;
if (startLength == 0) return;
var isSelection = textbox.SelectionLength > 0;
var length = Math.Max(!isSelection ? 1 : textbox.SelectionLength, 0);
int start = Math.Max(textbox.SelectionStart - 1, 0);
if (length == 0 || start == 0) return;
string newTxt = textbox.Text;
newTxt = newTxt.Remove(start, length);
textbox.Text = newTxt;
textbox.SelectionStart = start;
}
public static void MoveCaretRight(this TextBox textbox)
{
textbox.SelectionStart = Math.Min(Math.Max(0, textbox.SelectionStart + 1), textbox.Text.Length);
}
public static void MoveCaretLeft(this TextBox textbox)
{
textbox.SelectionStart = Math.Min(Math.Max(0, textbox.SelectionStart - 1), textbox.Text.Length);
}
public static bool IsModifier(this KeyEventArgs e)
{
return e.Control || e.Alt || e.Shift;
}
public static bool IsNavigationKey(this KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
case Keys.Down:
case Keys.Left:
case Keys.Right:
case Keys.PageUp:
case Keys.PageDown:
return true;
}
return false;
}
public static bool IsNonNumber(this KeyEventArgs e)
{
var key = (char)e.KeyCode;
return
char.IsLetter(key) ||
char.IsSymbol(key) ||
char.IsWhiteSpace(key) ||
char.IsPunctuation(key);
}
public static void Paste(TextBox textbox, Func<char, int, bool> charFilter = null)
{
var pasteText = Clipboard.GetText();
var strippedText = "";
for (var i = 0; i < pasteText.Length; i++)
{
if (charFilter == null || charFilter(pasteText[i], i))
strippedText += pasteText[i].ToString();
}
InsertText(textbox, strippedText);
}
}