• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 182
  • Last Modified:

Easy points, using a variable in checkbox(VAR).text

I have to gather the data from a buch of check boxes into one variable.
I have tried doing this several ways.
What do I need to change to get it to work?
I know that this is a complete mess but i just cant work it out
Thanks
  ____________________________
  Sub GatherLevels()
        Dim n As Integer
        Dim nbox As String
        Dim Var_Levels as Sting
        n = 1
        For n = 1 To 115
           nbox = "Me.checkbox" & n
           If nbox & .checked  = True Then
       
                Var_Levels = nbox & .Text & 
            End If
            n = n + 1
        Next


    End Sub
0
pharries
Asked:
pharries
  • 4
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Are you trying to determine how many of the checkboxes are checked and store that total in Var_Levels?

...or are you trying to build a list of the checked CheckBoxes text into a long string?

~IM
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Shown below are two techniques you can use to enumerate all of the Checkboxes and determine how many of them are checked.  

The first method in Button1 uses a helper function getControlByName() to find the control matching the name passed.  It mirrors what you attempting to do with your original code.

The second method in Button2 enumerates all the controls on the form and determines which ones are CheckBoxes using the TypeOf function.  This method is usefull if you want to include ALL of the checkboxes in your form in the count.

Regards,

Idle_Mind

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        Dim c As Control
        Dim cb As CheckBox
        Dim checkedItems As Integer
        Dim controlName As String

        For i = 1 To 115
            controlName = "CheckBox" & i
            c = getControlByName(controlName)
            If Not (c Is Nothing) Then
                Try
                    cb = CType(c, CheckBox)
                    If cb.Checked Then
                        checkedItems = checkedItems + 1
                    End If
                Catch ex As Exception
                    MessageBox.Show(ex.Message, "Error Converting Control", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End Try
            Else
                MessageBox.Show("Control Name: " & controlName, "Control Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Next
        MessageBox.Show("Number of Items Checked: " & checkedItems, "Checked Item Total", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Function getControlByName(ByVal controlName As String) As Control
        controlName = controlName.ToLower
        Dim c As Control
        For Each c In Me.Controls
            If c.Name.ToLower.Equals(controlName) Then
                Return c
            End If
        Next
    End Function

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim c As Control
        Dim cb As CheckBox
        Dim checkedItems As Integer

        For Each c In Me.Controls
            If TypeOf c Is CheckBox Then
                cb = CType(c, CheckBox)
                If cb.Checked Then
                    checkedItems = checkedItems + 1
                End If
            End If
        Next
        MessageBox.Show("Number of Items Checked: " & checkedItems, "Checked Item Total", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

End Class
0
 
pharriesAuthor Commented:
I was trying to build a list of the checked CheckBoxes text into a long string
thanks
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here are both methods again but this time putting the checked items text together seperated by spaces:

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        Dim c As Control
        Dim cb As CheckBox
        Dim checkedItems As String
        Dim controlName As String

        For i = 1 To 115
            controlName = "CheckBox" & i
            c = getControlByName(controlName)
            If Not (c Is Nothing) Then
                Try
                    cb = CType(c, CheckBox)
                    If cb.Checked Then
                        If checkedItems = "" Then
                            checkedItems = cb.Text
                        Else
                            checkedItems = checkedItems & " " & cb.Text
                        End If
                    End If
                Catch ex As Exception
                    MessageBox.Show(ex.Message, "Error Converting Control", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End Try
            Else
                MessageBox.Show("Control Name: " & controlName, "Control Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Next
        MessageBox.Show(checkedItems, "Checked Items", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Function getControlByName(ByVal controlName As String) As Control
        controlName = controlName.ToLower
        Dim c As Control
        For Each c In Me.Controls
            If c.Name.ToLower.Equals(controlName) Then
                Return c
            End If
        Next
    End Function

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim c As Control
        Dim cb As CheckBox
        Dim checkedItems As String

        For Each c In Me.Controls
            If TypeOf c Is CheckBox Then
                cb = CType(c, CheckBox)
                If cb.Checked Then
                    If checkedItems = "" Then
                        checkedItems = cb.Text
                    Else
                        checkedItems = checkedItems & " " & cb.Text
                    End If
                End If
            End If
        Next
        MessageBox.Show(checkedItems, "Checked Items", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

End Class
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here's another unconventional way:

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private checkedItems As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim c As Control
        Dim cb As CheckBox

        For Each c In Me.Controls
            If TypeOf c Is CheckBox Then
                cb = CType(c, CheckBox)
                AddHandler cb.CheckStateChanged, AddressOf Me.cb_CheckStateChanged
            End If
        Next
    End Sub

    Private Sub cb_CheckStateChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim cb As CheckBox = CType(sender, CheckBox)
        If cb.Checked Then
            If checkedItems = "" Then
                checkedItems = cb.Text
            Else
                checkedItems = checkedItems & " " & cb.Text
            End If
        Else
            checkedItems = Replace(checkedItems, cb.Text, "").Replace("  ", " ").Trim
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MessageBox.Show(checkedItems, "Checked Items", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

End Class
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now