Help: Control loops & properties in VB .Net 2005

vsugan_work

New member
Joined
May 16, 2006
Messages
3
Programming Experience
Beginner
Hi all,
I'm using Visual Basic 2005. I would like to loop through the controls
and wite or get some properties based on the controls.
This is the following piece of code in Visual Basic 6.0

HTML:
For Each ctl In frm.Controls
sCtlType = TypeName(ctl)
If sCtlType = "Label" Then
ctl.Caption = objLocalizer.GetResourceString(CInt(ctl.Tag))
ElseIf sCtlType = "CommandButton" Then
nVal = 0
nVal = Val(ctl.Tag)
ElseIf sCtlType = "Menu" Then
ctl.Caption =
objLocalizer.GetResourceString(CInt(ctl.Caption))
ElseIf sCtlType = "TabStrip" Then
For Each obj In ctl.Tabs
obj.Caption =
objLocalizer.GetResourceString(CInt(obj.Tag))
obj.ToolTipText =
objLocalizer.GetResourceString(CInt(obj.ToolTipText))
Next
ElseIf sCtlType = "Toolbar" Then
For Each obj In ctl.Buttons
obj.ToolTipText =
objLocalizer.GetResourceString(CInt(obj.ToolTipText))
Next
ElseIf sCtlType = "ListView" Then
For Each obj In ctl.ColumnHeaders
obj.Text =
objLocalizer.GetResourceString(CInt(obj.Tag))
Next
ElseIf sCtlType = "ctList" Then
ctl.HeaderFont = fnt
Count = ctl.ColumnCount
For Index = 1 To Count
ctl.ColumnText(Index) =
objLocalizer.GetResourceString(CInt(ctl.ColumnText(Index)))
Debug.Print ctl.ColumnText(Index)
Next Index
ElseIf sCtlType = "SSTab" Then
For Index = 0 To ctl.Tabs
ctl.TabCaption(Index) =
objLocalizer.GetResourceString(CInt(ctl.TabCaption(Index)))
Next Index
ElseIf sCtlType = "ActiveBar" Then
Dim i As Integer
Dim J As Integer
Dim BandCount As Integer
Dim ToolsCount As Integer
BandCount = ctl.Bands.Count
For i = 0 To BandCount
ToolsCount = ctl.Bands(Index).Tools.Count
For J = 0 To ToolsCount
nVal = 0
nVal = Val(ctl.Bands(i).Tools(J).Tag)
If nVal > 0 Then
ctl.Bands(i).Tools(J).Caption =
objLocalizer.GetResourceString(nVal)
End If
nVal = 0
nVal = Val(ctl.Bands(i).Tools(J).ToolTipText)
If nVal > 0 Then
ctl.Bands(i).Tools(J).ToolTipText =
objLocalizer.GetResourceString(nVal)
End If
Next J
Next i
Else
nVal = 0
nVal = Val(ctl.Tag)
If nVal > 0 Then ctl.Caption =
objLocalizer.GetResourceString(nVal)
nVal = 0
nVal = Val(ctl.ToolTipText)
If nVal > 0 Then ctl.ToolTipText =
objLocalizer.GetResourceString(nVal)
End If
Next

When the same code is to be converted to Visual Basic 2005, i would
like to know how to access
1. ctl.HeaderFont in case of "ctList"
2. ctl.ColumnCount in case of "ctList"
3. ctl.ColumnText(Index) in case of "ctList"
4. ctl.Tabs in case of ctl is a SSTab
5. ctl.TabCaption(Index) in case of a Tab in SSTab
Any Help in this regard would be appreciated

Thanks in advance,
Sugan
 
this is easy, just write that code in the .net form:
VB.NET:
For Each ctl As Control In Me.Controls
  If TypeOf(ctl) Is Label Then
    'Code for if it's a label
  End If
  If TypeOf(ctl) Is Button Then
    'Code for button
  End If
Next ctl
so on and so forth

to access other properties like HeaderFont, use the ctl variable but only try to access the HeaderFont after checking to see if it's of type "ctList", whatever ctList is...
 
i'd like to add/improve a bit to juggalo code:

VB.NET:
Dim tmpBtn as Button
Dim tmpLst as ListBox
Dim tmpLbl As Label
etc..
 
For Each ctl As Control In Me.Controls
  If TypeOf(ctl) Is Label Then
    tmpLbl = DirectCast(ctl, Label)
    tmpLbl.Text = "hello"
    tmpLbl.AutoSize = True
    tmpLbl.LabelSpecificProperty = Value
  Else If TypeOf(ctl) Is Button Then
    'Code for button
  End If
Next ctl

notes: make a temp variable for each type you will deal with. once you know the TypeOf ctl to be that type (button type or label type) you can cast it. this will mean you can get auto complete help in the editor etc, and uses the feature of type safety of VB.net

and other improvement is use of Else If because this:

If...
Endif
If...
Endif

in this case if it is type button then it cannot be of type label, so theres no use testing for it.. hence we use: if elseif elseif elseif end if

this way after finding an IF that is true, we dont evaluate the rest of the ifs
 
i've never been a fan of select case, any more than i'm a fan of the for loop syntax of vb. select case is far more limited in scope and, given that it is converted to the same IL as if endif endif else, i'd say its a high level code manipulation facility/simplification..

I'd love to know though, JB, how exactly you would use a select case in this situation? Select Case <statement> requires that statement be some primitive type comparable with =
as far as im aware it cannot be used with syntax such as null or type conversions:

VB.NET:
Select Case TypeOf(ctl) 'error here
  Case Button 'error here
  Case Is Label 'error here too


the other thing that i dont find great about select case is that they can potentiall;y be pages long, and its easy to forget what youre actually comparing 2 pages later.. if elseif on the other hand, are far more readable in this regard as they repeatedly state their intent. it may be a bit mor typing, but i'll probably always advocate If over Select ;)
 
hmmm, that is a good point, usually select case works in place of elseif's but there are other cases where select case doesnt work (for whatever reason)

i thought this would work:
VB.NET:
        For Each ctl As Control In Me.Controls
            Select Case TypeOf(ctl)
                Case Is Label

                Case Is Button
            End Select
        Next ctl
but it doesnt because of how TypeOf works
 
you'd probably have to do something a bit lame like:

Select Case ctl.getType.Name
Case "Button"
...



uuuuuggghhhhh this is wrong for so many reasons, i cant even believe i'm writing it on an internet forum..
 
JuggaloBrotha said:
hmmm, that is a good point, usually select case works in place of elseif's but there are other cases where select case doesnt work (for whatever reason)

i thought this would work:
VB.NET:
        For Each ctl As Control In Me.Controls
            Select Case TypeOf(ctl)
                Case Is Label
 
                Case Is Button
            End Select
        Next ctl
but it doesnt because of how TypeOf works

it's more a consequence of how select case works.. it evaluates the expression and stores is in an object, then compares each case to the object. because typeof is a compound statement that requires an IS, it doesnt produce a result on its own..

You can, of course, say:

Select Case TypeOf(ctl) is Button
Case True
Case False


but its a bit limited

and of cpourse you can additionally say (this is trick, but pointless)

VB.NET:
Select Case _
  (TypeOf(ctl) Is Button And 1) Or _
  (TypeOf(ctl) Is Label And 2) Or _
  (TypeOf(ctl) Is TextBox And 4) Or _
  (TypeOf(ctl) Is ListBox And 8) 
Case 0
  'it is none of the above
Case 1 
  'it is a button
Case 2
  'it is a label
Case 3
  'it is a button and a label
Case 4
  'it is a textbox
'5 = tb and btn
'6 = lbl + tbx
'7 = lbl + tbx + btn

of course, some might not make sense.. like case 3,5,6,7 etc.. but it's trick, no?
 
Back
Top