Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable all obejcts in a frame 6

Status
Not open for further replies.

Overdoos

IS-IT--Management
May 31, 2000
79
BE
Hello,

I once knew how to do this, but I forgot and now I need it again :/

OK, here goes...

I have a form containing 3 frames. Each frame contains a number of text-fields and a button. The textfields are (should be) disabled and the button is marked 'Edit'.

When you click the button, the caption becomes 'SAVE' and writes the changed data to the DB (this works). The thing that does not work is enabling all the textfields in the frame upon clicking the 'EDIT'-button and disabling the fields again when the 'SAVE'-button is clicked.

I can do this if I define all required procedures PER frame, but since I have to do nearly the same for 3 different frames, it is clear that I want to call a function in order to do this.

Anybody got an idea how I can address ALL objects contained in a frame in VB6?
 
Loop through the controls collection, and check each control to see if its "container" is the frame in question.
If it is, the enable it as needed.

Another way is to keep the edit pushbutton outside of the frame and have the frame disabled, and then enable the frame itself when the edit button is clicked, thereby enableing all of the controls inside of it at the same time.

Robert
 
Hi Overdoos:

To lock all the controls within a frame, I use:
Code:
    fraMyFrame.Enabled = False

To re-enable the controls, I use:
Code:
    fraMyFame.Enabled = True

Any control that appears to be in the frame but is not disabled (e.g. Edit button), I add the control to the frame's container, not to the frame itself.

Cassandra
 

The controls within the frame are disabled it's just that they do not inherit the disabled "look" as the frame when it is set to Enabled = false

My suggestion is to use TheVampire's suggestion with a small twist--use the container property of the frame in question to enable or disable all the object it holds (including the frame itself).

For Each obj In Frame1.Container
obj.Enabled = False
Next

You can create a function that passes a frame as a byRef argument as well as the state in which you want the controls placed (true/false).




Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
thank you all for your replys. The solution that TheVampire brought on was the one I was looking for, but I like the options the other solutions brought on as well.

I've got one small extra question though:

Is there a simple way to check what type of control we are dealing with? Ofc. I could check the first letters of the name for the convention I use, but something in me is hoping that somehow, the type of control is stored in some sort of parameter of that control.

The help-entrys of .type do not seem to be what I'm looking for and .style is also not quite what I need...
 
There are two ways to find the type of control.

Code:
if Textbox1 typeof TextBox then....

or (the one I prefer)

If Typename(Textbox1) = "TextBox" then....

Matt

If you can keep your head while those around you are losing theirs, you obviously haven't grasped the seriousness of the situation
 
the solution is near! (working on it now)

Thank you all. When I'm done, I'll post the code...
 
Overdoos,

This is the function that works for the entire frame:

Private Sub EnableDisableFrame(ByRef pobjFrame As Frame, ByVal pblnEnable As Boolean)

Dim objControl As Control

With pobjFrame

For Each objControl In .Parent.Controls

If objControl.Container.Name = .Name Then
objControl.Enabled = pblnEnable
End If
Next

.Enabled = pblnEnable

End With

End Sub


Vlad
 
Here we go again.

One step closer to home but I threw around some stuff in the app.

The current sithuation:

I've got a form containing a tab-string with several tabs. Of course all the tabs have a frame to fill the tabscreen and certain of those frames have frames in them to group stuff together.

The function to enable stuff in one of those 'sub-frames' works (I'll post about that later) but I also wanted a function that would disable all used input-fields in that one tab-screen (so in the 'parent'-frame). I thought of recursive procedures, but for some reason it hangs when I execute...

this is what it looks like
Code:
  _______________________
 /       \_______\_______\______________________
|+--[frame]------------------------------------+|
||                                             ||
|| +--[subframe1]----------------------------+ ||
|| | [item]  [item]        [item]            | ||
|| +-----------------------------------------+ ||
||                                             ||
|| +--[subframe2]----------------------------+ ||
|| | [item]  [item]        [item]            | ||
|| +-----------------------------------------+ ||
||                                             ||
|| +--[subframe3]----------------------------+ ||
|| | [item]  [item]        [item]            | ||
|| +-----------------------------------------+ ||
|+---------------------------------------------+|
|_______________________________________________|
|_STATUSBAR_____________________________________|

this is the code I use...

Code:
Public Sub DisableAllInScreen(ByRef objThisFrame As Object)
  Dim item As Object
  If objThisFrame.Index = "" Then
    ' disable everything in a non-indexed frame
    For Each item In frmDvmMain.Controls
      If item.Container.Name = objThisFrame.Name Then
        If UCase(TypeName(item)) = "FRAME" Then
          DisableAllInScreen (item)
        ElseIf UCase(TypeName(item)) = "COMBOBOX" Or _
               UCase(TypeName(item)) = "TEXTBOX" Or _
               UCase(TypeName(item)) = "CHECKBOX" Then
          item.Enabled = False
        End If
      End If
    Next item
  Else
    ' disable everything in an indexed-frame
    For Each item In frmDvmMain.Controls
      If item.Container.Name = objThisFrame.Name Then
        If item.Container.Index = objThisFrame.Index Then
          If UCase(TypeName(item)) = "FRAME" Then
            DisableAllInScreen (item)
          ElseIf UCase(TypeName(item)) = "COMBOBOX" Or _
                 UCase(TypeName(item)) = "TEXTBOX" Or _
                 UCase(TypeName(item)) = "CHECKBOX" Then
            item.Enabled = False
          End If
        End If
      End If
    Next item
  End If
End Sub

thanks
 
silly me, I forgot to mention the part where this goes wrong...

Code:
If UCase(TypeName(item)) = "FRAME" Then
  ' these are the line where things turn bad.
  ' I get an error saying I'm not passing an object
  ' where item is clearly defined as being one...
  DisableAllInScreen (item)
elseif...

 
A quick note, instead of using If .... ElseIf....Endif you could use a select case, It makes the code a bit clearer.

Code:
Public Sub DisableAllInScreen(ByRef objThisFrame As Object)
  Dim item As Object
  If objThisFrame.Index = "" Then
    ' disable everything in a non-indexed frame
    For Each item In frmDvmMain.Controls
      If item.Container.Name = objThisFrame.Name Then
           Select case Ucase(Typename(item))
	Case "FRAME"
	          DisableAllInScreen (item)
	Case "COMBOBOX","TEXTBOX","CHECKBOX"
       	          item.Enabled = False
          End Select
      End If
    Next item
  Else
    ' disable everything in an indexed-frame
    For Each item In frmDvmMain.Controls
      If item.Container.Name = objThisFrame.Name Then
        If item.Container.Index = objThisFrame.Index Then
           Select case Ucase(Typename(item))
	Case "FRAME"
	          DisableAllInScreen (item)
	Case "COMBOBOX","TEXTBOX","CHECKBOX"
       	          item.Enabled = False
           End Select
        End If
      End If
    Next item
  End If
End Sub

Matt

If you can keep your head while those around you are losing theirs, you obviously haven't grasped the seriousness of the situation
 
Take away the brackets as well.

Code:
DisableAllInScreen item

Matt

If you can keep your head while those around you are losing theirs, you obviously haven't grasped the seriousness of the situation
 
Thanks Mat.

Ofc I should have seen it, but i didn't. Removing the brackets did the trick.

God, the more I try to manipulate this trick I get more and more frustrated about how inconsistent this language is. I hope I'll learn to like it someday.

(Sorry, about this, but I just had to get it off of my chest.)

 
it certainly has some 'Quirks' for want of a better word!

If you can keep your head while those around you are losing theirs, you obviously haven't grasped the seriousness of the situation
 
Overdoos,

Put my EnableDisableContainer sub into the module. This sub is recursive and handles "unlimited" number of layers of embedded into each other containers including such containers as frames, picture boxes, and SStab controls. In the form, call it for any container-layer you want to.

Let me know how you like it.

Vlad

Option Explicit

Public Const CONTAINER_ENABLE As Boolean = True
Public Const CONTAINER_DISABLE As Boolean = False

Private Const CONTAINER_FRAME As String = "Frame"
Private Const CONTAINER_PIC_BOX As String = "PictureBox"
Private Const CONTAINER_SSTAB As String = "SSTab"

Public Enum eEnableDisableContainer
edcDisable = CONTAINER_DISABLE
edcEnable = CONTAINER_ENABLE
End Enum

Public Sub EnableDisableContainer(ByRef pobjContainer As Control, _
Optional ByVal pblnEnable As eEnableDisableContainer = edcEnable, _
Optional ByVal pintSSTabIndx As Integer)

'******************************************************************************

' DESCRIPTION: Enables/disables frames, picture boxes, and SStab tabs including
' all the controls they contain.
'
' Frame control example to disable it:
'
' EnableDisableContainer Frame1, edcDisable
'
' SSTab control example to disable controls at its first tab.
'
' EnableDisableContainer SSTab1, edcDisable, 0
'
'
'******************************************************************************

Dim lngErrNum As Long
Dim strErrSource As String
Dim strErrDesc As String


On Error GoTo MethodExit

Dim objControl As Control
Dim objContainer As Control

Set objContainer = pobjContainer

With objContainer

For Each objControl In .Parent.Controls
Select Case TypeName(pobjContainer)

Case CONTAINER_FRAME, CONTAINER_PIC_BOX
If objControl.Container.Name = .Name Then
objControl.Enabled = pblnEnable
End If

If TypeName(objControl) = CONTAINER_FRAME Or _
TypeName(objControl) = CONTAINER_PIC_BOX Then
If objControl.Container.Name = .Name Then
EnableDisableContainer objControl, pblnEnable
End If
End If

.Enabled = pblnEnable

Case CONTAINER_SSTAB

If .Tab = pintSSTabIndx Then
If objControl.Container.Name = .Name Then
objControl.Enabled = pblnEnable
End If
If TypeName(objControl) = CONTAINER_FRAME Or _
TypeName(objControl) = CONTAINER_PIC_BOX Then
If objControl.Container.Name = .Name Then
EnableDisableContainer objControl, pblnEnable
End If
End If

.TabEnabled(pintSSTabIndx) = pblnEnable
End If

Case Else
' Do nothing since other types of controls are not containers
End Select
Next

End With

MethodExit:
Set objContainer = Nothing

If Err.Number <> 0 Then
MsgBox "Error " & CStr(Err.Number) & " in EnableDisableContainer"
End If

End Sub

 
Vladk,

thanks for all the work you put into this. I'm trying to figure out how this works exactly. So far it seems like a great solution.

The reason I refused to use the first version of your script as is, is that I really want to get the hang of this. (I need to be able to understand what is going on before I use it).

thanks.
 
Vladk,

I've gone throught your code, and asked myself why it wouldn't halt on the same error as mine so I created a button to activate it and ran in on my program.

What I feared came true... it halted on the same control as my piece of code... the imagelist.

The problem as far as I can see is that the imagelist has no way of storing a container and once you test it for it's container-value, you get a 438 error.

BTW. The line I used to run your code:
Code:
EnableDisableContainer fmeMainDetail(1), edcDisable

--
fmeMainDetail is the name of the frame
1 is the index of the controlarray (frames) where I need everything to be disabled

another one I tried gave me no results...
Code:
EnableDisableContainer tabMainDetail, edcDisable, 1

--
tabMainDetail is the name of the tabstrip
1 is the index of the tab where the controls are that need to be disabled.

any ideas on the imagelist-problem? I think I'l try to cut it out with an IF or a SELECT CASE statement...
 
Being that an image list is not something you will see anyway, your best bet is to just protect this in the error handler.

You could have the error handler monitor for this error and resume around it.

So a modified version of Vladk's code

Code:
Option Explicit

Public Const CONTAINER_ENABLE As Boolean = True
Public Const CONTAINER_DISABLE As Boolean = False

Private Const CONTAINER_FRAME As String = "Frame"
Private Const CONTAINER_PIC_BOX As String = "PictureBox"
Private Const CONTAINER_SSTAB As String = "SSTab"

Public Enum eEnableDisableContainer
    edcDisable = CONTAINER_DISABLE
    edcEnable = CONTAINER_ENABLE
End Enum

Public Sub EnableDisableContainer(ByRef pobjContainer As Control, _
                                  Optional ByVal pblnEnable As eEnableDisableContainer = edcEnable, _
                                  Optional ByVal pintSSTabIndx As Integer)
    
    '******************************************************************************

    '   DESCRIPTION: Enables/disables frames, picture boxes, and SStab tabs including
    '                all the controls they contain.
    '
    '                Frame control example to disable it:
    '
    '                   EnableDisableContainer Frame1, edcDisable
    '
    '                SSTab control example to disable controls at its first tab.
    '
    '                   EnableDisableContainer  SSTab1, edcDisable, 0
    '
    '
    '******************************************************************************

    Dim lngErrNum As Long
    Dim strErrSource As String
    Dim strErrDesc As String

    
    On Error GoTo MethodExit
    
    Dim objControl As Control
    Dim objContainer As Control
   
    Set objContainer = pobjContainer
    
    With objContainer
        
        For Each objControl In .Parent.Controls
            Select Case TypeName(pobjContainer)
                  
                Case CONTAINER_FRAME, CONTAINER_PIC_BOX
                    If objControl.Container.Name = .Name Then
                        objControl.Enabled = pblnEnable
                    End If
                    
                    If TypeName(objControl) = CONTAINER_FRAME Or _
                       TypeName(objControl) = CONTAINER_PIC_BOX Then
                        If objControl.Container.Name = .Name Then
                            EnableDisableContainer objControl, pblnEnable
                        End If
                    End If
                    
                    .Enabled = pblnEnable
                    
                Case CONTAINER_SSTAB
                    
                    If .Tab = pintSSTabIndx Then
                        If objControl.Container.Name = .Name Then
                            objControl.Enabled = pblnEnable
                        End If
                        If TypeName(objControl) = CONTAINER_FRAME Or _
                           TypeName(objControl) = CONTAINER_PIC_BOX Then
                            If objControl.Container.Name = .Name Then
                                EnableDisableContainer objControl, pblnEnable
                            End If
                        End If
                        
                        .TabEnabled(pintSSTabIndx) = pblnEnable
                    End If
                    
                Case Else
                    ' Do nothing since other types of controls are not containers
            End Select
SkipIt:
        Next
    
    End With
    
MethodExit:
    Set objContainer = Nothing
    
    
    If Err.Number <> 0 Then
        If Err.Number = 438 Then
            Err.Clear
            Resume SkipIt
        Else
            MsgBox "Error " & CStr(Err.Number) & " in EnableDisableContainer"
        End If
    End If
    
End Sub

This handles an image list, or any other control without a container property.

Matt

If you can keep your head while those around you are losing theirs, you obviously haven't grasped the seriousness of the situation
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top