CodeItBetter Programming Another VB Programming Blog

How to Copy List Box Content To Another List Box\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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'Controls - How to Copy List Box Content To Another List Box\Combo Box

Option Explicit
 
'Add two List Boxes, a Combo Box and two Command Buttons to your form.
'Add few Items to List1. Press the first button to copy List1 content to List2
'and Press the second button to copy List1 content to Combo1.

Public Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Public Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
 
Private Sub Command1_Click()
    Call CopyListToList(List1, List2)
End Sub
 
Private Sub Command2_Click()
    Call CopyListToCombo(List1, Combo1)
End Sub
 
Private Function CopyListToList(lst As ListBox, target As ListBox) As Long
    Dim c As Long
    Const LB_GETCOUNT = &H18B
    Const LB_GETTEXT = &H189
    Const LB_ADDSTRING = &H180
    Dim numItems As Long
    Dim sItemText As String * 255
    'get the number of items in the lst list
    numItems = SendMessageLong(lst.hWnd, LB_GETCOUNT, 0&, 0&)
    'if it is more than one, then copy the items
    If numItems > 0 Then
        For c = 0 To numItems - 1
            Call SendMessageStr(lst.hWnd, LB_GETTEXT, c, ByVal sItemText)
            Call SendMessageStr(target.hWnd, LB_ADDSTRING, 0&, ByVal sItemText)
        Next
    End If
    'get the number of items in the target list and return that as the function value
    numItems = SendMessageLong(target.hWnd, LB_GETCOUNT, 0&, 0&)
    CopyListToList = numItems
End Function
 
Private Function CopyListToCombo(lst As ListBox, target As ComboBox) As Long
    Dim c As Long, numItems As Long
    Const LB_GETCOUNT = &H18B
    Const LB_GETTEXT = &H189
    Const CB_GETCOUNT = &H146
    Const CB_ADDSTRING = &H143
    Dim sItemText As String * 255
    'get the number of items in the lst list
    numItems = SendMessageLong(lst.hWnd, LB_GETCOUNT, 0&, 0&)
    'if it is more than one, then copy the items
    If numItems > 0 Then
        For c = 0 To numItems - 1
            Call SendMessageStr(lst.hWnd, LB_GETTEXT, c, ByVal sItemText)
            Call SendMessageStr(target.hWnd, CB_ADDSTRING, 0&, ByVal sItemText)
        Next
    End If
    'get the number of items in the target combo and return that as the function value
    numItems = SendMessageLong(target.hWnd, CB_GETCOUNT, 0&, 0&)
    CopyListToCombo = numItems
End Function
Filed under: Controls Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.