繁体   English   中英

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

[英]How to paste text in textbox current cursor?

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

不是 textbox1 += string

一种更简单的方法是使用Paste方法:

  textbox1.Paste("text to insert");

我用.NET 4.0完成了这个

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");

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

textBox1.SelectedText = "Text";

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

假设您的TextBox名为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();

试试这段代码:

 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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM