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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1416454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:18:28  来源:igfitidea点击:

How to paste text in textbox current cursor?

c#textbox

提问by monkey_boys

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

如何将文本粘贴到TextBoxWindows 窗体中当前光标位置的 a 中?

Nottextbox1 += string

不是textbox1 += string

采纳答案by MNZ

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

回答by Aziz

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

回答by Maxim Zaslavsky

The best way to accomplish this is to use the TextBox.Text.Insert(int indexSelectionStart, string text). What this method does is insert textinto 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.

完成此操作的最佳方法是使用TextBox.Text.Insert(int indexSelectionStart, string text)。此方法的作用是在您指定的索引处将文本插入 TextBox - 它用作 TextBox.Text 是一个字符串,我们将在特定点插入文本。您希望在光标/选择器所在的位置插入文本,并找到该索引,我们可以使用TextBox.SelectionStartstring string.insert(int startIndex, string value)

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.

假设您的 TextBox 名为 textBox1。这就是代码的样子,假设您要插入的文本存储在名为strInsert的字符串中。

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

回答by user238450

This ensures that the cursor is at some position within the textbox, then inserts the text wherever the cursor is located.

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

        if (textBox1.CaretIndex <= 0)
        {

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

回答by Mike

The simple way would be

简单的方法是

textBox1.Paste();

That replaces the current selection with the contents of the clipboard.

这将用剪贴板的内容替换当前选择。

If you need to do it manually then it's a bit more work. Remember if you're "pasting" then you are "replacing" the current selection if there is one. So you need to handle that first. You'll need to save SelectionStart if you had a selection as removing the text will screw it up.

如果您需要手动执行此操作,则需要做更多的工作。请记住,如果您正在“粘贴”,那么您就是在“替换”当前选择(如果有)。所以你需要先处理它。如果您有选择,则需要保存 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;
}

After you're done you'll want to invalidate the control to force it to redraw.

完成后,您需要使控件无效以强制它重绘。

textBox1.Invalidate();

回答by bsara

A much easier way would be to use the Pastemethod:

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

  textbox1.Paste("text to insert");

I've done this using .NET 4.0

我已使用 .NET 4.0 完成此操作

回答by Jonathan Wood

I know this is late but the most efficient way appears to be:

我知道这已经很晚了,但最有效的方法似乎是:

textBox1.SelectedText = "Text";

回答by Asieh hojatoleslami

try this code:

试试这个代码:

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

回答by jv_

I realize this is an old post but i hope this collection of methods for TextBox will help others struggling with manipulating this control.

我意识到这是一篇旧帖子,但我希望 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);
    }
}