Solved

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

Posted on 2004-10-14
7
172 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
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…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now