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

C# 如何在文本框当前光标中粘贴文本?,c#,textbox,C#,Textbox,在Windows窗体中,如何将文本粘贴到当前光标位置的文本框中 不textbox1+=string实现这一点的最佳方法是使用TextBox.Text.Insert(int indexSelectionStart,string Text)。此方法的作用是将文本插入到指定索引处的文本框中。它使用string string.insert(int startIndex,string value)作为文本框。文本是我们将在特定点插入文本的字符串。您希望在光标/选择器所在的位置插入文本,要查找该索引,我们可

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


textbox1+=string
实现这一点的最佳方法是使用TextBox.Text.Insert(int indexSelectionStart,string Text)。此方法的作用是将文本插入到指定索引处的文本框中。它使用
string string.insert(int startIndex,string value)
作为文本框。文本是我们将在特定点插入文本的字符串。您希望在光标/选择器所在的位置插入文本,要查找该索引,我们可以使用TextBox.SelectionStart

 textBox1.Text = textBox1.Text.Insert(textBox1.SelectionStart, "Whatever");
假设您的文本框名为textBox1。 假设您希望插入的文本存储在名为strInsert的字符串中,则代码可能是这样的


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

        if (textBox1.CaretIndex <= 0)
        {

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

if(textBox1.CaretIndex简单的方法是

textBox1.Paste();
用剪贴板的内容替换当前选择的

如果需要手动执行,则需要更多的工作。请记住,如果要“粘贴”,则要“替换”当前选择(如果有)。因此,您需要首先处理该问题。如果有选择,则需要保存选择开始,因为删除文本会使其出错

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完成了这项工作,我知道这已经很晚了,但最有效的方法似乎是:

textBox1.SelectedText = "Text";
请尝试以下代码:

 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);
    }
}
公共静态类InputExtensions
{
公共静态void InsertText(此文本框,字符串strippedText)
{
int start=textbox.SelectionStart;
字符串newText=textbox.Text;
newText=newText.Remove(textbox.SelectionStart,textbox.SelectionLength);
newText=newText.Insert(textbox.SelectionStart,strippedText);
Text=newtext;
textbox.SelectionStart=start+strippedText.Length;
}
公共静态无效删除(此文本框文本框)
{
var startedength=textbox.Text.Length;
if(textbox.Text.Length==0)返回;
var isSelection=textbox.SelectionLength>0;
var length=Math.Max(!isSelection?1:textbox.SelectionLength,0);
int start=textbox.SelectionStart;
字符串newText=textbox.Text;
如果(长度==0 | |开始+长度>惊人长度)返回;
newText=newText.Remove(开始,长度);
Text=newtext;
textbox.SelectionStart=start;
}
公共静态无效退格(此文本框文本框)
{
var startedength=textbox.Text.Length;
如果(惊人长度==0)返回;
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)返回;
字符串newText=textbox.Text;
newText=newText.Remove(开始,长度);
Text=newtext;
textbox.SelectionStart=start;
}
公共静态无效MoveCaretRight(此文本框文本框)
{
textbox.SelectionStart=Math.Min(Math.Max(0,textbox.SelectionStart+1),textbox.Text.Length);
}
公共静态无效MoveCaretLeft(此文本框文本框)
{
textbox.SelectionStart=Math.Min(Math.Max(0,textbox.SelectionStart-1),textbox.Text.Length);
}
公共静态布尔IsModifier(此KeyEventArgs e)
{
返回e.控制| | e.高度| | e.移位;
}
公共静态bool IsNavigationKey(此KeyEventArgs e)
{
开关(如钥匙代码)
{
案例密钥。向上:
案例键。向下:
箱子钥匙。左:
箱子钥匙。右:
case Keys.PageUp:
case Keys.PageDown:
返回true;
}
返回false;
}
公共静态bool为非数字(此KeyEventArgs为e)
{
var key=(char)e.KeyCode;
返回
字符(键)||
字符IsSymbol(键)||
char.IsWhiteSpace(键)||
字符ispunchuation(键);
}
公共静态无效粘贴(TextBox TextBox,Func charFilter=null)
{
var pasteText=Clipboard.GetText();
var strippedText=“”;
for(var i=0;i
为什么插入的光标会移动到文本框的开头?如何在插入文本后将光标聚焦到插入文本集
textBox1。选择开始
到您想要的任何位置。两件事:首先,这是一个直接的代码示例吗?在C#中,关键字var不用于创建变量,而是使用精确的数据类型。因此,您将使用字符串插入Text=“Text”int selectionIndex=textBox1.SelectionStart;.2,您不需要将textBox1.SelectionStart定义为它自己的变量,您可以直接调用它,但所有这一切都只是消除了一行代码,所以不是很重要。——Maximz2005 0秒前的
var
在C#3.5(或其他)中是新的,这是一种自动类型推断,为您节省了一些输入。无论如何,我看不出在字符串足够时使用var有什么意义。是的,但这是另一种方式,您可以选择使用它,也可以不使用。;
var
允许您在
List controls=new List()等情况下不重复自己的操作+1这更好,因为它不会
 string insertText = "Text";
            textBox1.Text = textBox1.Text+ insertText;
            textBox1.SelectionStart = textBox1.Text.Length +1;
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);
    }
}