VB中防止将重复项目添加到combobox控件中?

解决方案 »

  1.   

    '如果数据量不是特别大的话,可以这可判断
        '如果 组合框中已经有值的话,先取得值列表放在变量中
        
        Dim i As Integer
        Dim strText As String   '存放已经加入的项,每项用逗号(,)分隔,如果项中可能含有逗号,可改用其他分隔
        
        Combo1.Clear
        strText = ","
        
        For i = 1 To 20
            If InStr(strText, "," & "text" & CStr(i) & ",") = 0 Then
            
                Combo1.AddItem "text" & CStr(i)
                strText = strText & "text" & CStr(i) & ","
                
            End If
        
            If InStr(strText, "," & "text" & CStr(i) & ",") = 0 Then
            
                Combo1.AddItem "text" & CStr(i)
                strText = strText & "text" & CStr(i) & ","
                
            End If
        Next