如何在文本框当前光标中粘贴文本?
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);
}
}
两件事:首先,是直接的代码示例?在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