回答
var insertText = "Text";
var selectionIndex = textBox1.SelectionStart;
textBox1.Text = textBox1.Text.Insert(selectionIndex, insertText);
textBox1.SelectionStart = selectionIndex + insertText.Length;
textBox1.Text = textBox1.Text.Insert(textBox1.SelectionStart, "Whatever");
为什么插入光标移动到开始文本框? – 2009-09-12 23:48:39
插入文本 – 2009-09-12 23:49:58
将'textBox1.SelectionStart'设置为您想要的任何位置后,光标如何能够在插入后集中注意力。 – Aziz 2009-09-12 23:52:05
做到这一点,最好的办法是使用TextBox.Text.Insert(INT indexSelectionStart,字符串文本)。这个方法的作用是:将文本插入文本框,在您指定的索引 - 它使用string string.insert(int startIndex, string value)
作为TextBox.Text是我们要在特定点插入文本的字符串。您希望插入文字,其中光标/选择器是,并且要找到该索引,我们可以使用TextBox.SelectionStart。
假设您的文本框名为textBox1。 这是代码的样子,假设您要插入的文本存储在名为的字符串strInsert中。
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();
替换为剪贴板中的内容的当前选择。
如果您需要手动完成,那么这是一个更多的工作。请记住,如果您正在“粘贴”,那么您将“替换”当前的选择(如果有的话)。所以你需要先处理。你需要保存SelectionStart,如果你有选择,删除文本会把它搞砸。
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();
一个更容易的方法是使用Paste
方法:
textbox1.Paste("text to insert");
我做这个使用.NET 4.0
+1它好多了,因为它不会影响你的光标位置和滚动。 – 2014-03-01 12:47:51
这是简单和完美的工作 有了这个,当你选择一些字符串,你可以覆盖到选定的字符串 – kordiseps 2017-12-18 15:45:55
我知道这是迟到,但似乎最有效的方法是:
textBox1.SelectedText = "Text";
是的。 Upvoted。 – AndrewBenjamin 2015-03-27 17:34:45
如何在asp.net中实现相同,因为我没有在asp.net中找到SelectedText属性TextBox控件 – Sri 2016-06-16 10:18:05
@Sri,很明显,因为ASP.NET服务器没有关于客户端的信息(例如光标位置)。您应该使用JavaScript代码进行此类操作。 – jAC 2017-05-31 14:52:52
试试这个代码:
string insertText = "Text";
textBox1.Text = textBox1.Text+ insertText;
textBox1.SelectionStart = textBox1.Text.Length +1;
我意识到这是一个旧帖子,但我希望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);
}
}
- 1. 粘贴文本框中光标处的文本行
- 2. 文本框粘贴问题
- 3. 在多个文本框中粘贴serialnumber
- 4. 如何在vim中使用鼠标选择一段文本并将其粘贴到当前光标位置(无)?
- 5. 将文本粘贴到文本视图之前捕获文本
- 6. 如何从剪贴板粘贴文本?
- 7. 在React Component的文本框中复制文本的粘贴
- 8. 在GWT文本框中捕获粘贴的文本
- 9. BAT脚本粘贴文本
- 10. 如何在MFC中限制文本框中的复制粘贴?
- 11. 文本粘贴中的R
- 12. 如何检测在WinForms文本框中粘贴的传入文本ctrl-v?
- 13. 如何在复制文本框中的格式化文本时粘贴值
- 14. 在鼠标粘贴事件中获取文本框的值
- 15. 在光标下方键入并获取文本框当前词
- 16. 如何使用Ctrl + v在文本框中启用粘贴
- 17. 如何在文本框中检测粘贴操作?
- 18. 如何防止在文本框中粘贴非数值?
- 19. 从粘贴的文本去掉文本框中的“http://”
- 20. 多语言文本复制/粘贴到WinForms中的文本框?
- 21. 如何向前移动文本光标?
- 22. 制作链接粘贴到文本框
- 23. 硒粘贴文本到文本区域
- 24. Javascript?文档粘贴事件 - 获取粘贴的文本在FireFox
- 25. 网站如何抑制粘贴文本?
- 26. 如何修改粘贴的文本?
- 27. 如何使用js粘贴文本行?
- 28. 当我粘贴文本,它不能得到文本的高度
- 29. 在文本框中的光标对齐
- 30. 如何防止在文本框上粘贴Alpha版窗口
两件事:首先,是直接的代码示例?在C#中,关键字var不用于创建变量,而是使用精确的数据类型。因此,你可以使用* string insertText =“Text”; int selectionIndex = textBox1.SelectionStart; *。第二,你不需要将textBox1.SelectionStart定义为它自己的变量,你可以直接调用它,但是所有这些都会消除一行代码,所以不是很重要。 - Maximz2005 0秒前 – 2009-09-13 00:25:37
'var'是C#3.5(或其他)中的新成员,它是一种自动类型推断,为您节省了一些键入空间。请参阅http://msdn.microsoft.com/en-us/library/bb383973.aspx – Svend 2009-09-13 00:49:22
哇!我从来不知道!非常酷! – 2009-09-13 01:41:37