none
VB.NET checkedlistbox控件如何选择含有指定字符的选项 RRS feed

  • 问题

  • 我想用VB.NET实现一个功能,用户在某个textbox里面可以输入一些指定的字符,然后按下确定后自动将checkedlistbox中含有该字段的选项自动选中,请问这个如何实现呢?
    2018年7月31日 2:27

答案

  • 你好,

    如果是winforms 的话,你可以参考下面的代码:

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    
            For Each i In CheckedListBox1.CheckedIndices
                CheckedListBox1.SetItemCheckState(i, CheckState.Unchecked)
            Next
            Dim s As String = TextBox1.Text
           
    
            For index = 0 To CheckedListBox1.Items.Count - 1
                If Not String.IsNullOrEmpty(s) AndAlso CheckedListBox1.Items(index).ToString().Contains(s) Then
                    CheckedListBox1.SetItemChecked(index, True)
                End If
            Next
    
    
        End Sub

    Best regards,

    Zhanglong


    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.


    2018年7月31日 7:43
    版主

全部回复

  • 你好,

    如果是winforms 的话,你可以参考下面的代码:

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    
            For Each i In CheckedListBox1.CheckedIndices
                CheckedListBox1.SetItemCheckState(i, CheckState.Unchecked)
            Next
            Dim s As String = TextBox1.Text
           
    
            For index = 0 To CheckedListBox1.Items.Count - 1
                If Not String.IsNullOrEmpty(s) AndAlso CheckedListBox1.Items(index).ToString().Contains(s) Then
                    CheckedListBox1.SetItemChecked(index, True)
                End If
            Next
    
    
        End Sub

    Best regards,

    Zhanglong


    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.


    2018年7月31日 7:43
    版主
  • Thx, working perfectly
    2018年8月1日 4:48