Solved

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

Posted on 2004-10-14
7
178 Views
Last Modified: 2013-11-18
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
Comment
Question by:pharries
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
7 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12315192
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12315837
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
 

Author Comment

by:pharries
ID: 12316017
I was trying to build a list of the checked CheckBoxes text into a long string
thanks
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 100 total points
ID: 12316227
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12316251
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question