Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否在windows窗体中为禁用的控件注册MouseEnter/MouseLeave事件?_C#_.net_Windows_Winforms - Fatal编程技术网

C# 是否在windows窗体中为禁用的控件注册MouseEnter/MouseLeave事件?

C# 是否在windows窗体中为禁用的控件注册MouseEnter/MouseLeave事件?,c#,.net,windows,winforms,C#,.net,Windows,Winforms,我想为禁用的按钮注册MouseEnter/MouseLeave事件。虽然它对已启用的按钮有效,但并不完全有效 //Enable Disable controls on form load EnableDisableControls("Load"); var grupButtons = control.Controls.OfType<Button>(); foreach (Button btns in grupB

我想为禁用的按钮注册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";
        }
//在表单加载时启用或禁用控件
启用禁用控制(“加载”);
var grupButtons=control.Controls.OfType();
foreach(grupButtons中的按钮BTN)
{
//btns.MouseMove+=新的MouseEventHandler(MainframeDataExchangeTool\u MouseMove);
btns.MouseEnter+=新事件处理程序(btns\u MouseEnter);
btns.MouseLeave+=新事件处理程序(btns\u MouseLeave);
}
私有void btns_MouseEnter(对象发送方,事件参数e)
{
}
私有void btns_MouseLeave(对象发送方,事件参数e)
{
var parent=发送方作为控制;
string tipstring=string.Empty;
如果(父项==null)
{
返回;
}
字符串enter=sender.GetType().ToString()+“:MouseEnter”;
}

它正在为启用按钮工作…但为禁用按钮做什么。。。我必须在mouseenter上显示工具提示操作,并使其在Mouseleve上立即消失?

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

您可以使用以下技巧:

把你的按钮放在面板1上,

然后对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);
//Suppose your disabled Button is button1
public partial class Form1 : Form, IMessageFilter
{
    public Form1()
    {
        InitializeComponent();
        button1.Enabled = false;
        button1.BackColor = Color.Green;
        //Try this to see it in action
        button1.MouseEnter += (s, e) => {
            button1.BackColor = Color.Red;
        };
        button1.MouseLeave += (s, e) => {
            button1.BackColor = Color.Green;
        };
        Application.AddMessageFilter(this);//Add the IMessageFilter to the current Application
    }
    bool entered;
    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == 0x200) //WM_MOUSEMOVE = 0x200
        {
            if (Control.FromHandle(m.HWnd) == button1.Parent && 
                button1.ClientRectangle.Contains(button1.PointToClient(MousePosition)))
            {
                if (!entered) {
                    entered = true;
                    //Raise the MouseEnter event via Reflection
                    typeof(Button).GetMethod("OnMouseEnter", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
                        .Invoke(button1, new[] { EventArgs.Empty });
                }                    
            }
            else if (entered) {
                //Raise the MouseLeave event via Reflection
                typeof(Button).GetMethod("OnMouseLeave", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
                    .Invoke(button1, new []{EventArgs.Empty});
                entered = false;                    
            }
        }
        return false;
    }
}
它会有用的。

为什么你不能试试


通过在表单上添加MouseMove事件…

您可以尝试一些
表单范围的鼠标消息
解决方案,如下所示:

    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);
//Suppose your disabled Button is button1
public partial class Form1 : Form, IMessageFilter
{
    public Form1()
    {
        InitializeComponent();
        button1.Enabled = false;
        button1.BackColor = Color.Green;
        //Try this to see it in action
        button1.MouseEnter += (s, e) => {
            button1.BackColor = Color.Red;
        };
        button1.MouseLeave += (s, e) => {
            button1.BackColor = Color.Green;
        };
        Application.AddMessageFilter(this);//Add the IMessageFilter to the current Application
    }
    bool entered;
    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == 0x200) //WM_MOUSEMOVE = 0x200
        {
            if (Control.FromHandle(m.HWnd) == button1.Parent && 
                button1.ClientRectangle.Contains(button1.PointToClient(MousePosition)))
            {
                if (!entered) {
                    entered = true;
                    //Raise the MouseEnter event via Reflection
                    typeof(Button).GetMethod("OnMouseEnter", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
                        .Invoke(button1, new[] { EventArgs.Empty });
                }                    
            }
            else if (entered) {
                //Raise the MouseLeave event via Reflection
                typeof(Button).GetMethod("OnMouseLeave", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
                    .Invoke(button1, new []{EventArgs.Empty});
                entered = false;                    
            }
        }
        return false;
    }
}

或者,如果您想要一种简单的方法来维护事件处理,您可以永远不禁用按钮。在按钮周围添加某种包装类,以更改按钮的实现


新的disable属性只需更改按钮上的一些CSS,并更改一个属性,使单击处理程序不会执行任何操作(或其他相关事件)。

我尝试了很多次,但最终使用了这个简单的技巧,我认为它更有效

创建扩展UserControl的子类(CustomControl,其中仅包含基本控件)

然后创建一个只禁用其中的basecontrol而不是整个CustomControl的方法,而不是将“Enabled”属性设置为false

在CustomControl上设置工具提示仍将能够启动eventhandlers,并将basecontrol设置为禁用。这在使用CustomControl的任何地方都有效,而不是在您使用的每个表单上进行编码

以下是提示:)


}

设置按钮的
工具提示
属性不起作用吗?尝试使用按钮的
工具提示
属性,而不是在事件中执行此操作在windows窗体中没有任何工具提示属性…如果您可以举个例子,如果您在按钮显示工具提示时禁用按钮,那么您肯定有问题。我不认为这是一个可解决的问题,你必须避免这种情况发生。侯赛因·拉扎伊……甚至在工作,但当我进入群组框时,它被解雇了……我使用了群组框和其中的按钮……但我希望在我进入按钮和离开按钮时事件被解雇,,,,谢谢你的帮助