Results 1 to 2 of 2

Thread: Substituting a ListView for a Combo's Dropdown List

  1. #1

    Thread Starter
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Question Substituting a ListView for a Combo's Dropdown List

    Hi

    The code below attaches the Listview1 to Combo1 of Form1. But it can't attach ListView2 to Combo2 of Form2. What i mean is, i want to attach any Listview to any Combo of any Form. So how can modify this code in such a way that will satisfy my requirement.

    Please help me with this issue.



    To a form, add a combo (Combo1), a listview control (ListView1), and three command buttons (Command1, Command2, Command3). Set the Style property of the Combo to Style 2, and add the following code to the form:


    VB Code:
    1. ' Form Code
    2. ' =======
    3.  
    4. Option Explicit
    5.  
    6. Private bKeepOpen As Boolean
    7.  
    8. Private Sub Form_Load()
    9.  
    10.    Dim c As Long
    11.    Dim chd As ColumnHeader
    12.    Dim itmx As ListItem
    13.    
    14.   'Add some dummy data to the listview and hide
    15.    With ListView1
    16.      
    17.       Set chd = .ColumnHeaders.Add(, , "Name", 1000)
    18.       Set chd = .ColumnHeaders.Add(, , "Col 2", 1000)
    19.       Set chd = .ColumnHeaders.Add(, , "Col 3", 1000)
    20.       Set chd = .ColumnHeaders.Add(, , "Col 4", 600)
    21.      
    22.       For c = 1 To 15
    23.          Set itmx = .ListItems.Add(, , Screen.Fonts(c))
    24.          itmx.SubItems(1) = "screen"
    25.          itmx.SubItems(2) = "font"
    26.          itmx.SubItems(3) = c
    27.       Next
    28.      
    29.       .View = lvwReport
    30.       .FullRowSelect = True  'vb6 only
    31.       .BorderStyle = ccNone
    32.       .Visible = False
    33.          
    34.    End With
    35.  
    36.   'set inital state of command buttons
    37.    Command1.Caption = "hook combo"
    38.    Command2.Caption = "unhook combo"
    39.    Command3.Caption = "unhook && end"
    40.    Command1.Enabled = True
    41.    Command2.Enabled = False  
    42. End Sub
    43.  
    44.  
    45. Private Sub Command1_Click()
    46.  
    47.    If defWinProc = 0 Then
    48.       Hook Combo1.hwnd
    49.       Command1.Enabled = False
    50.       Command2.Enabled = True
    51.    End If
    52.    
    53. End Sub
    54.  
    55.  
    56. Private Sub Command2_Click()
    57.  
    58.   'unhook the combo
    59.    If defWinProc <> 0 Then
    60.       Unhook Combo1.hwnd
    61.       defWinProc = 0
    62.       Command1.Enabled = True
    63.       Command2.Enabled = False      
    64.    End If
    65.    
    66. End Sub
    67.  
    68.  
    69. Private Sub Command3_Click()
    70.  
    71.    Unload Me
    72.  
    73. End Sub
    74.  
    75.  
    76. Private Sub Form_Unload(Cancel As Integer)
    77.  
    78.    If defWinProc <> 0 Then Unhook Combo1.hwnd
    79.    
    80. End Sub
    81.  
    82.  
    83. Private Sub ListView1_KeyDown(KeyCode As Integer, Shift As Integer)
    84.  
    85.   'set flag to allow arrow and enter
    86.   'keys to simulate behaviour of normal
    87.   'combo
    88.    bKeepOpen = True
    89.  
    90. End Sub
    91.  
    92.  
    93. Private Sub ListView1_KeyPress(KeyAscii As Integer)
    94.  
    95.   'set flag to allow arrow and enter
    96.   'keys to simulate behaviour of normal
    97.   'combo
    98.    If KeyAscii = vbKeyReturn Then
    99.      
    100.      'simulate selecting item with enter
    101.       bKeepOpen = False
    102.       Call ListView1_Click
    103.    Else
    104.    
    105.      'alpha or arrow keys being used,
    106.      'so keep open
    107.       bKeepOpen = True
    108.      
    109.    End If
    110.      
    111. End Sub
    112.  
    113.  
    114. Private Sub ListView1_Click()
    115.  
    116.    Dim itmx As ListItem
    117.  
    118.    If ListView1.ListItems.Count > 0 Then
    119.  
    120.       Set itmx = ListView1.SelectedItem
    121.  
    122.      'For a style 0 combo, you can not assign
    123.      'to the Text property from within the click
    124.      'event, so the selected item must be 'added'
    125.      'as the only combo item, and selected using
    126.      'its listindex property.
    127.      '
    128.      'For a style 2 combo, the text property
    129.      'can't be set unless there is an exact
    130.      'match to a list item, so again we fake it
    131.      'by adding the selection to the combo and
    132.      'selecting it.
    133.      '
    134.      'Finally, since the tabs can't be used
    135.      'in the combo's edit window, as it doesn't
    136.      'support tabstops either, on selection we'll
    137.      'display the main listview item
    138.       With Combo1
    139.          .Clear
    140.          .AddItem itmx.Text
    141.          .ListIndex = 0
    142.       End With
    143.  
    144.    End If
    145.  
    146.    If bKeepOpen = False Then
    147.       ListView1.Visible = False
    148.       Combo1.SetFocus
    149.    End If
    150.    
    151. End Sub

    VB Code:
    1. ' BAS Module Code
    2. ' ============
    3.  
    4. Option Explicit
    5.  
    6. Public defWinProc As Long
    7.  
    8. Public Const GWL_WNDPROC As Long = -4
    9. Private Const CBN_DROPDOWN As Long = 7
    10. Private Const WM_LBUTTONDOWN As Long = &H201
    11. Private Const WM_KEYDOWN As Long = &H100
    12. Private Const VK_F4 As Long = &H73
    13.  
    14. Private Declare Function CallWindowProc Lib "user32" _
    15.    Alias "CallWindowProcA" _
    16.   (ByVal lpPrevWndFunc As Long, _
    17.    ByVal hwnd As Long, ByVal Msg As Long, _
    18.    ByVal wParam As Long, ByVal lParam As Long) As Long
    19.  
    20. Private Declare Function SendMessage Lib "user32" _
    21.    Alias "SendMessageA" _
    22.   (ByVal hwnd As Long, _
    23.    ByVal wMsg As Long, _
    24.    ByVal wParam As Long, _
    25.    lParam As Any) As Long
    26.  
    27. Public Declare Function SetWindowLong Lib "user32" _
    28.    Alias "SetWindowLongA" _
    29.   (ByVal hwnd As Long, ByVal nIndex As Long, _
    30.    ByVal dwNewLong As Long) As Long
    31.  
    32.  
    33. Public Sub Unhook(hwnd As Long)
    34.    
    35.    If defWinProc <> 0 Then
    36.    
    37.       Call SetWindowLong(hwnd, _
    38.                          GWL_WNDPROC, _
    39.                          defWinProc)
    40.       defWinProc = 0
    41.    End If
    42.    
    43. End Sub
    44.  
    45.  
    46. Public Sub Hook(hwnd As Long)
    47.  
    48.    'Don't hook twice or you will
    49.    'be unable to unhook it.
    50.     If defWinProc = 0 Then
    51.    
    52.       defWinProc = SetWindowLong(hwnd, _
    53.                                  GWL_WNDPROC, _
    54.                                  AddressOf WindowProc)
    55.      
    56.     End If
    57.    
    58. End Sub
    59.  
    60.  
    61. Public Function WindowProc(ByVal hwnd As Long, _
    62.                            ByVal uMsg As Long, _
    63.                            ByVal wParam As Long, _
    64.                            ByVal lParam As Long) As Long
    65.  
    66.   'only if the window is the combo box...
    67.    If hwnd = Form1.Combo1.hwnd Then
    68.    
    69.       Select Case uMsg
    70.      
    71.          Case CBN_DROPDOWN  'the list box of a combo
    72.                             'box is about to be made visible.
    73.            
    74.            'return 1 to indicate we ate the message
    75.             WindowProc = 1
    76.    
    77.          Case WM_KEYDOWN   'prevent the F4 key from showing
    78.                            'the combo's list
    79.            
    80.             If wParam = VK_F4 Then
    81.            
    82.               'set up the parameters as though a
    83.               'mouse click occurred on the combo,
    84.               'and call this routine again
    85.                Call WindowProc(hwnd, WM_LBUTTONDOWN, 1, 1000)
    86.                
    87.             Else
    88.            
    89.               'there's nothing to do keyboard-wise
    90.               'with the combo, so return 1 to
    91.               'indicate we ate the message
    92.                WindowProc = 1
    93.            
    94.             End If
    95.            
    96.          Case WM_LBUTTONDOWN  'process mouse clicks
    97.          
    98.            'if the listview is hidden, position and show it
    99.             If Form1.ListView1.Visible = False Then
    100.            
    101.                With Form1
    102.                   .ListView1.Left = .Combo1.Left
    103.                   .ListView1.Width = .Combo1.Width
    104.                   .ListView1.Top = .Combo1.Top + .Combo1.Height + 1
    105.                   .ListView1.Visible = True
    106.                   .ListView1.SetFocus
    107.                End With
    108.                
    109.             Else
    110.                  
    111.               'the listview must be visible, so hide it
    112.                Form1.ListView1.Visible = False
    113.             End If
    114.    
    115.            'return 1 to indicate we processed the message
    116.             WindowProc = 1
    117.          
    118.          Case Else
    119.          
    120.            'call the default window handler
    121.             WindowProc = CallWindowProc(defWinProc, _
    122.                                         hwnd, _
    123.                                         uMsg, _
    124.                                         wParam, _
    125.                                         lParam)
    126.    
    127.       End Select
    128.    
    129.    End If  'If hwnd = Form1.Combo1.hwnd
    130.    
    131. End Function

  2. #2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width