none
How to select items in Checkedlistbox that include some specified characters using VB.NET? RRS feed

  • Question

  • I want to make a feature for my application, which is after the user specified some characters in a textbox, it automatically selects all items in a checked list box that include those characters. Does anyone know how to accomplish this? Many thanks!


    Tuesday, July 31, 2018 2:34 AM

All replies

  • When you say, "it automatically selects all items in a checked list box that include those characters" do you mean any occurrence of those characters, those characters in the same order but not necessarily in sequence, or exactly those characters in sequence?

    You are going to loop through the items in the CheckedListBox and compare them to the characters in the textbox.  The question is what that comparison should look like...


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, July 31, 2018 2:50 AM
    Moderator
  • The following is a good start if not a solution.

    Code module for language extension method

    Public Module Extensions
        ''' <summary>
        ''' Find a item in a CheckedListBox via Contains. This could be tweaked to
        ''' do starts-with, equals, ends-with along with contains or have several overloads
        ''' of this method.
        ''' </summary>
        ''' <param name="sender">CheckedListBox</param>
        ''' <param name="pText">Text to locate via contains</param>
        ''' <param name="pChecked">True to check, False to un-check if found</param>
        <Runtime.CompilerServices.Extension>
        Public Sub FindItemAndSetChecked(sender As CheckedListBox, pText As String, pChecked As Boolean)
    
            Dim result = (
                    From this In sender.Items.Cast(Of String)().Select(Function(item, index) New With
                        {
                            Key .Item = item,
                            Key .Index = index
                        })
                    Where this.Item.ToLower().Contains(pText.ToLower())
                    Select this).FirstOrDefault()
    
            ' if found check or un-check as per pChecked
            If result IsNot Nothing Then
                sender.SetItemChecked(result.Index, pChecked)
            End If
        End Sub
    End Module

    Usage where it finds Karen and checks it

    CheckedListBox1.FindItemAndSetChecked("kar", True)

    Usage which fails because of no matches.

    CheckedListBox1.FindItemAndSetChecked("kao", True)

    On failure nothing happens. If you need to know then tweak it into a function and return True or False in the If statement.

    Finally, if you need to do multiple items, use a loop and iterate via the extension method shown above.

    EDIT: Here is the original and a all version.

    Public Module Extensions
        ''' <summary>
        ''' Find a item in a CheckedListBox via Contains. This could be tweaked to
        ''' do starts-with, equals, ends-with along with contains or have several overloads
        ''' of this method.
        ''' </summary>
        ''' <param name="sender">CheckedListBox</param>
        ''' <param name="pText">Text to locate via contains</param>
        ''' <param name="pChecked">True to check, False to un-check if found</param>
        <Runtime.CompilerServices.Extension>
        Public Sub FindItemAndSetChecked(sender As CheckedListBox, pText As String, pChecked As Boolean)
    
            Dim result = (
                    From this In sender.Items.Cast(Of String)().Select(Function(item, index) New With
                        {
                            Key .Item = item,
                            Key .Index = index
                        })
                    Where this.Item.ToLower().Contains(pText.ToLower())
                    Select this).FirstOrDefault()
    
            ' if found check or un-check as per pChecked
            If result IsNot Nothing Then
                sender.SetItemChecked(result.Index, pChecked)
            End If
        End Sub
        <Runtime.CompilerServices.Extension>
        Public Sub FindAllAndSetChecked(sender As CheckedListBox, pText As String, pChecked As Boolean)
    
            Dim result = (
                    From this In sender.Items.Cast(Of String)().Select(Function(item, index) New With
                                                                          {
                                                                          Key .Item = item,
                                                                          Key .Index = index
                                                                          })
                    Where this.Item.ToLower().Contains(pText.ToLower())
                    Select this).ToList
    
            For Each Item In result
                sender.SetItemChecked(Item.Index, pChecked)
            Next
        End Sub
    End Module

    Usage

    CheckedListBox1.FindAllAndSetChecked("ar", True)


    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, July 31, 2018 2:53 AM
    Moderator
  • I mean I want it to automatically select any items in the checked list box that include the specified characters in the same order
    Tuesday, July 31, 2018 3:08 AM
  • Hi

    Is this the method you need, enter a character in the TextBox and select it in the Checkedlistbox?

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            CheckedListBox1.Items.Add("ab")
            CheckedListBox1.Items.Add("bc")
            CheckedListBox1.Items.Add("cd")
            CheckedListBox1.Items.Add("de")
        End Sub
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            For i As Integer = 0 To CheckedListBox1.Items.Count - 1
                If CheckedListBox1.Items(i).ToString().Contains(TextBox1.Text) Then
                    CheckedListBox1.SetItemChecked(i, True)
                End If
            Next
        End Sub
    End Class

    Best Regards,

    Alex


    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.

    Tuesday, July 31, 2018 3:33 AM
  • Yeah, that looks like what I need. I'll try to see if it works, Thanks!
    Tuesday, July 31, 2018 4:05 AM
  • Hi

    Do you resolve the issue? If you resolve the issue, could you please mark the helpful as answer. it will be beneficial to other community.

    Best Regards,

    Alex


    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.

    Friday, August 10, 2018 5:42 AM