如何在文本框中粘贴文本当前光标?

如何将文本粘贴到Windows窗体中当前光标位置的TextBox

不是 textbox1 += string

 var insertText = "Text"; var selectionIndex = textBox1.SelectionStart; textBox1.Text = textBox1.Text.Insert(selectionIndex, insertText); textBox1.SelectionStart = selectionIndex + insertText.Length; 

更简单的方法是使用Paste方法:

  textbox1.Paste("text to insert"); 

我已经使用.NET 4.0做了这个

  textBox1.Text = textBox1.Text.Insert(textBox1.SelectionStart, "Whatever"); 

我知道这是迟到,但最有效的方式似乎是:

 textBox1.SelectedText = "Text"; 

最好的方法是使用TextBox.Text.Insert(int indexSelectionStart,string text) 。 这个方法的作用是在指定的索引处 插入文本到文本框中 – 它使用string string.insert(int startIndex, string value)作为TextBox.Text是一个string,我们将在特定的位置插入文本。 你希望在光标/select器所在的位置插入文本,并find该索引,我们可以使用TextBox.SelectionStart

假设你的TextBox被命名为textBox1。 这就是代码的样子,假设你想要插入的文本存储在名为strInsert的string中。

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

这可以确保光标位于文本框的某个位置,然后在光标所在的位置插入文本。

  if (textBox1.CaretIndex <= 0) { textBox1.Focus(); textBox1.Text = textBox1.Text.Insert( textBox1.CaretIndex, "Whatever"); } else { textBox1.Text = textBox1.Text.Insert( textBox1.CaretIndex, "Whatever"); } 

简单的方法是

 textBox1.Paste(); 

用剪贴板的内容replace当前的select。

如果你需要手动做,那么这是一个更多的工作。 请记住,如果您正在“粘贴”,那么您将“取代”当前select(如果有的话)。 所以你需要先处理。 你需要保存SelectionStart如果你有一个select作为删除文本会搞砸了。

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

完成后,您需要使控件失效,强制重画。

 textBox1.Invalidate(); 

试试这个代码:

  string insertText = "Text"; textBox1.Text = textBox1.Text+ insertText; textBox1.SelectionStart = textBox1.Text.Length +1; 

我意识到这是一个旧的post,但我希望这个TextBox的方法集合将帮助其他人操纵这个控制挣扎。

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