Winform 自定义控件的右键菜单的问题

csfufish 2009-12-02 08:21:04
有一个自定义类型,继承与component ,在类里面定义一个ContextMenu 的属性,
在窗体的Form_load 函数中给 自定义控件的ContextMenu 赋值,运行窗体后右键没有出来??
...全文
426 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2009-12-02
  • 打赏
  • 举报
回复
csfufish 2009-12-02
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 sugar_tiger 的回复:]
你拖个contextMenuStrip1,加几个项,然后写如下代码,我测试了是可以的
C# code
OutlookBarItem obi=new OutlookBarItem();//定义自定义类的对象 obi.contextMenu=this.contextMenuStrip1;//设置其值为contextMenuStrip1;//obi.contextMenu.Show();//可以直接show出来this.ContextMenuStrip= obi.contextMenu;//也可以设置为ContextMenuStrip ,点右键show出来
[/Quote]

这里不行哦,您把那个第三方控件下下来看看。想定义子项的右键菜单
Sugar_Tiger 2009-12-02
  • 打赏
  • 举报
回复

你拖个contextMenuStrip1,加几个项,然后写如下代码,我测试了是可以的

OutlookBarItem obi = new OutlookBarItem();//定义自定义类的对象
obi.contextMenu = this.contextMenuStrip1; //设置其值为contextMenuStrip1;
//obi.contextMenu.Show(); //可以直接show出来
this.ContextMenuStrip = obi.contextMenu;//也可以设置为ContextMenuStrip ,点右键show出来
csfufish 2009-12-02
  • 打赏
  • 举报
回复
第三方控件的源代码在这里可以下载
http://www.cnblogs.com/sousou/
那个哥们修改好了告诉一声,谢谢
csfufish 2009-12-02
  • 打赏
  • 举报
回复
控件里面的代码有点问题
是这个

#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
csfufish 2009-12-02
  • 打赏
  • 举报
回复
用的一个第三方控件,想修改下,但是没成功

#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);

jbo126 2009-12-02
  • 打赏
  • 举报
回复
同意二楼的说法
特别 2009-12-02
  • 打赏
  • 举报
回复
要在你自定义控件里自己捕捉右键,然后弹出菜单吧
Sugar_Tiger 2009-12-02
  • 打赏
  • 举报
回复
component 类本来就没有ContextMenu属性,自己定义?
给下代码?

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧