Still trying to disable controls

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

With the help of Cor Ligthert [MVP] in a pryor post I was able to make this
sub:

Public Sub disableControls(ByVal frm As Form)
'this is to create a read only form that has active buttons
Dim x As Long 'used for doing nothing
For Each ctr As Control In frm.Controls
If TypeOf ctr Is ComboBox Then
ctr.Enabled = False
ElseIf TypeOf ctr Is DateTimePicker Then
ctr.Enabled = False
ElseIf TypeOf ctr Is TextBox Then
ctr.Enabled = False
ElseIf TypeOf ctr Is GroupBox Then
ctr.Enabled = False
ElseIf TypeOf ctr Is Label Then
x = x
ElseIf TypeOf ctr Is Button Then
x = x
ElseIf TypeOf ctr Is TabControl Then
For Each tabctr As Control In System.Windows.Forms.TabControl
'???????????????
Next
End If
Next

This works great eccept for the TabControl. I need the TextBoxs, etc in the
TabControl disabled but not the Tabs or Buttons within the TabControl.
I can't seem to find the syntax I need to step through this collection (if
it is a collection). I will probably need the syntax to do the same with a
Group (which I was unable at first pass to do).

Thanks for the help. (A piece of code showing how would be Great!)
Rich
 
RichG said:
With the help of Cor Ligthert [MVP] in a pryor post I was able to make this
sub:

Public Sub disableControls(ByVal frm As Form)
'this is to create a read only form that has active buttons
Dim x As Long 'used for doing nothing
For Each ctr As Control In frm.Controls
If TypeOf ctr Is ComboBox Then
ctr.Enabled = False
ElseIf TypeOf ctr Is DateTimePicker Then
ctr.Enabled = False
ElseIf TypeOf ctr Is TextBox Then
ctr.Enabled = False
ElseIf TypeOf ctr Is GroupBox Then
ctr.Enabled = False
ElseIf TypeOf ctr Is Label Then
x = x
ElseIf TypeOf ctr Is Button Then
x = x
ElseIf TypeOf ctr Is TabControl Then
For Each tabctr As Control In System.Windows.Forms.TabControl
'???????????????
Next
End If
Next

This works great eccept for the TabControl. I need the TextBoxs, etc in the
TabControl disabled but not the Tabs or Buttons within the TabControl.
I can't seem to find the syntax I need to step through this collection (if
it is a collection). I will probably need the syntax to do the same with a
Group (which I was unable at first pass to do).

You would do it the same way you would with a form. You would probably
be better off by creating a method that takes as it's parameter a
ControlCollection and then disables the controls. This sub can call
itself if the control is working on also has controls (such as a Panel
or TabPage):

Public Sub disableControls(ByVal container As ControlCollection)
For Each ctr As Control In container
If TypeOf ctr Is ComboBox Then
ctr.Enabled = False
ElseIf TypeOf ctr Is DateTimePicker Then
ctr.Enabled = False
ElseIf TypeOf ctr Is TextBox Then
ctr.Enabled = False
ElseIf TypeOf ctr Is GroupBox Then
ctr.Enabled = False
ElseIf TypeOf ctr Is Panel
'If we're dealing with a Panel, then pass its control
collection
'recursively back into this sub to process it's controls.
Dim tmpPanel As Panel = DirectCast(ctr, Panel)
disableControls(tmpPanel.Controls)
ElseIf TypeOf ctr Is TabControl Then
'If we're working with a TabControl, then we need to loop
through its
'tab pages to get at the controls on each page.
Dim tmpCtrl As TabControl = DirectCast(ctr, TabControl)
For Each tab As TabPage In tmpCtrl
disableControls(tab.Controls)
Next
End If
Next
End Sub


Notice that I changed the argument to the Sub to be a ControlCollection
since that what the Form controls collection is as well as a Panel and
TabPage. Notice that for a TabControl, I looped through the tab pages
and then passed it's control collection back into this sub. You could
call this from a button click by using this:

disableControls(MyForm.Controls)

Hope this helps

Chris
 
Chris
Thanks a bunch. I was not able to implement exactly as you described, but
this what I ended up with. I could not implement an undefined
ControlCollection. So I ended up with two subs.

Public Sub disableControlControls(ByVal container As
Control.ControlCollection)
For Each ctr As Control In container
If TypeOf ctr Is ComboBox Then
ctr.Enabled = False
ElseIf TypeOf ctr Is DateTimePicker Then
ctr.Enabled = False
ElseIf TypeOf ctr Is TextBox Then
ctr.Enabled = False
ElseIf TypeOf ctr Is GroupBox Then
ctr.Enabled = False
ElseIf TypeOf ctr Is Panel Then
Dim tmpPanel As Panel = DirectCast(ctr, Panel)
disableControlControls(tmpPanel.Controls)
ElseIf TypeOf ctr Is TabControl Then
Dim tmpCtrl As TabControl = DirectCast(ctr, TabControl)
For Each tab As TabPage In tmpCtrl.Controls
disableControlControls(tab.Controls)
Next
End If
Next
End Sub

Public Sub disableControls(ByVal frm As Form)
'this is to create a read only form that has active buttons
Dim x As Long
For Each ctr As Control In frm.Controls
If TypeOf ctr Is ComboBox Then
ctr.Enabled = False
ElseIf TypeOf ctr Is DateTimePicker Then
ctr.Enabled = False
ElseIf TypeOf ctr Is TextBox Then
ctr.Enabled = False
ElseIf TypeOf ctr Is Label Then
x = x
ElseIf TypeOf ctr Is Button Then
x = x
ElseIf TypeOf ctr Is GroupBox Then
Dim tmpGrp As GroupBox = DirectCast(ctr, GroupBox)
disableControlControls(tmpGrp.Controls)
ElseIf TypeOf ctr Is Panel Then
'If we're dealing with a Panel, then pass its control
collection
'recursively back into this sub to process it's controls.
Dim tmpPanel As Panel = DirectCast(ctr, Panel)
disableControlControls(tmpPanel.Controls)
ElseIf TypeOf ctr Is TabControl Then
'If we're working with a TabControl, then we need to loop
through its
'tab pages to get at the controls on each page.
Dim tmpCtrl As TabControl = DirectCast(ctr, TabControl)
For Each tab As TabPage In tmpCtrl.Controls
disableControlControls(tab.Controls)
Next
End If
Next
End Sub

I don't understand this:
Dim tmpCtrl As TabControl = DirectCast(ctr, TabControl)
What is the purpose of the DirectCast?

Thanks, Rich
 
Back
Top