Results 1 to 2 of 2

Thread: Loop to check what checkboxes are selected

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    1

    Loop to check what checkboxes are selected

    Hi there,

    First of all i wish to apologise for what you might find any easy question, i am a beginner programmer, but i thank you for your time and guidance in advance.

    I have a input form that has a number of check boxes on which relate the users enquiring courses. I need to create a loop of some sort that allows each checkbox to be checked it is selected. If so, output that within a message box. Thats it put simply anyway.

    I have removed a lot of the code that is not related to this query but my code at the bottom is the message box that i wish to output all details in.

    Currently in the message box it displays the number of checkboxes selected but i just need to link it to the related text and entered where it is highlighted in RED. I hope this undetailed and badly explained situation is enough for your understanding. Any guidance on this will be greatly appreciated. Thanks

    ************Current Code***********

    Private Sub ButtonSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSubmit.Click
    Dim VarEnquiredCourses As Integer
    Dim Wrap As String
    Dim AllCheckBoxes As CheckBox() = {CheckBoxVB10, CheckBoxJava, CheckBoxDreamweaver, CheckBoxCIW, CheckBoxCompMain, CheckBoxSage, CheckBoxPBook, CheckBoxMSOffice}
    Dim Chk As CheckBox

    'The Enquiring Courses Checkboxes

    CheckBoxVB10.Text = "Visual Basic 10 "
    CheckBoxJava.Text = "Java "
    CheckBoxDreamweaver.Text = "Dreamweaver "
    CheckBoxCIW.Text = "CIW "
    CheckBoxCompMain.Text = "Computer Maintenance "
    CheckBoxSage.Text = "Sage "
    CheckBoxPBook.Text = "Practical Bookeeping "
    CheckBoxMSOffice.Text = "Microsoft Office "

    For Each Chk In AllCheckBoxes

    If Chk.CheckState = 1 Then

    VarEnquiredCourses = VarEnquiredCourses + 1

    End If

    Next

    MsgBox(FirstName & " " & LastName & " would like to know details on the following courses" _
    & Wrap & Wrap & VarEnquiredCourses & Wrap & Wrap & Wrap & "He is " & Age & " years old and" & AnOrA & ComboBoxCompExp.SelectedItem & _
    " level computer user and wishes to start studying in " & MonthName(DatePickerOfStudy.Value.Month.ToString) & _
    " " & Year(DatePickerOfStudy.Text) & Wrap & Wrap & _
    "Our Courier Costs will be: £", vbOKOnly, "Request for course brochures")

    End Sub

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Loop to check what checkboxes are selected

    The following works for VS2008 +

    Code:
    Dim Checked = (From x In AllCheckBoxes Where x.Checked _
                   Select Course = x.Text).ToArray
    For a nice display

    Code:
    If Checked IsNot Nothing Then
        MsgBox(String.Format("You selected {0} courses as listed {1}", _
                             Checked.Count, _
                             Checked.CreateStringList(", ", " and ")))
    Else
        MsgBox("No course selected")
    End If
    Place the following in a code module
    Code:
    <System.Diagnostics.DebuggerStepThrough()> _
    <Runtime.CompilerServices.Extension()> _
    Function CreateStringList(ByVal items() As String, ByVal separator As String, ByVal lastSeparator As String) As String
        Dim Result As String = String.Join(separator, items)
        Dim Position As Integer = Result.LastIndexOf(separator)
        If Position > -1 Then
            Result = Result.Remove(Position, separator.Length)
            Result = Result.Insert(Position, lastSeparator)
        End If
        Return Result
    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width