Register MouseEnter/MouseLeave events for disabled controls in windows form?

后端 未结 5 1375
暗喜
暗喜 2021-01-13 14:43

I want to register MouseEnter/MouseLeave events for disabled buttons. It does not work allthough it does work for enabled buttons..

//Enable Disable controls         


        
相关标签:
5条回答
  • 2021-01-13 15:12

    I tried many but ended up using this simple trick which I think it is more effective.

    Create a subclass(CustomControl with just base control in it) which extends UserControl

    then instead of setting "Enabled" property to false create a Method which disables just basecontrol in it instead of whole CustomControl.

    Set the tool tip on CustomControl still will be able to fire eventhandlers setting the basecontrol disabled. This works wherever CustomControl is in use rather than coding on every form you use with.

    Here is the hint.. :)

    public partial class MyTextBox : UserControl
    {
     ...
     ...
     ...
    
    
     public void DisableMyTextBox()
      {
        this.txt.Enabled = false;  //txt is the name of Winform-Textbox from my designer
        this.Enabled = true;
      }
    
    public void EnableMyTextBox()
      {
        this.txt.Enabled = true;
        this.Enabled = true;
      }
    
    //set the tooltip from properties tab in designer or wherever
    

    }

    0 讨论(0)
  • 2021-01-13 15:17

    yes , when you disable button , events will disable.

    you can use this trick:

    put your button in panel1 , enter image description here

    then use the same event of button for panel1. like this:

        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);
    

    it will work.

    0 讨论(0)
  • 2021-01-13 15:19

    Why can't you try this?

    By adding MouseMove event on the form...

    0 讨论(0)
  • 2021-01-13 15:22

    Alternatively, if you want an easy way of maintaining the event handling, you could just never actually disable the button. Add some sort of wrapper class around a button that changes the the implementation of your buttons.

    The new disable property could just change some CSS on the button and change a property that would make the click handler not do anything (or other relevant events).

    0 讨论(0)
  • 2021-01-13 15:27

    You can try some Form-wide Mouse message solution like this:

    //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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题