c# - ContextMenuStrip 的不稳定行为

标签 c# .net winforms 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;
 }
  1. 显示上下文菜单时未正确选择 ListView 中的项目。换句话说,当项目被选中时,详细信息字符串值显示在标签控件中。
  2. 如果上下文菜单可见,并且选择了一个项目,则项目详细信息不会更改。
  3. 上下文菜单位置短暂出现在鼠标位置,然后移动到鼠标位置。

上下文菜单有什么问题吗?

最佳答案

我已尽力重现您的问题。我想我至少可以帮助您解决您列出的三个问题中的两个。

1. The item in the list view is not always properly selected. In other words, when the item is selected, a detail string value is displayed in a label control.

当通过 ListView.ItemSelectionChanged 事件选择项目时,您会收到通知:

//
// this handler's only responsibility is updating the item info label:
//
void lstModules_ItemSelectionChanged(object sender,
                                     ListViewItemSelectionChangedEventArgs e)
{
    if (e.IsSelected)
    {
        // an item has been selected; update the label, e.g.:
        lblModuleDetails.Text = e.Item.Text;
    }
    else
    {
        // some item has been de-selected; clear the label:
        lblModuleDetails.Text = string.Empty;
    }
}

3. Context menu location briefly appears at the old mouse location then moves to the new mouse location.

我相信你尝试做的太多了。让框架处理您通过 ListView.ContextMenuStrip 属性指定的上下文菜单的显示。您遇到的效果是您手动调用 ContextMenuStrip.Show(...) 导致框架显示上下文菜单,然后您再次执行相同的操作,在另一个位置。

因此,尽量不要调用这个函数;上下文菜单仍应出现。

//
// this handler's only responsibility is setting the correct context menu:
//
void lstModules_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        var hitTest = lstModules.HitTest(e.Location);
        if (hitTest != null && hitTest.Item != null)
        {
            lstModules.ContextMenuStrip = mnuContext_Module;
        }
        else
        {
            lstModules.ContextMenuStrip = mnuContext_Desktop;
        }
    }
}

顺便说一句,如果可行,您还可以摆脱 lstModules_MouseMove 事件处理程序和 mouse 位置对象。

关于c# - ContextMenuStrip 的不稳定行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2212705/

相关文章:

c# - Linq To Sql - 在限制返回的行数时防止子查询

c# - 无法将 lambda 表达式转换为类型 'System.Delegate',因为它不是委托(delegate)类型

c# - 以编程方式创建窗口时,Treeview 工具提示表现得很奇怪

c# - 使用 LINQ for Dropdown 返回串联字符串

C#多线程无符号递增

c# - 使用自动映射器 C# 进行映射时出现问题

c# - 如何使用 Visual Studio 条件断点中断特定的 Guid

c# - 填充 xaml 矩形倍数 solidcolorbrush

vb.net - 控件级别上的哪个事件相当于 Form.Load 事件?

c# - 这怎么可能 : OnPaint processed while in WaitOne