Form navigation

chowchow123

Active member
Joined
Sep 6, 2007
Messages
34
Programming Experience
1-3
Hi

I have a form where I want to set the textbox fields to readonly mode by pressing a toolbar button. I've tried the code below - and I get a type cast error 'Unable to cast object of type 'System.Windows.Forms.BindingNavigator' to type 'System.Windows.Forms.TextBoxBase'.' What can I change the Me.Controls to which is causing the problem?


Dim i As TextBoxBase

For Each i In Me.Controls 'iterate through whole form

If TypeOf (i) Is TextBoxBase Then 'if i is a textbox, do nothing

i.ReadOnly = True 'not a button, so enable it for edit

End If

Next
 
It's a good start, here's the working code. Compare the two and think about what i've done and you should be able to see where you went wrong. If not ask and i'll explain.


VB.NET:
For Each Ctrl As Control In Me.Controls 'iterate through whole form

If TypeOf Ctrl Is TextBoxBase Then 'if i is a textbox, do nothing

DirectCast(Ctrl, TextBoxBase).ReadOnly = True 'not a button, so enable it for edit

End If

Next

Note though, this code won't detect any textboxes that you may have in Panels or GroupBoxes.
 
Thanks for the caveat - I tried the code out and then realised that I use tab controls on the form and group boxes

How would I modify it so it works with groupboxes ?
 
VB.NET:
Dim ctrl As Control = Me.GetNextControl(Me, True)
  While ctrl IsNot Nothing
    If TypeOf ctrl Is TextBoxBase Then
      DirectCast(ctrl, TextBoxBase).ReadOnly = True
    End If
    ctrl = Me.GetNextControl(ctrl, True)
  End While
 
vis781, that is a pretty neat trick, I have been trying to loop through contols in group boxes to clear them and was not having any luck, now it works just fine.

Is there any way to loop through radio buttons in a group box to see if the Check_Changed event has occurred so I don't have to repeatedly call

VB.NET:
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
            End Sub

for every radio button.

Thanks
CoachBarker
 
You can use that event handler to handle the event for all your radiobuttons in the group. (handles rad1.cc, rad2.cc, rad3.cchanged) You don't have to write that, just select these controls in designer, and assign event handler in properties box (filter by events)
 
I do not understand what you mean. When I select them all and go to the properties window there is no listing for event handler or filter by events:confused:
 
You know where you find properties for control, there is an icon that looks like lightning, when you hover it the tooltip says "Events".
 
Back
Top