我在WinForms中有一个richtextbox,并创建了复制和粘贴功能,并且可以在光标处复制和粘贴。但是,一旦粘贴,我的光标就会移动到richtextbox的开头。如何使其停留在该位置或移至粘贴部分的末尾?
我尝试过
Point p = new Point(Cursor.Position.X, Cursor.Position.Y);
rtbNotePad.PointToClient(p); //but does not work.
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
rtbNotePad.Copy();
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
string pasteText = Clipboard.GetText(TextDataFormat.Text).ToString();
Point p = new Point(Cursor.Position.X, Cursor.Position.Y);
if (Clipboard.ContainsText())
{
rtbNotePad.Text = rtbNotePad.Text.Insert(rtbNotePad.SelectionStart, Clipboard.GetText(TextDataFormat.Text).ToString());
rtbNotePad.PointToClient(p);
}
}
答案 0 :(得分:0)
您应该使用SelectionStart
属性来控制ReachtextBox
中的光标位置。
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
string pasteText = Clipboard.GetText(TextDataFormat.Text).ToString();
if (Clipboard.ContainsText())
{
var start = rtbNotePad.SelectionStart; // use this if you want to keep cursor where it was
//start += pasteText.Length; // use this if want to move cursor to the end of pasted text
rtbNotePad.Text = rtbNotePad.Text.Insert(rtbNotePad.SelectionStart, Clipboard.GetText(TextDataFormat.Text).ToString());
rtbNotePad.SelectionStart = start;
// rtbNotePad.Focus();
}
}