contextMenu的一个问题, this.ContextMenu=null;this.ContextMenu=ss; 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 一般的做法是只要有菜单就弹出来,这样告诉客户有这个功能,只是有些菜单项 在某些条件下才可以用 你可以在 菜单的 PopUP事件里面来 根据条件 判断 菜单项的可用性 或者你就如楼上所说 来动态添加和移出 ContextMenu 以下是哦程序中ListBox的弹出菜单//创建快捷菜单private void listBox1_MouseUp(object sender, MouseEventArgs e){ if (e.Button == MouseButtons.Right) { ContextMenu listContextMenu = new ContextMenu(); listContextMenu.MenuItems.Add("播放(&P)",new EventHandler(this.menuPlay_Click)); listContextMenu.MenuItems.Add("暂停(&C)",new EventHandler(this.menuPause_Click)); listContextMenu.MenuItems.Add("停止(&S)",new EventHandler(this.menuStop_Click)); listContextMenu.MenuItems.Add("-"); listContextMenu.MenuItems.Add("全屏 <Alt-Enter>",new EventHandler(this.menuFull_Click)); listContextMenu.MenuItems.Add("-"); listContextMenu.MenuItems.Add("删除选定列表项 <Del>",new EventHandler(this.menuDelSelect_Click)); listContextMenu.MenuItems.Add("删除所有列表项(&D)",new EventHandler(this.menuDelAll_Click)); listContextMenu.MenuItems[0].Enabled = menuPlay.Enabled; listContextMenu.MenuItems[1].Enabled = menuPause.Enabled; listContextMenu.MenuItems[2].Enabled = menuStop.Enabled; listContextMenu.MenuItems[4].Enabled = menuFull.Enabled; listContextMenu.MenuItems[6].Enabled = menuDelSelect.Enabled; listContextMenu.Show(this.listBox1,new Point(e.X ,e.Y)); }} 用两个ContextMenu,一个是你要用的,里面有Item;另一个是没有Item的。满足条件就用第一个,不满足就用第二个!using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace WindowsApplication2{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.ContextMenu contextMenu1; private System.Windows.Forms.ContextMenu contextMenu2; private System.Windows.Forms.MenuItem menuItem1; private System.Windows.Forms.MenuItem menuItem2; /// <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 Form Designer generated code /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.contextMenu1 = new System.Windows.Forms.ContextMenu(); this.contextMenu2 = new System.Windows.Forms.ContextMenu(); this.menuItem1 = new System.Windows.Forms.MenuItem(); this.menuItem2 = new System.Windows.Forms.MenuItem(); // // contextMenu1 // this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem1, this.menuItem2}); // // menuItem1 // this.menuItem1.Index = 0; this.menuItem1.Text = "11111"; // // menuItem2 // this.menuItem2.Index = 1; this.menuItem2.Text = "11111"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 273); this.ContextMenu = this.contextMenu1; this.Name = "Form1"; this.Text = "Form1"; this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { if(e.X < 150 && e.Y <150) { this.ContextMenu = contextMenu1; } else { this.ContextMenu = contextMenu2; } } }} iframe空引用 求一正则表达式 C#中 变量 数组 集合能够批量产生么? Remoting相关问题 请教大家:欲将XML文件的标签更换该如何实现? 怎样加入多个版本?具体怎么做? datagrid怎么没有对列操作的属性? winform中xml文件的读取问题 .net里的并行任务Task的生命周期是多少? 怎样由dll文件反编译成源码,用ildasm好像不行。 什么是".net"技术? 为什么 s = "00000000"; s[5] = '1'; 无法通过编译? 我想改变某一个字符的值, 正确的写法是什么?
在某些条件下才可以用
你可以在 菜单的 PopUP事件里面来 根据条件 判断 菜单项的可用性
或者你就如楼上所说 来动态添加和移出 ContextMenu
private void listBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ContextMenu listContextMenu = new ContextMenu();
listContextMenu.MenuItems.Add("播放(&P)",new EventHandler(this.menuPlay_Click));
listContextMenu.MenuItems.Add("暂停(&C)",new EventHandler(this.menuPause_Click));
listContextMenu.MenuItems.Add("停止(&S)",new EventHandler(this.menuStop_Click));
listContextMenu.MenuItems.Add("-");
listContextMenu.MenuItems.Add("全屏 <Alt-Enter>",new EventHandler(this.menuFull_Click));
listContextMenu.MenuItems.Add("-");
listContextMenu.MenuItems.Add("删除选定列表项 <Del>",new EventHandler(this.menuDelSelect_Click));
listContextMenu.MenuItems.Add("删除所有列表项(&D)",new EventHandler(this.menuDelAll_Click));
listContextMenu.MenuItems[0].Enabled = menuPlay.Enabled;
listContextMenu.MenuItems[1].Enabled = menuPause.Enabled;
listContextMenu.MenuItems[2].Enabled = menuStop.Enabled;
listContextMenu.MenuItems[4].Enabled = menuFull.Enabled;
listContextMenu.MenuItems[6].Enabled = menuDelSelect.Enabled;
listContextMenu.Show(this.listBox1,new Point(e.X ,e.Y));
}
}
满足条件就用第一个,不满足就用第二个!using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.ContextMenu contextMenu2;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
/// <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 Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.contextMenu2 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "11111";
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "11111";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.ContextMenu = this.contextMenu1;
this.Name = "Form1";
this.Text = "Form1";
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.X < 150 && e.Y <150)
{
this.ContextMenu = contextMenu1;
}
else
{
this.ContextMenu = contextMenu2;
}
}
}
}