当前位置:编程学习 > C#/ASP.NET >>

今天写了一段代码,添加右键菜单。可是每点一次右键,就多出一个xianshi,我只想要一个xianshi,哪位大侠帮我解决一下,谢谢啦。急啊,今天就要交项目啊!!

private void gridReport_MouseDown(object Sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ToolStripMenuItem newItem = new ToolStripMenuItem();
                newItem.Text = "xianshi";
                newItem.Visible = true;
                RightMenum.Items.Add(newItem);
            }
           
        } --------------------编程问答-------------------- 是呀,你总是在添加,你应该先清除RightMenum.Items --------------------编程问答-------------------- private void gridReport_MouseDown(object Sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ToolStripMenuItem newItem = new ToolStripMenuItem();
                newItem.Text = "xianshi";
                newItem.Visible = true;
                RightMenum.Items.Add(newItem);
            }
           RightMenum.Items.Clear();
        }

清空一下 --------------------编程问答-------------------- 这样的话 右键就不管用了啊 亲。。 --------------------编程问答-------------------- 你在右键的时候让它出来一个菜单,类似与电脑桌面那样的,加一个添加按钮,点击添加按钮的时候,在给他赋值。别直接写在鼠标事件里,你这段代码是写在控件的鼠标单击事件里的,只要你单击鼠标右键,他就会执行这段代码 --------------------编程问答-------------------- private void gridReport_MouseDown(object Sender, MouseEventArgs e)
         {
             if (e.Button == MouseButtons.Right)
             {
                if(RightMenum.Items里有xianshi) 
                 {
                  return;
                 }
                 ToolStripMenuItem newItem = new ToolStripMenuItem();
                 newItem.Text = "xianshi";
                 newItem.Visible = true;
                 RightMenum.Items.Add(newItem);
             }
            
         }  --------------------编程问答--------------------
有了xianshi就不要再加了啊、 --------------------编程问答-------------------- 是用richTextBox1吗?

换成mousudown试试


 private void richTextBox1_MouseDown(object sender, MouseEventArgs e)
        {
            this.richTextBox1.ContextMenuStrip = contextMenuStrip1;
            contextMenuStrip1.Show();
        }
--------------------编程问答--------------------
引用 1 楼 bdmh 的回复:
是呀,你总是在添加,你应该先清除RightMenum.Items

++++ --------------------编程问答--------------------
引用 5 楼 5653325 的回复:
private void gridReport_MouseDown(object Sender, MouseEventArgs e)
         {
             if (e.Button == MouseButtons.Right)
             {
                if(RightMenum.Items里有xian……



你这   if(RightMenum.Items里有xian……
   怎么实现啊 ???? --------------------编程问答--------------------
引用 9 楼 w15020096260 的回复:
引用 5 楼 5653325 的回复:private void gridReport_MouseDown(object Sender, MouseEventArgs e)
         {
             if (e.Button == MouseButtons.Right)
             {
                if(Rig……

            if (e.Button == MouseButtons.Right)
            {
                RightMenum.Items.Clear();
                ToolStripMenuItem newItem = new ToolStripMenuItem();
                newItem.Text = "xianshi";
                newItem.Visible = true;
                RightMenum.Items.Add(newItem);
            }

先清除在添加。或者是先判断是否有这一项,有就不添加了。 --------------------编程问答--------------------
引用 10 楼 haukwong 的回复:
引用 9 楼 w15020096260 的回复:引用 5 楼 5653325 的回复:private void gridReport_MouseDown(object Sender, MouseEventArgs e)
         {
             if (e.Button == MouseButtons.Right)
             {……



怎么判断啊 ,,新手 求指点啊 。。。 --------------------编程问答--------------------             if (e.Button == MouseButtons.Right)
            {
               //两种方式都可以
                //contextMenuStrip1.Items.RemoveByKey("xianshi");
                if (contextMenuStrip1.Items.ContainsKey("xianshi"))
                {
                    return;
                }
                ToolStripMenuItem newItem = new ToolStripMenuItem();
                newItem.Text = "xianshi";
                newItem.Visible = true;
                newItem.Name = "xianshi";
                contextMenuStrip1.Items.Add(newItem);
            } --------------------编程问答-------------------- 先RightMenum.Items.Clear() --------------------编程问答--------------------   if (contextMenuStrip1.Items.ContainsKey("xianshi"))
                {
                    return;
                } --------------------编程问答--------------------
清理你的 items

\

推荐你用这个 contextMenuStrip1;

这样你的 mouseDown 里的鼠标判断都可以省略了 --------------------编程问答-------------------- 亲,给你的菜单价格name,判断name存在就不要加了,亲。 --------------------编程问答--------------------
引用 14 楼 ysj1163620987 的回复:
if (contextMenuStrip1.Items.ContainsKey("xianshi"))
                {
                    return;
                }


+1 --------------------编程问答-------------------- 来看看。学习一下啊 --------------------编程问答-------------------- 判断是否有这个,然后再添加。 --------------------编程问答--------------------             if (e.Button1 == MouseButtons.Right)
            {


                if (contextMenuStrip1.Items.ContainsKey("xianshi"))
                {
                    return;
                }
                ToolStripMenuItem newItem = new ToolStripMenuItem();
                newItem.Text = "xianshi";
                newItem.Visible = true;
                newItem.Name = "xianshi";
                contextMenuStrip1.Items.Add(newItem);
            }












<p><a href="http://www.answersfull.com/other/list/?1.html">C#问题--更多来自</a></p>
<p><a href="http://www.answersfull.com/more01/list/?1.html">C#问题--更多来自</a></p>
<p><a href="http://www.answersfull.com/more02/list/?1.html">C#问题--更多来自</a></p>
<p><a href="http://www.answersfull.com/more03/list/?1.html">C#问题--更多来自</a></p>
<p><a href="http://www.answersfull.com/more04/list/?1.html">C#问题--更多来自</a></p>
<p><a href="http://www.answersfull.com/more05/list/?1.html">C#问题--更多来自</a></p>
<p><a href="http://www.answersfull.com/more06/list/?1.html">C#问题--更多来自</a></p>
<p><a href="http://www.answersfull.com/more07/list/?1.html">C#问题--更多来自</a></p> --------------------编程问答-------------------- 菜单项固定不好吗,非得要在右键的时候添加 --------------------编程问答--------------------
引用 楼主 w15020096260 的回复:
private void gridReport_MouseDown(object Sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ToolStripMenuItem newItem = new ToolStrip……

单例模式 --------------------编程问答-------------------- 你可以做个判断、  第一次 就添加   过后就跳过不添加
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,