CodeItBetter Programming Another VB Programming Blog

How to check for duplicates in Combo Box

Posted on January 5, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
'Controls - How to check for duplicates in Combo Box
Option Explicit
 
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As Long
Const CB_SHOWDROPDOWN = &H14F
Const CB_FINDSTRINGEXACT = &H158
 
Private Sub Command1_Click()
    With Combo1
        If Not CheckDuplicates(.hWnd, .Text) Then
            .AddItem .Text
        End If
        SendMessage .hWnd, CB_SHOWDROPDOWN, True, ByVal 0&
    End With
End Sub
 
Private Sub Form_Load()
    'Put some items in the combobox
    With Combo1
        .AddItem "Sunday"
        .AddItem "Monday"
        .AddItem "Tuesday"
        .AddItem "Wednesday"
        .AddItem "Thursday"
        .AddItem "Friday"
        .AddItem "Saturday"
        'Provide the default value
        .Text = .List(0)
        'Open the dropdown list
        SendMessage .hWnd, CB_SHOWDROPDOWN, True, ByVal 0&
    End With
End Sub
 
Private Function CheckDuplicates(chwnd As Long, StringText As String) As Boolean
    CheckDuplicates = SendMessageByString(chwnd, CB_FINDSTRINGEXACT, -1, ByVal StringText) > -1
End Function

Related posts:

  1. How to determine the Number of Rows in Combo Box
  2. How to Show/Hide ComboBox’s DropDown List
  3. How to show the Drop Down of ComboBox List
  4. How to remove duplicates from list box (in two ways)
  5. How to find list box item
  6. How to add items to combo box control using API
  7. How to Copy List Box Content To Another List Box\Combo Box
  8. How to check on which word the Mouse hover in Rich Text Box
  9. How to scan List Box for the text in TextBox
  10. How to Link a text box to a list box? So, as you type, it highlights any matching entries

Filed under: Controls Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.