【问题标题】:Insert text around Selection of TextBox control在 TextBox 控件的选择周围插入文本
【发布时间】:2021-01-07 15:17:57
【问题描述】:

我想在我的TextBox 控件的选择周围插入文本。

我试过了:

if (textBox1.SelectionLength > 0)
        {
            object userclipboard = new object();
            userclipboard = Clipboard.GetDataObject;
            textBox1.Copy();
            string textCopied = Clipboard.GetTextObject;
            string finalString = "text" + textCopied + "more text";
            textBox1.Paste();
            Clipboard.SetDataObject(userclipboard);
        }

但它不起作用。

我做错了什么?

【问题讨论】:

  • 你有没有调试过代码,看看发生了什么,出了什么问题?
  • 它在错误列表中。

标签: c# .net winforms textbox selection


【解决方案1】:

使用Clipboard.GetText() 粘贴剪贴板中的值,使用Clipboard.SetText(finalString); 复制值

string textCopied = Clipboard.GetText();
string finalString = "text" + textCopied + "more text";
Clipboard.SetText(finalString);
textBox1.Text = Clipboard.GetText();

如果您想使用 TextBox 属性从文本框中复制选择文本

string textCopied = textBox1.SelectedText; // To get the selected text from textbox1
string finalString = "text" + textCopied + "more text";
Clipboard.SetText(finalString);
textBox1.Paste();

textBox1.Copy(); 将复制您的选择文本,例如 Clipboard.SetText(finalString);

【讨论】:

    猜你喜欢
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 2017-07-11
    相关资源
    最近更新 更多