c# – 在Windows窗体中为禁用的控件注册MouseEnter / MouseLeave事件?

我想为禁用按钮注册MouseEnter / MouseLeave事件.虽然它对启用的按钮有效,但它不起作用.//Enable Disable controls on form loadEnableDisableControls(Load);var grupButtons = control.Controls.OfTypeButton(...

我想为禁用按钮注册MouseEnter / MouseLeave事件.虽然它对启用的按钮有效,但它不起作用.

//Enable Disable controls on form load
                EnableDisableControls("Load");

var grupButtons = control.Controls.OfType<Button>();
                    foreach (Button btns in grupButtons)
                    {
                        //btns.MouseMove += new MouseEventHandler(MainframeDataExchangeTool_MouseMove);
                        btns.MouseEnter += new EventHandler(btns_MouseEnter);
                        btns.MouseLeave += new EventHandler(btns_MouseLeave);
                    }

private void btns_MouseEnter(object sender, EventArgs e)
        {

        }

        private void btns_MouseLeave(object sender, EventArgs e)
        {
            var parent = sender as Control;
            string tipstring = string.Empty;
            if (parent == null)
            {
                return;
            }
            string enter = sender.GetType().ToString() + ": MouseEnter";
        }

它适用于启用按钮……但是如何禁用按钮…我必须在mouseenter上显示工具提示操作并立即在mouseleave上使其消失?

解决方法:

是的,当您禁用按钮时,事件将禁用.

你可以使用这个技巧:

把你的按钮放在panel1中,

然后对panel1使用相同的按钮事件.像这样:

    btns.MouseEnter += new EventHandler(btns_MouseEnter);
    btns.MouseLeave += new EventHandler(btns_MouseLeave);

    panel1.MouseEnter += new System.EventHandler(btns_MouseEnter);
    panel1.MouseLeave += new System.EventHandler(btns_MouseLeave);

它会工作.

本文标题为:c# – 在Windows窗体中为禁用的控件注册MouseEnter / MouseLeave事件?

基础教程推荐