ContextMenuStrip 的不稳定行为

发布于 2024-08-20 20:50:50 字数 1224 浏览 11 评论 0原文

我从 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. 显示上下文菜单时,列表视图中的项目未正确选择。换句话说,当选择该项目时,详细字符串值将显示在标签控件中。
  2. 如果上下文菜单可见,并且选择了某个项目,则该项目的详细信息不会更改。
  3. 上下文菜单位置短暂出现在鼠标位置,然后移动到鼠标位置。

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

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;
 }
  1. 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.
  2. If a context menu is visible, and an item is selected, the item details do not change.
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷炫老祖宗 2024-08-27 20:50:50

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

1。列表视图中的项目并不总是被正确选择。换句话说,当选择该项目时,详细信息字符串值将显示在标签控件中。

当通过 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.上下文菜单位置短暂出现在旧鼠标位置,然后移动到新鼠标位置。

我相信您尝试做的事情太多了。让框架处理您通过 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 位置对象。

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.

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.

You can be notified when an item has been selected via the ListView.ItemSelectionChanged event:

//
// 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.

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 calling ContextMenuStrip.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.

//
// 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;
        }
    }
}

Btw., if that works, you can also get rid of your lstModules_MouseMove event handler and the mouse location object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文