我有一個基本的文本編輯器應用程式,我的目標是添加一個功能,用戶可以單擊按鈕并在游標所在的位置添加預制文本。
我目前有這個(使用我在網上找到的一些代碼)
richTextBox1.CaretPosition.InsertTextInRun(s);
我打算讓字串 s 成為要添加的字串。
但是,System.Windows.Forms 中的 RichTextBox 不包含 CaretPosition。我發現 2010 年的一篇文章建議您使用 System.Windows.Control,但是,在 .Net Core 中不再可以訪問它,它取決于演示框架。
那么,有什么方法可以在.net core 中實作我的目標(在滑鼠游標后插入一個字串,在富文本框中)?
uj5u.com熱心網友回復:
更新2:
使用如下代碼完全滿足text文本環境的richtextbox:
richTextBox1.Select(richTextBox1.SelectionStart, richTextBox1.TextLength);//Select everything after the cursor
string tmp = richTextBox1.SelectedText;//Copy them
richTextBox1.SelectedText = "";//Set to null
richTextBox1.AppendText("Hello world" tmp);//Add the target string and add the original text
更新1:
// Determine if there is any text in the Clipboard to paste into the text box.
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)
{
IDataObject dataObject = Clipboard.GetDataObject();
Clipboard.SetDataObject("Hello World");
richTextBox1.Paste();
Clipboard.SetDataObject(dataObject);
}
else
{
Clipboard.SetDataObject("Hello World");
richTextBox1.Paste();
}
此操作會保留原始粘貼板的內容。
僅當剪貼板是資料時才有效。我會在考慮之后繼續更新。
原文:只需要使用剪貼板和
根據您的需要自行更改。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/483106.html
上一篇:將串列框保存到SQL資料庫,“程序或函式Music_Add指定了太多引數”
下一篇:讀取檔案行時忽略空格c#