Winform 自定义控件的右键菜单的问题
有一个自定义类型,继承与component ,在类里面定义一个ContextMenu 的属性,在窗体的Form_load 函数中给 自定义控件的ContextMenu 赋值,运行窗体后右键没有出来?? --------------------编程问答-------------------- component 类本来就没有ContextMenu属性,自己定义?
给下代码? --------------------编程问答-------------------- 要在你自定义控件里自己捕捉右键,然后弹出菜单吧
--------------------编程问答-------------------- 同意二楼的说法 --------------------编程问答-------------------- 用的一个第三方控件,想修改下,但是没成功
#region OutlookBar Item class
[ToolboxItem(false)]
public class OutlookBarItem : Component
{
#region Class variables
string text;
int imageIndex = -1;
object tag;
private ContextMenuStrip _contextMenu;
public ContextMenuStrip contextMenu
{
get { return _contextMenu; }
set { _contextMenu = value; }
}
#endregion
#region Events
#endregion
#region Constructors
public OutlookBarItem(string text, int ImageIndex)
{
this.text = text;
this.imageIndex = ImageIndex;
}
public OutlookBarItem(string text, int imageIndex, object tag)
{
this.text = text;
this.imageIndex = imageIndex;
tag = tag;
}
public OutlookBarItem()
{
// To support designer
imageIndex = -1;
}
#endregion
#region Overrides
#endregion
#region Methods
#endregion
#region Properties
public int ImageIndex
{
set { imageIndex = value; }
get { return imageIndex; }
}
public string Text
{
set { text = value; }
get { return text; }
}
public object Tag
{
set { tag = value; }
get { return tag; }
}
#endregion
#region Implementation
#endregion
}
#endregion
窗体里面的代码
OutlookBarBand outlookShortcutsBand = new OutlookBarBand("权限管理");
outlookShortcutsBand.ContextMenu = null;
outlookShortcutsBand.SmallImageList = this.imageList1;
outlookShortcutsBand.LargeImageList = this.imageList1;
outlookShortcutsBand.Items.Add(new OutlookBarItem("用户管理", 0));
outlookShortcutsBand.Items.Add(new OutlookBarItem("机构管理", 1));
outlookShortcutsBand.Items.Add(new OutlookBarItem("角色管理", 2));
MenuItem menuItem1 = new MenuItem("测试一");
MenuItem menuItem2 = new MenuItem("测试二");
MenuItem menuItem3 = new MenuItem("测试三");
menuItem1.Click += new EventHandler(menuItem1_click);
menuItem2.Click += new EventHandler(menuItem2_click);
menuItem3.Click += new EventHandler(menuItem3_click);
outlookShortcutsBand.Items[0].ContextMenu = new ContextMenu(new MenuItem[] { menuItem1, menuItem2, menuItem3 });
outlookShortcutsBand.Background = SystemColors.AppWorkspace;
outlookShortcutsBand.TextColor = Color.White;
outlookBar1.Bands.Add(outlookShortcutsBand);
--------------------编程问答-------------------- 控件里面的代码有点问题
是这个
--------------------编程问答-------------------- 第三方控件的源代码在这里可以下载
#region OutlookBar Item class
[ToolboxItem(false)]
public class OutlookBarItem : Component
{
#region Class variables
string text;
int imageIndex = -1;
object tag;
private ContextMenu _contextMenu;
public ContextMenu contextMenu
{
get { return _contextMenu; }
set { _contextMenu = value; }
}
#endregion
#region Events
#endregion
#region Constructors
public OutlookBarItem(string text, int ImageIndex)
{
this.text = text;
this.imageIndex = ImageIndex;
}
public OutlookBarItem(string text, int imageIndex, object tag)
{
this.text = text;
this.imageIndex = imageIndex;
tag = tag;
}
public OutlookBarItem()
{
// To support designer
imageIndex = -1;
}
#endregion
#region Overrides
#endregion
#region Methods
#endregion
#region Properties
public int ImageIndex
{
set { imageIndex = value; }
get { return imageIndex; }
}
public string Text
{
set { text = value; }
get { return text; }
}
public object Tag
{
set { tag = value; }
get { return tag; }
}
#endregion
#region Implementation
#endregion
}
#endregion
http://www.cnblogs.com/sousou/
那个哥们修改好了告诉一声,谢谢 --------------------编程问答--------------------
你拖个contextMenuStrip1,加几个项,然后写如下代码,我测试了是可以的
--------------------编程问答--------------------
OutlookBarItem obi = new OutlookBarItem();//定义自定义类的对象
obi.contextMenu = this.contextMenuStrip1; //设置其值为contextMenuStrip1;
//obi.contextMenu.Show(); //可以直接show出来
this.ContextMenuStrip = obi.contextMenu;//也可以设置为ContextMenuStrip ,点右键show出来
这里不行哦,您把那个第三方控件下下来看看。想定义子项的右键菜单 --------------------编程问答-------------------- 自定义控件
补充:.NET技术 , C#