在RichTextBox中,右键菜单不能用啊,像记事本里选中文本的时候,单击右键的时候会有粘贴复制等项的嘛,这个要如何打开?

解决方案 »

  1.   

    定义事件 
    private void OnMouseDown(object sender, MouseEventArgs e)
    {
       if(e.Button == MouseButtons.Right)
         {
             //添加菜单弹出代码.................
         }
    }
      

  2.   

    能不能写全一点?我是菜鸟,看不懂啊.
    if(e.Button == MouseButtons.Right)
         {
             //添加菜单弹出代码.................
         }这里也不知道怎么写.
      

  3.   

    用contextmenu控件关联到你的RichTextBox。先设置好一个contextmenu,然后设置RichTextBox.contextmenu就行了!!!
      

  4.   

    设置关联菜单绑定到RichTextBox,然后在Popup好象是这个名这个事件中,判断RichTextBox.Text是否为空不为空同使菜单可用即将Enabled=true,否则为false.
      

  5.   

    添加contextmenu控件关联到RichTextBox,然后
    设置好contextmenu,OK搞定
      

  6.   

    contextmenu控件在哪?怎么没看过。
      

  7.   

    我给你传个例子, [email protected]
      

  8.   

    还是不太明白啊。现在我添加了一个contextmenu了,命名为:contextmenu
    我在RichTextBox的contextmenu中选择了contextmenu,接下来,我该怎么做??
      

  9.   

    你先选中contextmenu,在里面设置几个菜单,然后设置了RichTextBox的contextmenu后就可以直接在richtextbox右键就行了
      

  10.   

    我选中contextmenu,在属性下面有个编程菜单,可是点击的时候没有反应啊,我要在contextmenu中设置菜单啊。
      

  11.   

    this.contextMenu1 = new System.Windows.Forms.ContextMenu();
    this.richTextBox1 = new System.Windows.Forms.RichTextBox();
    this.SuspendLayout();
    // 
    // richTextBox1
    // 
    this.richTextBox1.Location = new System.Drawing.Point(272, 240);
    this.richTextBox1.Name = "richTextBox1";
    this.richTextBox1.TabIndex = 0;
    this.richTextBox1.Text = "richTextBox1";
    this.richTextBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.richTextBox1_MouseDown);
    private void richTextBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if(e.Button == MouseButtons.Right)
    {
    this.richTextBox1.ContextMenu = this.contextMenu1;
    this.contextMenu1.MenuItems.Add("file");
    }
    }
      

  12.   

    this.contextMenu1 = new System.Windows.Forms.ContextMenu();
    this.richTextBox1 = new System.Windows.Forms.RichTextBox();
    this.menuItem1 = new System.Windows.Forms.MenuItem();
    this.menuItem2 = new System.Windows.Forms.MenuItem();
    this.SuspendLayout();
    // 
    // contextMenu1
    // 
    this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
     this.menuItem1,
     this.menuItem2});
    // 
    // richTextBox1
    // 
    this.richTextBox1.Location = new System.Drawing.Point(272, 240);
    this.richTextBox1.Name = "richTextBox1";
    this.richTextBox1.TabIndex = 0;
    this.richTextBox1.Text = "richTextBox1";
    this.richTextBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.richTextBox1_MouseDown);
    // 
    // menuItem1
    // 
    this.menuItem1.Index = 0;
    this.menuItem1.Text = "复制";
    this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
    // 
    // menuItem2
    // 
    this.menuItem2.Index = 1;
    this.menuItem2.Text = "粘贴";
    this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);private void richTextBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if(e.Button == MouseButtons.Right)
    {
    this.richTextBox1.ContextMenu = this.contextMenu1;
    }
    } private void menuItem1_Click(object sender, System.EventArgs e)
    {
    SendKeys.Send("^C");
    } private void menuItem2_Click(object sender, System.EventArgs e)
    {
    SendKeys.Send("^V");
    }楼主现在可以结帖了吧。
      

  13.   

    还是看不太懂啊,这段代码对于你们可能很简单,可是对于我这菜鸟,感觉难哦。比如放哪我都不知道,不过很感谢  paul8765(且行且珍惜) ,结帐少不了你的。
    麻烦先说一下思路,行不??再说详细一点行不?
      

  14.   

    控件里面有右键菜单你把它编辑好后再把RichTextBox的ContexMenu指定为那个就可以了
      

  15.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace richTextTest
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.RichTextBox richTextBox1;
    private System.Windows.Forms.MenuItem menuCopy;
    private System.Windows.Forms.MenuItem menuCut;
    private System.Windows.Forms.MenuItem menuPaste;
    private System.Windows.Forms.MenuItem menuCancel;
    private System.Windows.Forms.ContextMenu popUp;

    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.richTextBox1 = new System.Windows.Forms.RichTextBox();
    this.popUp = new System.Windows.Forms.ContextMenu();
    this.menuCopy = new System.Windows.Forms.MenuItem();
    this.menuCut = new System.Windows.Forms.MenuItem();
    this.menuPaste = new System.Windows.Forms.MenuItem();
    this.menuCancel = new System.Windows.Forms.MenuItem();
    this.SuspendLayout();
    // 
    // richTextBox1
    // 
    this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
    this.richTextBox1.Location = new System.Drawing.Point(0, 0);
    this.richTextBox1.Name = "richTextBox1";
    this.richTextBox1.RightToLeft = System.Windows.Forms.RightToLeft.No;
    this.richTextBox1.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.Vertical;
    this.richTextBox1.Size = new System.Drawing.Size(648, 430);
    this.richTextBox1.TabIndex = 0;
    this.richTextBox1.Text = "richTextBox1";
    this.richTextBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.richTextBox1_MouseUp);
    // 
    // popUp
    // 
    this.popUp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
      this.menuCopy,
      this.menuCut,
      this.menuPaste,
      this.menuCancel});
    // 
    // menuCopy
    // 
    this.menuCopy.Index = 0;
    this.menuCopy.Text = "拷贝";
    this.menuCopy.Click += new System.EventHandler(this.menuItem1_Click);
    // 
    // menuCut
    // 
    this.menuCut.Index = 1;
    this.menuCut.Text = "剪切";
    this.menuCut.Click += new System.EventHandler(this.menuItem2_Click_1);
    // 
    // menuPaste
    // 
    this.menuPaste.Index = 2;
    this.menuPaste.Text = "粘贴";
    this.menuPaste.Click += new System.EventHandler(this.menuPaste_Click);
    // 
    // menuCancel
    // 
    this.menuCancel.Index = 3;
    this.menuCancel.Text = "撤销";
    this.menuCancel.Click += new System.EventHandler(this.menuCancel_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(648, 430);
    this.Controls.Add(this.richTextBox1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    }
    private void richTextBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if (e.Button==MouseButtons.Right)
    {
    this.popUp.Show(this.richTextBox1,new Point(e.X,e.Y));
    }
    } private void menuItem1_Click(object sender, System.EventArgs e)
    {
    if (this.richTextBox1.SelectionLength>0)
    this.richTextBox1.Copy();
    } private void menuItem2_Click_1(object sender, System.EventArgs e)
    {
    if (this.richTextBox1.SelectedText!="")
    this.richTextBox1.Cut(); } private void menuPaste_Click(object sender, System.EventArgs e)
    {
    if(Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)
    {
    if (this.richTextBox1.SelectionLength > 0)
    {
    this.richTextBox1.SelectionStart=this.richTextBox1.SelectionStart + this.richTextBox1.SelectionLength;
    }
    this.richTextBox1.Paste();
    }
    } private void menuCancel_Click(object sender, System.EventArgs e)
    {
    if(this.richTextBox1.CanUndo==true)
    {
    this.richTextBox1.Undo();
    this.richTextBox1.ClearUndo();
    }
    } }
    }