Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Looping through controls on a form


Reply
 
Thread Tools Display Modes
  #1  
Old 08-11-2012, 11:16 AM
celtic celtic is offline
Centurion
 
Join Date: Mar 2009
Posts: 129
Default Looping through controls on a form


Hi all.

I'm trying to get the tag associated with checkboxes on a form.
So I am using a recursion routine to loop through all of the controls on the form.

(I would have used a function and returned a List(Of string) but it seems that any local variable in the routine is re-dimensioned with each recursive call)

What I'm specifically having trouble with is the statement: If ctrl.checked = True Then...
(in subroutine getRoutinesToRun).

The compiler complains that .checked is not part of control.
I thought this should work since the test is after the TypeOf ctrl has already been decided to be a checkbox.

So, I'm not sure how to fix it, and confused as to why it doesn't work since the control at the decision point is supposed to be a checkbox.

So I'm looking at how to fix it, and why it isn't working as is.

Thank you all in advance.

Code:
Friend routinesToRun = New List(Of String)

Private Sub btnReportRun_Click(sender As System.Object, e As System.EventArgs) Handles btnReportRun.Click
        ' Get which reports to run
        'routinesToRun is a list(of string) declared at the declarations level of the form 
        'since the recursion routine re-dimensions variables with each recursive call, otherwise
        'I would have used a function and returned a List(Of String)

        routinesToRun.Clear() 
        getRoutinesToRun(Me)

    End Sub

    Private Sub getRoutinesToRun(ByVal Page As Control)
        ' Trying to get the tag associated with checkboxes on a form 
        ' if the checkbox is checked

        For Each ctrl As Control In Page.Controls
            If TypeOf ctrl Is CheckBox Then
                If ctrl.checked = True Then
                    routinesToRun.Add(CType(ctrl, CheckBox).Tag)
                End If
            Else
                If ctrl.Controls.Count > 0 Then
                    getRoutinesToRun(ctrl)
                End If
            End If
        Next

    End Sub
Reply With Quote
  #2  
Old 08-11-2012, 01:53 PM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

All the TypeOf check does is say "Hey, is this thing a CheckBox?" It doesn't actually tell the compiler to treat it like a checkbox. To do that, you have to cast. In this case, it's probably best to use TryCast to do both the check and cast at the same time:
Code:
Dim checkbox As CheckBox = TryCast(ctrl, CheckBox)
If checkbox IsNot Nothing Then
    ' It is a checkbox
Else
    ' It is not
End If
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 08-11-2012, 03:51 PM
celtic celtic is offline
Centurion
 
Join Date: Mar 2009
Posts: 129
Default

Aah! OK.

Thank you so much !
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->