none
how to use button to pop up msgbox when 2 or more items check on checklistbox RRS feed

  • Question

  • im just starting to learn vb and i dont familiar with coding so i get so much trouble to understand it.

    for example, there is 3 items to check. if the fist and the second were checked then msgbox pop up and says 1 and 2. how can i do that??
    thanks
    sorry for my bad english


    Tuesday, October 10, 2017 6:55 AM

All replies

  • for example, there is 3 items to check. if the fist and the second were checked then msgbox pop up and says 1 and 2.

    What are the 'items' that are checked?  If they are checkboxes on a form, then you would use code like

        If CheckBox1.Checked AndAlso CheckBox2.Checked Then
            MsgBox("1 and 2")
        End If

    but it's not clear where you would want that code to be inserted.  For instance, should the check occur when the user checks or unchecks a checkbox, or should it occur at some other point in the processing? And does it matter whether or not 3 is also checked?

        Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles _
            CheckBox1.CheckedChanged, _
            CheckBox2.CheckedChanged, _
            CheckBox3.CheckedChanged
            If CheckBox1.Checked AndAlso CheckBox2.Checked AndAlso Not CheckBox3.Checked Then
                MsgBox("1 and 2")
            End If
        End Sub

    If you are referring to a CheckdListBox rather than a list of check boxes then the equivalent code might be something like:

    Private Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged
        If CheckedListBox1.CheckedIndices.Contains(0) AndAlso CheckedListBox1.CheckedIndices.Contains(1) Then
            MsgBox("1 and 2")
        End If
    End Sub

    For a useful response you should add a lot more detail to your question.

    • Edited by Acamar Tuesday, October 10, 2017 7:26 AM
    Tuesday, October 10, 2017 7:17 AM
  • Hi Priekiew,

    Do you want to get this?

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If CheckedListBox1.CheckedItems.Count >= 2 Then
                ' If so, loop through all checked items and print results.  
                Dim x As Integer
                Dim s As String = ""
                For x = 0 To CheckedListBox1.CheckedItems.Count - 1
                    s = s & "Checked Item " & (x + 1).ToString & " = " & CheckedListBox1.CheckedItems(x).ToString & ControlChars.CrLf
                Next x
                MessageBox.Show(s)
            End If
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Friday, October 13, 2017 7:24 AM
    Tuesday, October 10, 2017 8:00 AM
    Moderator
  • Here is a start (got to leave for work or I'd finish it).

    Public Class Form1
        Private maxNumberOfCheckedItems As Integer = 2
        Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
            Dim items As CheckedListBox = CType(sender, CheckedListBox)
            If items.CheckedItems.Count = (maxNumberOfCheckedItems - 1) Then
                MessageBox.Show("Two items checked")
            ElseIf items.CheckedItems.Count > (maxNumberOfCheckedItems - 1) Then
                e.NewValue = CheckState.Unchecked
            End If
        End Sub
    End Class
    Public Module Extensions
        <Runtime.CompilerServices.Extension>
        Public Function CheckedItems(ByVal sender As CheckedListBox) As String()
            Return sender.CheckedItemsList().ToArray()
        End Function
        <Runtime.CompilerServices.Extension>
        Public Function CheckedItemsList(ByVal sender As CheckedListBox) As List(Of String)
            Dim Result = From T In sender.Items.OfType(Of String)().Where(Function(item, index) sender.GetItemChecked(index))
                         Select T
            Return Result.ToList()
        End Function
    End Module
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, October 10, 2017 1:09 PM
    Moderator
  • Sorry for the late reply, been a busy day. So in this example I loaded the CheckListBox with day names, checked today. The code logic will popup a MessageBox when two items are checked (I believe your example of 1 and 2 are just an example but may be wrong).

    The code module code could easily be inline with the code as shown in the second example but I think it's nice to have a method such as in the module for use in other projects (the code module would be in it's own .vb file).

    Public Class Form1
        Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
            If e.NewValue = CheckState.Checked AndAlso CType(sender, CheckedListBox).CheckedItems.Count = 1 Then
                MessageBox.Show($"{CheckedListBox1.Items(e.Index)}{Environment.NewLine}{CheckedListBox1.CheckedItemsList.First}")
            End If
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            CheckedListBox1.DataSource = Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames.ToList
            CheckedListBox1.SetItemChecked(Now.DayOfWeek + 1, True)
        End Sub
    End Class
    Public Module Extensions
        <Runtime.CompilerServices.Extension>
        Public Function CheckedItemsList(ByVal sender As CheckedListBox) As List(Of String)
            Return sender.Items.OfType(Of String)() _
                .Where(Function(item, index) sender.GetItemChecked(index)) _
                .Select(Function(item) item) _
                .ToList
        End Function
    End Module
    

    Without code module

    Public Class Form1
        Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
            If e.NewValue = CheckState.Checked AndAlso CType(sender, CheckedListBox).CheckedItems.Count = 1 Then
                MessageBox.Show($"{CheckedListBox1.Items(e.Index)}{Environment.NewLine}{CheckedListBox1.Items.OfType(Of String)().Where(Function(item, index) CheckedListBox1.GetItemChecked(index)).Select(Function(item) item).First}")
            End If
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            CheckedListBox1.DataSource = Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames.ToList
            CheckedListBox1.SetItemChecked(Now.DayOfWeek + 1, True)
        End Sub
    End Class

    Caveat, in the above examples, both, the MessageBox appears prior to the second item being checked so it may be a tad confusing to the user so I did an example with a label as it's clear to what's going on.

    Public Class Form1
        Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
            If e.NewValue = CheckState.Checked AndAlso CType(sender, CheckedListBox).CheckedItems.Count = 1 Then
                Label1.Text = $"{CheckedListBox1.Items(e.Index)},{CheckedListBox1.Items.OfType(Of String)().Where(Function(item, index) CheckedListBox1.GetItemChecked(index)).Select(Function(item) item).First}"
            Else
                Label1.Text = ""
            End If
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            CheckedListBox1.DataSource = Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames.ToList
            CheckedListBox1.SetItemChecked(Now.DayOfWeek + 1, True)
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, October 11, 2017 12:00 AM
    Moderator