ContextMenuStrip 的不稳定行为
我从 ContextMenuStip 中得到一些不稳定的行为:
private void lstModules_MouseMove(object sender , MouseEventArgs e)
{ mouse = e.Location; }
private void lstModules_MouseDown(object sender , MouseEventArgs e)
{
ListViewItem item = null;
if((hitTest = lstModules.HitTest(mouse)) != null)
item = hitTest.Item;
switch (e.Button)
{
case MouseButtons.Right:
if (item != null)
{
// valid item selection
ShowModuleDetails(item.Name);
lstModules.ContextMenuStrip = mnuContext_Module;
}
else
{
// right-click - no item selection
lblModuleDetails.Text = string.Empty;
lstModules.ContextMenuStrip = mnuContext_Desktop;
}
lstModules.ContextMenuStrip.Show(lstModules , mouse);
break;
case MouseButtons.Left:
if (item != null)
{ ShowModuleDetails(item.Name); }
break;
}
}
private void ShowModuleDetails(string modName)
{
// get module details from dictionary
lblModuleDetails.Text = Modules[modName].Details;
}
- 显示上下文菜单时,列表视图中的项目未正确选择。换句话说,当选择该项目时,详细字符串值将显示在标签控件中。
- 如果上下文菜单可见,并且选择了某个项目,则该项目的详细信息不会更改。
- 上下文菜单位置短暂出现在旧鼠标位置,然后移动到新鼠标位置。
我的上下文菜单有什么问题吗?
I am getting some erratic behavior from a ContextMenuStip
:
private void lstModules_MouseMove(object sender , MouseEventArgs e)
{ mouse = e.Location; }
private void lstModules_MouseDown(object sender , MouseEventArgs e)
{
ListViewItem item = null;
if((hitTest = lstModules.HitTest(mouse)) != null)
item = hitTest.Item;
switch (e.Button)
{
case MouseButtons.Right:
if (item != null)
{
// valid item selection
ShowModuleDetails(item.Name);
lstModules.ContextMenuStrip = mnuContext_Module;
}
else
{
// right-click - no item selection
lblModuleDetails.Text = string.Empty;
lstModules.ContextMenuStrip = mnuContext_Desktop;
}
lstModules.ContextMenuStrip.Show(lstModules , mouse);
break;
case MouseButtons.Left:
if (item != null)
{ ShowModuleDetails(item.Name); }
break;
}
}
private void ShowModuleDetails(string modName)
{
// get module details from dictionary
lblModuleDetails.Text = Modules[modName].Details;
}
- The item in the list view is not properly selected when the context menu is showing. In other words, when the item is selected, a detail string value is displayed in a label control.
- If a context menu is visible, and an item is selected, the item details do not change.
- Context menu location briefly appears at the old mouse location then moves to the new mouse location.
Is there something I'm doing wrong with the context menus?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我尝试尽可能重现您的问题。我想我可以帮助您解决您列出的三个问题中的至少两个。
当通过
ListView.ItemSelectionChanged
选择项目时,您会收到通知> 活动:我相信您尝试做的事情太多了。让框架处理您通过 ListView.ContextMenuStrip 属性指定的上下文菜单的显示。您遇到的效果是由您手动调用 ContextMenuStrip.Show(...) 引起的,这导致框架显示上下文菜单,然后您再次执行相同的操作,在另一个位置。
因此,尽量不要调用该函数;上下文菜单仍应出现。
顺便说一句,如果可行,您还可以删除
lstModules_MouseMove
事件处理程序和mouse
位置对象。I tried to reproduce your problem as far as I could. I think I can help you out with at least two of the three issues you've listed.
You can be notified when an item has been selected via the
ListView.ItemSelectionChanged
event:I believe you try to do too much. Let the framework handle the displaying of the context menu which you have specified via the
ListView.ContextMenuStrip
property. The effect you experience is caused by your manually callingContextMenuStrip.Show(...)
, which results in the displaying of the context menu by the framework, and then you doing the same thing a second time, at another location.Therefore, try not to call this function; the context menu should still appear.
Btw., if that works, you can also get rid of your
lstModules_MouseMove
event handler and themouse
location object.