启用复制,剪切,过去的窗口,丰富的文本框 [英] Enable copy, cut, past window in a rich text box
本文介绍了启用复制,剪切,过去的窗口,丰富的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的节目丰富的文本框( richTextBox1
)为波纹管中。但是,当我右键点击它,它不会弹出一个复制,剪切,过去的窗口。你能告诉我怎样才能让这个复制,剪切,过去的窗口中我的格式文本框?我是新的C#,请让我知道一步一步,如果你知道如何解决这个
I have a rich text box(richTextBox1
) in my program as shown bellow. But when I right click on it, it doesn't pop up a "copy, cut, past" window. Can you please tell me how can I enable this "copy, cut, past" window in to my Rich Text Box? I am new to C#, please let me know step by step, if you know how to solve this
推荐答案
这段代码尝试
更新的代码:
UPDATED CODE:
private void richTextBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{ //click event
//MessageBox.Show("you got it!");
ContextMenu contextMenu = new System.Windows.Forms.ContextMenu();
MenuItem menuItem = new MenuItem("Cut");
menuItem.Click += new EventHandler(CutAction);
contextMenu.MenuItems.Add(menuItem);
menuItem = new MenuItem("Copy");
menuItem.Click += new EventHandler(CopyAction);
contextMenu.MenuItems.Add(menuItem);
menuItem = new MenuItem("Paste");
menuItem.Click += new EventHandler(PasteAction);
contextMenu.MenuItems.Add(menuItem);
richTextBox1.ContextMenu = contextMenu;
}
}
void CutAction(object sender, EventArgs e)
{
richTextBox1.Cut();
}
void CopyAction(object sender, EventArgs e)
{
Graphics objGraphics;
Clipboard.SetData(DataFormats.Rtf, richTextBox1.SelectedRtf);
Clipboard.Clear();
}
void PasteAction(object sender, EventArgs e)
{
if (Clipboard.ContainsText(TextDataFormat.Rtf))
{
richTextBox1.SelectedRtf
= Clipboard.GetData(DataFormats.Rtf).ToString();
}
}
的如果您要复制另一个粘贴应用程序如记事本(无伴奏)
请更换以下方法的
if you want to copy paste with another application like notepad (without styles )
please replace following methods
void CopyAction(object sender, EventArgs e)
{
Clipboard.SetText(richTextBox1.SelectedText);
}
void PasteAction(object sender, EventArgs e)
{
if (Clipboard.ContainsText())
{
richTextBox1.Text
+= Clipboard.GetText(TextDataFormat.Text).ToString();
}
}
这篇关于启用复制,剪切,过去的窗口,丰富的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文