Link to home
Start Free TrialLog in
Avatar of pharries
pharries

asked on

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
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

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
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
Avatar of pharries
pharries

ASKER

I was trying to build a list of the checked CheckBoxes text into a long string
thanks
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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