contextmenustrip 怎么获取子菜单,怎么返回上一层

lzj047111 2012-09-28 10:57:41
contextmenustrip 怎么获取子菜单,怎么返回上一层
...全文
310 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
lzj047111 2012-09-29
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 的回复:]

ToolStripItem oItem = childItem.OwnerItem.OwnerItem;
if (oItem != null){
((ToolStripMenuItem)oItem).DropDownItems //得到父级同级的集合
}
else{
}



知道么,null是不能转化为任何类型的
[/Quote]
我在加到三层,然后根据你的代码调试一下吧
lzj047111 2012-09-29
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 的回复:]

ToolStripItem oItem = childItem.OwnerItem.OwnerItem;
if (oItem != null){
((ToolStripMenuItem)oItem).DropDownItems //得到父级同级的集合
}
else{
}



知道么,null是不能转化为任何类型的
[/Quote]
childItem.OwnerItem -- 这个已经获取到第一层菜单了吧。没错吧。
childItem.OwnerItem.OwnerItem -- 在加一个OwnerItem,那么是不是应该为NULL。
而childItem.OwnerItem,已经获取到第一层,为什么还需要加OwnerItem呢?
ggewt2td 2012-09-29
  • 打赏
  • 举报
回复
ToolStripItem oItem = childItem.OwnerItem.OwnerItem;
if (oItem != null){
((ToolStripMenuItem)oItem).DropDownItems //得到父级同级的集合
}
else{
}



知道么,null是不能转化为任何类型的
ggewt2td 2012-09-29
  • 打赏
  • 举报
回复
真是服了你了,是Null是因为是最顶层,你先要判断是不是null,
不是null才强制转化啊,

大哥。
lzj047111 2012-09-29
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 的回复:]

强制转化一下不就行了,
[/Quote]
强制转化成ToolStripMenuItem过。提示Null
ggewt2td 2012-09-29
  • 打赏
  • 举报
回复
强制转化一下不就行了,
lzj047111 2012-09-29
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 的回复:]

ToolStripMenuItem oItem = childItem.OwnerItem.OwnerItem;
if (oItem != null){
oItem.DropDownItems //得到父级同级的集合
}
else{
[菜单容器].Items //得到最顶层集合
[/Quote]
ToolStripMenuItem oItem = childItem.OwnerItem.OwnerItem; // 这里返回的对象是ToolStripItem,而不是
ToolStripMenuItem哦。
ggewt2td 2012-09-29
  • 打赏
  • 举报
回复
ToolStripMenuItem oItem = childItem.OwnerItem.OwnerItem;
if (oItem != null){
oItem.DropDownItems //得到父级同级的集合
}
else{
[菜单容器].Items //得到最顶层集合
ggewt2td 2012-09-29
  • 打赏
  • 举报
回复
错了,应该是

childItem.OwnerItem.OwnerItem.DropDownItems
ggewt2td 2012-09-29
  • 打赏
  • 举报
回复
childItem.OwnerItem.DropDownItems 这个就可以得到 A_1/A_2/A_3 集合啊
按索引遍历
ggewt2td 2012-09-29
  • 打赏
  • 举报
回复
ToolStripItem oItem = childItem.OwnerItem;
if (oItem != null){
((ToolStripMenuItem)oItem).DropDownItems //得到父级同级的集合
}
else{
}


知道那个意思就行了,你自己去弄吧,
不要照抄我的代码,我的代码随手写的,
lzj047111 2012-09-28
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 的回复:]

childItem.OwnerItem.OwnerItem

再继续套啊,

如果父级为空,那就直接用菜单容器来套
[/Quote]
是这样的。
假如:
我的菜单是这样
第一层菜单有如下项:
A_1\A_2\A_3(共有三个项)
当我点击A_1时。
进入到A_1的子菜单。
A_1_1\A_1_2\A_1_3(共有三个项)

现在,我是处于A_1的子菜单,我通过一个按钮,点击一下,就可以返回到第一层菜单。
我通过你的childItem.OwnerItem。的确是返回A_1.但是,我还想要拿到A_2\A_3?
ggewt2td 2012-09-28
  • 打赏
  • 举报
回复
childItem.OwnerItem.OwnerItem

再继续套啊,

如果父级为空,那就直接用菜单容器来套
lzj047111 2012-09-28
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]

childItem.OwerItem.DropDownItems
[/Quote]
这个是获取childItem.OwerItem下面的子菜单啊。
我的意思是说,已经获取到父级childItem.OwnerItem;
但是,与父级同级的项怎么获取??
ggewt2td 2012-09-28
  • 打赏
  • 举报
回复
晕,你稍微动下脑筋就明白了,
ggewt2td 2012-09-28
  • 打赏
  • 举报
回复
childItem.OwerItem.DropDownItems
lzj047111 2012-09-28
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

ToolStripMenuItem TMenuItem = this.contextmenustrip1.Items[0] as ToolStripMenuItem;

ToolStripMenuItem childItem = TMenuItem.DropDownItems[0]; //得到第二层菜单
childItem.OwerItem; //得到第二层菜单的父菜单(第一层的)
[/Quote]
childItem.OwnerItem; 这个获取到它的父级了。
怎么获取同级的所有项呢???
lzj047111 2012-09-28
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

ToolStripMenuItem TMenuItem = this.contextmenustrip1.Items[0] as ToolStripMenuItem;

ToolStripMenuItem childItem = TMenuItem.DropDownItems[0]; //得到第二层菜单
childItem.OwerItem; //得到第二层菜单的父菜单(第一层的)
[/Quote]
ToolStripMenuItem childItem = TMenuItem.DropDownItems[0]; //得到第二层菜单
这样写,有错误。
foreach (ToolStripMenuItem item in childItem.DropDownItems)
{

}
// 这样就没问题
ggewt2td 2012-09-28
  • 打赏
  • 举报
回复
ToolStripMenuItem TMenuItem = this.contextmenustrip1.Items[0] as ToolStripMenuItem;

ToolStripMenuItem childItem = TMenuItem.DropDownItems[0]; //得到第二层菜单
childItem.OwerItem; //得到第二层菜单的父菜单(第一层的)
ggewt2td 2012-09-28
  • 打赏
  • 举报
回复
TMenuItem.OwnerItem 就可以得到第二层的第一层菜单啊
加载更多回复(8)

110,549

社区成员

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

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

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