Question controlname (checkbox) from variable ?

Super_Grover

Active member
Joined
Apr 7, 2010
Messages
32
Programming Experience
Beginner
Hi al!

I've been lurking here for some time and finally registered en now posting.
This is my first question which I can't find the answer for. Sorry in advance if this is too easy to ask or too n00b...

Anyway, I'd like to refer to 12 checkboxes on my form, "CBOX_mnth1....CBOX_mnth12", by a variable. So I'd like to loop through 1..12 and assigning the number after the text "CBOX_mnth". This together would make the controlname, like CBOX_mnth7 in case of x=7.
I hope this is a bit understandable, for Englisch is not my native language.

I wanna do something like this:
(the {..} are from another language and used to assign a variable as controlname, which is axactly what I'm after in VB.net)

Dim ls_month As String
For x = 1 to 12
ls_month = "CBOX_mnth" & Convert.ToString(x)
IF {ls_month}.checked = true then
'do something
End if
END


Thanks in advance for your help!
 

Budius

Well-known member
Joined
Aug 6, 2010
Messages
137
Location
UK
Programming Experience
3-5
a direct loop as a string, like you're proposing is not possible, but you can easily use a collection and loop through its members I've done this before, I'll post my code that will luckily give you some ideas on how to:
VB.NET:
Public Class Main
    Private Inputs As New Collection

Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Create the Digital Inputs
        Inputs.Add(DI1)
        Inputs.Add(DI2)
        Inputs.Add(DI3)
        Inputs.Add(DI4)
        Inputs.Add(DI5)
        Inputs.Add(DI6)
        Inputs.Add(DI7)
        Inputs.Add(DI8)
        Inputs.Add(DI9)
        Inputs.Add(DI10)
        Inputs.Add(DI11)
        Inputs.Add(DI12)
        Inputs.Add(DI13)
        Inputs.Add(DI14)
        Inputs.Add(DI15)
        Inputs.Add(DI16)

End Sub

' And now I can easily loop through them

VB.NET:
Private Sub UpdateScreen(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles CommsReader.ProgressChanged
                ' Drawings Color
                ' ========================================
                For i = 1 To 16             ' Inputs
                    If r.BitwiseAnd(WTU.Inputs, i - 1) And Not Invert(i) _
                    Or Not r.BitwiseAnd(WTU.Inputs, i - 1) And Invert(i) Then
                        Inputs.Item(i).FillColor = Color.FromName(Inputs.Item(i).Tag)
                    Else
                        Inputs.Item(i).FillColor = Color.Gray
                    End If
                Next i
End Sub
End Class

and in case you want to use events from those guys you can do this on the form load:
VB.NET:
        ' Attached the Comands .click handler
        AddHandler DO1Cmd.Click, AddressOf OutputsClick
        AddHandler DO2Cmd.Click, AddressOf OutputsClick
        AddHandler DO3Cmd.Click, AddressOf OutputsClick
        AddHandler DO4Cmd.Click, AddressOf OutputsClick
        AddHandler DO5Cmd.Click, AddressOf OutputsClick
        AddHandler DO6Cmd.Click, AddressOf OutputsClick
        AddHandler DO7Cmd.Click, AddressOf OutputsClick
        AddHandler DO8Cmd.Click, AddressOf OutputsClick
        AddHandler DO9Cmd.Click, AddressOf OutputsClick
        AddHandler DO10Cmd.Click, AddressOf OutputsClick
        AddHandler DO11Cmd.Click, AddressOf OutputsClick
        AddHandler DO12Cmd.Click, AddressOf OutputsClick
        AddHandler DO13Cmd.Click, AddressOf OutputsClick
        AddHandler DO14Cmd.Click, AddressOf OutputsClick
        AddHandler DO15Cmd.Click, AddressOf OutputsClick
        AddHandler DO16Cmd.Click, AddressOf OutputsClick
        AddHandler DO17Cmd.Click, AddressOf OutputsClick
        AddHandler DO18Cmd.Click, AddressOf OutputsClick
and have the OutputsClick routine:
VB.NET:
Sub OutputsClick(ByVal sender As Object, ByVal e As System.EventArgs)
   ' you can work it out with the sender.name

End Sub
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,469
Location
Norway
Programming Experience
10+
IF {ls_month}.checked = true then
VB.NET:
IF CType(Me.Controls(ls_month), CheckBox).Checked Then

If reacting to a specific CheckBox being checked is the goal, then you should instead use the CheckedChanged event, you can use the same event handler for all checkboxes and identify the control from event 'sender' parameter.
 

Super_Grover

Active member
Joined
Apr 7, 2010
Messages
32
Programming Experience
Beginner
Thanks very much guys!
Tonight I'll be able to work on it I hope and give both options a try.
I'll let you know how it turns out.
 

Super_Grover

Active member
Joined
Apr 7, 2010
Messages
32
Programming Experience
Beginner
Sorry guys, I can't get it working...

For x = 1 To 12
ls_monthname = "CheckBox" & Convert.ToString(x)
If CType(Me.Controls(ls_monthname), CheckBox).CheckState = CheckState.Checked Then
MessageBox.Show(x & " " & ls_monthname)
End If
Next


What am I doing wrong?
 

Super_Grover

Active member
Joined
Apr 7, 2010
Messages
32
Programming Experience
Beginner
On this line:

If CType(Me.Controls(ls_monthname), CheckBox).CheckState = CheckState.Checked Then

i get an "NullReferenceExeption was unhandled" error. I need to create an object instance? Of what, and how?

Sorry again for the n00b question but thanks in advance!
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,469
Location
Norway
Programming Experience
10+
It means that in Me.Controls collection there is no control named ls_monthname. So either is ls_monthname wrong, or the control is in a different Controls collection (ie a different container).
 

Super_Grover

Active member
Joined
Apr 7, 2010
Messages
32
Programming Experience
Beginner
Okay, thanks very much for all the help guys!
Too bad I couldn't get it working no matter what I tried. I will continue trying later on, but for nod I had to move on and get this appl working. So I coded it the long way, which meant 12 times as much code but that's fine for now. I'll work on the elegance later with your input.
For now I'm struggling with two more probs involving database access. I'll start a new thread for those probs.
 

Budius

Well-known member
Joined
Aug 6, 2010
Messages
137
Location
UK
Programming Experience
3-5
typing exact as JohnH said:
VB.NET:
IF CType(Me.Controls(ls_month), CheckBox).Checked Then
I managed it to work instantly in a simple 1 form 1 checkbox context.
Maybe you're using this in a module and not in the form so you have to address FormName.Controls(____)

And anyway, the original way I presented (using a collection) you still have a nice effective way to use less code, I reckon you should give it a try.
 

Super_Grover

Active member
Joined
Apr 7, 2010
Messages
32
Programming Experience
Beginner
Thanks Bodius,
I really want to get this working. Chances are I got some free time tonight. Then I'll give it another go if I can't continue working on the the other issues (see other thread).
I'll let you know.
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
Another alternative is to use a control as your collection. I propose using one of the container controls (groupbox, panel, etc). These controls will have a Controls property which of course is a collection of the controls contained in the container control. Similar to the collection method suggested by Budius, but instead of adding to a collection via code, you add to the collection via the designer.
 
Top Bottom