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

后端 未结 5 1377
暗喜
暗喜 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
    

    }

提交回复
热议问题