Solved

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

Posted on 2004-10-14
7
176 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
  • 4
7 Comments
 
LVL 85

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 85

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 85

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 85

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

839 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