Brijesh Patel
Brijesh Patel

Reputation: 2958

Enable Controls within disabled Panel in VB.Net

I have some Controls inside a Panel inside a Form. I want to disable the Panel but some Controls inside the panel need to stay enabled.

Is there any way to enable some Controls inside a disabled Panel?
I would be happy about some ideas how to do that.

Upvotes: 0

Views: 4981

Answers (3)

user4874305
user4874305

Reputation: 1

I have a good solution for it.

Unless you really want to disable your panel, just create 2 functions:

Function disablePanel()

    For Each element In yourForm.yourPanel.controls
        element.enabled = False
    Next

    Return Nothing
End Function


Function enablePanel()

    For Each element In yourForm.yourPanel.controls
        element.enabled = true
    Next

    Return Nothing
End Function

thanks to it, you have all of your component in your panel disable, but not your panel. So you can still do whatever you want on your panel, or not disable a wanted control : )

cheers.

Upvotes: 0

One of the benefits of Panels is the can used to provide logical grouping with little or no visual element (compared to a Groupbox which is both logical AND visual). Given your question, perhaps all the child controls do not actually belong in the same container (logical group). You could use 2 or 3 panels to solve the container/child enabling problem and use the BorderStyle and BackColor properties to make them look like they are all one panel (perhaps on a master panel whose sole role is to position the children).

You can also "cascade" enabling to logically dependent elements from events. For instance, consider a checkbox "Absolute Position" which controls whether or not other controls (X Pos, Y Pos) are enabled. X and Y's enabled state can be toggled from events on chkAbsPosition such as CheckChanged and EnabledChanged.

Depending on your form, you might be able to toggle 2 or 3 controls and let each of them in turn set the state for dependent elements.

HTH

Upvotes: 2

Giannis Paraskevopoulos
Giannis Paraskevopoulos

Reputation: 18441

The panel is a container and its enabled property will supersede its child ones. So you have to enable the panel in order to be able to enable the controls.

What you could possibly do is not put the controls in the panel, but on top of it, and enable disable accordingly. That way they will look like they are in the panel when they are not.

Upvotes: 0

Related Questions