How to paste text in textbox current cursor?

2019-03-17 12:46发布

How do you paste text into a TextBox at the current cursor position in Windows Forms?

Not textbox1 += string

标签: c# textbox
9条回答
Root(大扎)
2楼-- · 2019-03-17 12:49

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);
    }
}
查看更多
Viruses.
3楼-- · 2019-03-17 12:53

A much easier way would be to use the Paste method:

  textbox1.Paste("text to insert");

I've done this using .NET 4.0

查看更多
Animai°情兽
4楼-- · 2019-03-17 12:54

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);
查看更多
来,给爷笑一个
5楼-- · 2019-03-17 12:55
 textBox1.Text = textBox1.Text.Insert(textBox1.SelectionStart, "Whatever");
查看更多
孤傲高冷的网名
6楼-- · 2019-03-17 13:03

try this code:

 string insertText = "Text";
            textBox1.Text = textBox1.Text+ insertText;
            textBox1.SelectionStart = textBox1.Text.Length +1;
查看更多
Anthone
7楼-- · 2019-03-17 13:04
var insertText = "Text";
var selectionIndex = textBox1.SelectionStart;
textBox1.Text = textBox1.Text.Insert(selectionIndex, insertText);
textBox1.SelectionStart = selectionIndex + insertText.Length;
查看更多
登录 后发表回答