Reputation: 7348
How do you paste text into a TextBox
at the current cursor position in Windows Forms?
Not textbox1 += string
Upvotes: 44
Views: 43877
Reputation: 1907
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);
}
}
Upvotes: 1
Reputation: 865
var insertText = "Text";
var selectionIndex = textBox1.SelectionStart;
textBox1.Text = textBox1.Text.Insert(selectionIndex, insertText);
textBox1.SelectionStart = selectionIndex + insertText.Length;
Upvotes: 51
Reputation: 3360
try this code:
string insertText = "Text";
textBox1.Text = textBox1.Text+ insertText;
textBox1.SelectionStart = textBox1.Text.Length +1;
Upvotes: 0
Reputation: 67195
I know this is late but the most efficient way appears to be:
textBox1.SelectedText = "Text";
Upvotes: 6
Reputation: 8249
A much easier way would be to use the Paste
method:
textbox1.Paste("text to insert");
I've done this using .NET 4.0
Upvotes: 52
Reputation: 31
The simple way would be
textBox1.Paste();
That replaces the current selection with the contents of the clipboard.
If you need to do it manually then it's a bit more work. Remember if you're "pasting" then you are "replacing" the current selection if there is one. So you need to handle that first. You'll need to save SelectionStart if you had a selection as removing the text will screw it up.
string newText = "your text";
int start = textBox1.SelectionStart;
bool haveSelection = textBox1.SelectionLength > 0;
string text = (haveSelection) ? textBox1.Text.Remove(start,textBox1.SelectionLength) : textBox1.Text;
textBox1.Text = text.Insert(start,newText);
if(haveSelection)
{
textBox1.SelectionStart = start;
textBox1.SelectionLength = newText.Length;
}
After you're done you'll want to invalidate the control to force it to redraw.
textBox1.Invalidate();
Upvotes: 1
Reputation: 11
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");
}
Upvotes: 1
Reputation: 18065
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);
Upvotes: 1
Reputation: 20705
textBox1.Text = textBox1.Text.Insert(textBox1.SelectionStart, "Whatever");
Upvotes: 5