4

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

//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";
        }

It is working for enable button ...but what to do for disable button ... I have to show tooltip operation on mouseenter and make it disapper immediately on mouseleave ?

Cas Nouwens
  • 395
  • 1
  • 5
  • 25
Rahul Chowdhury
  • 1,138
  • 6
  • 29
  • 52

5 Answers5

5

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.

AmirHossein Rezaei
  • 1,086
  • 1
  • 16
  • 20
  • Hossein Razaei ...Even is working but it is getting fired when i am entering in the groupbox ...I have used group box and buttons inside it...but i want event get fired when i enter the button and when i leave it ,,,And thanks for your help – Rahul Chowdhury Oct 10 '13 at 08:28
2

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;
    }
}
King King
  • 61,710
  • 16
  • 105
  • 130
0

Why can't you try this?

By adding MouseMove event on the form...

Community
  • 1
  • 1
Vanest
  • 906
  • 5
  • 14
0

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

ImGreg
  • 2,946
  • 16
  • 43
  • 65
0

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

}

Rakesh
  • 21
  • 1