Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET forms - disabled control missing events

I have a disabled from element and I want to enable it on double-click.

The problem is the DoubleClick handler only gets called when Foo.Enabled = True. When it's disabled, the handler doesn't received the double-click event.

this.Foo.DoubleClick += new System.EventHandler(this.Foo_OnDoubleClick);

// Handler
private void Foo_OnDoubleClick(object sender, EventArgs e)
{
    Console.WriteLine("Double click");
}

Is there any workaround to this?

like image 396
armandino Avatar asked Dec 29 '25 19:12

armandino


2 Answers

Create your own control inheriting from its base control (FOO) and override the Enable behavior. That way you will be able to make it behave the way that you wish.

EXAMPLE:

public class MyControl : Button //Example control
{
    //Override and/or implement what you need in this control

    public MyControl()
    {

    }

    protected override void OnEnabledChanged(EventArgs e)
    {
        // Do whatever you wish to do
    }
}
like image 51
tweellt Avatar answered Dec 31 '25 09:12

tweellt


I still want to prevent users from entering a value when it is disabled.

Your question makes it sounds like you want to apply this to any control, but then your comment makes it sounds like you want to apply it to a TextBox.

If the latter case is true, set the TextBox.ReadOnly property to True instead of disabling the control.

Users will not be able to edit the value, but the double-click event will still fire.

like image 26
Grant Winney Avatar answered Dec 31 '25 07:12

Grant Winney