Matt Wilko
Matt Wilko

Reputation: 27322

WinForms UserControl How Do I Stop the control receiving focus

I have a custom UserControl that contains just one TextBox.

When I set the control to Enabled = False, the TextBox is disabled but the control is not (control still fires the Enter event).

How do I ensure that the UserControl will not receive focus?

My Enabled Property Looks like this:

Private _Enabled As Boolean = True

Public Shadows Property Enabled As Boolean
    Get
        Return _Enabled
    End Get
    Set(value As Boolean)
        _Enabled = value
        txtTime.Enabled = value
    End Set
End Property

Upvotes: 1

Views: 1700

Answers (2)

Deanna
Deanna

Reputation: 24253

To answer your immediate question, you need to pass the enabled property to the base object:

public new bool Enabled {
    get { return _Enabled; }
    set {
        _Enabled = value;
        textBox1.Enabled = value;
        base.Enabled = value;
    }
}

However... the correct way to do it is using the OnEnabledChanged override:

protected override void OnEnabledChanged(EventArgs e) {
    textBox1.Enabled = this.Enabled;
    base.OnEnabledChanged(e);
}

(As you tagged it C#, I assume you can convert back to VB.net yourself)

Upvotes: 3

Miguel Angelo
Miguel Angelo

Reputation: 24182

Every control already has a property called Enabled. You don't have to create your own property to do that.

This property is inherited from the Control class, that is the base for every windows forms controls.

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx

Upvotes: 2

Related Questions