Disable mouse capturing by Button

不打扰是莪最后的温柔 提交于 2019-12-24 13:52:13

问题


I create custom ContextMenuStrip with Button in it:

ContextMenuStrip _contextMenu = new ContextMenuStrip();
_contextMenu.Items.Add(new ToolStripMenuItem("Item"));
_contextMenu.Items.Add(new ToolStripControlHost(new Button()));

When I open this context menu and move mouse over 'Item' it is highlighted. But after I clicked Button and then move mouse over 'Item' again it isn't highlighted anymore. Looks like Button captures mouse. How can I avoid this or release capturing after Button clicking?


回答1:


You can create your own button class inherited from Button and set ControlStyles.Selectable to false, this will prevent it from taking focus:

public class MyButton : Button
{
    public MyButton()
    {
        SetStyle(ControlStyles.Selectable, false);
    }
}

And then just use it instead of Button:

_contextMenu.Items.Add(new ToolStripControlHost(new MyButton()));


来源:https://stackoverflow.com/questions/35363778/disable-mouse-capturing-by-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!