从的ContextMenuStrip的古怪行为 [英] Erratic behavior from ContextMenuStrip

查看:140
本文介绍了从的ContextMenuStrip的古怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一些古怪行为 ContextMenuStip

 私人无效lstModules_MouseMove(对象发件人,发送MouseEventArgs E)
 {鼠标= e.Location; }
 私人无效lstModules_MouseDown(对象发件人,发送MouseEventArgs E)
 {
  ListViewItem的项目= NULL;
  如果((的hitTest = lstModules.HitTest(鼠标))!= NULL)
   项目= hitTest.Item;

  开关(e.Button)
  {
   案例MouseButtons.Right:
    如果(项目!= NULL)
    {
     //有效的项目选择
     ShowModuleDetails(item.Name);
     lstModules.ContextMenuStrip = mnuContext_Module;
    }
    其他
    {
     //右击 - 没有项目选择
     lblModuleDetails.Text =的String.Empty;
     lstModules.ContextMenuStrip = mnuContext_Desktop;
    }

    lstModules.ContextMenuStrip.Show(lstModules,鼠标);
    打破;
   案例MouseButtons.Left:
    如果(项目!= NULL)
    {ShowModuleDetails(item.Name); }
    打破;
  }
 }
 私人无效ShowModuleDetails(字符串的modname)
 {
        //从词典模块的详细信息
  lblModuleDetails.Text =模块[的modname] .Details;
 }
 

  1. 在列表中查看该项目时,上下文菜单显示不正确选择。换句话说,当项目被选中,一个细节的字符串值显示在标签控件。
  2. 如果上下文菜单是可见的,并且选择了一个项目,该项目的详细信息并没有改变。
  3. 在上下文菜单位置短暂地出现在的的鼠标位置,然后移动到的的鼠标位置。

有什么我做错了与上下文菜单?

解决方案

我试图重现你的问题,据我所能。我想我可以帮你,至少你们两个已经列出了三个问题。

  

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

您可以在一个项目已经通过了 ListView.ItemSelectionChanged 事件选择通知:

  //
//此处理的唯一责任是更新项目信息标签:
//
无效lstModules_ItemSelectionChanged(对象发件人,
                                     ListViewItemSelectionChangedEventArgs五)
{
    如果(e.IsSelected)
    {
        //一个项已被选择;更新标签,例如:
        lblModuleDetails.Text = e.Item.Text;
    }
    其他
    {
        //一些项目已经取消选择;清除标签:
        lblModuleDetails.Text =的String.Empty;
    }
}
 

  

3。上下文菜单位置短暂地出现在旧的鼠标位置,然后移动到新的鼠标位置。

我相信你尝试做太多。让框架处理您通过 ListView.ContextMenuStrip 属性指定的上下文菜单的显示。你体验效果是由您手动调用 ContextMenuStrip.Show(...),这将导致在上下文菜单中的框架显示,然后你做同样的事情在第二时间,在另一个位置。

因此​​,尽量不要调用这个函数;上下文菜单中应该还是会出现。

  //
//此处理的唯一责任是设置正确的上下文菜单:
//
无效lstModules_MouseDown(对象发件人,发送MouseEventArgs E)
{
    如果(e.Button == MouseButtons.Right)
    {
        VAR的hitTest = lstModules.HitTest(e.Location);
        如果(的hitTest = NULL和放大器;!&安培;!hitTest.Item = NULL)
        {
            lstModules.ContextMenuStrip = mnuContext_Module;
        }
        其他
        {
            lstModules.ContextMenuStrip = mnuContext_Desktop;
        }
    }
}
 

顺便说一句,如果作品,你也可以摆脱你的 lstModules_MouseMove 事件处理程序和小鼠位置对象。

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?

解决方案

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.

这篇关于从的ContextMenuStrip的古怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆