I want to register MouseEnter/MouseLeave events for disabled buttons. It does not work allthough it does work for enabled buttons..
//Enable Disable controls
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
}
yes , when you disable button , events will disable.
you can use this trick:
put your button in panel1 ,

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.
Why can't you try this?
By adding MouseMove event on the form...
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).
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;
}
}