Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

ListBox like Combo dropdown

ListBox like Combo dropdown

ListBox like Combo dropdown

When a ComboBox is dropped the dropdown window is displayed 'on top' of the underlying Form (or Container) so it is not clipped if it extends beyond the Form's (Container's) borders.
Can I make a ListBox behave in the same way?

RE: ListBox like Combo dropdown

So your ListBox is NOT "displayed 'on top' of the underlying Form (or Container)"?
Or it IS "clipped if it extends beyond the Form's (Container's) borders"?

How does it look like in design mode?

Have fun.

---- Andy

RE: ListBox like Combo dropdown

Gi9ven that a ListBox does not have a dropdown component, it is difficult to see how we can make it behave in the same way ...

Perhaps you can clarify your requirement a little.

RE: ListBox like Combo dropdown

I'm building an AutoComplete TextBox which uses a ListBox as its 'dropdown window'.
You type stuff in the TextBox and the ListBox drops with previous entries like the one you are typing.
I essentially end up with a dropdown combobox but separating the TextBox and ListBox box Controls/ Events gives more flexibility than that offered in a real ComboBox.
All is fine except if the TextBox is near the bottom of a Form; the ListBox drops but is clipped by the Form's borders. Under the same circumstances a real ComboBox's dropdown window is not clipped, it extends out beyond the Form's borders.
Currently I have the ListBox (Top) displayed at Tb.Top + Tb.Height if room in its container below that point is greater than List.Height, otherwise it's displayed above the TextBox at Tb.Top - List.Height; that works well but it is not a viable solution if the Form is a small/ short one.

RE: ListBox like Combo dropdown

Well ... it's possible, but involves a lot of faffing about with the API if you try and do it directly with the listbox (essentially by making it a popup window instead of a child window). You can emulate it, though, relatively simply - although there is still a lot of housekeeping to make sure the list portion appears in the right place, and disappears when it is supposed to - some of which work it sounds like you may already have done.

Here's the cheap solution

Create Form1 with 1 textbox, and two command buttons (example uses command buttons to make the 'popup' appear and disappear; in reality you'd want to work this in to your autocomplete event chain)

Create Form2 with no borders, and stick a listbox on it (flat style rather than 3d style).

And add a module for good measure

OKay - form 1 gets:


Option Explicit

Private Sub Command1_Click()

    Dim x As Long
    Dim y As Long
    x = Form1.Text1.Left
    y = Form1.Text1.Top + Form1.Text1.Height
    Form1.ScaleMode = vbPixels
    TranslateCoords Form1, x, y
    Call SetWindowPos(Form2.hwnd, HWND_TOPMOST, x, y, 100, 100, SWP_SHOWWINDOW Or SWP_NOSIZE)
End Sub

Private Sub Command2_Click()
     Unload Form2
End Sub 

Form2 gets:


Option Explicit

Private Sub Form_Load()
    ' Remember to set List1.IntegralHeight = False
    List1.Appearance = 0
    With Me
        List1.Move 0, 0, .Width, .Height
    End With

End Sub 

And finally the module gets


Option Explicit

Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Public Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Public Type POINTAPI
        x As Long
        y As Long
End Type
Public Const HWND_TOPMOST = -1
Public Const SWP_NOSIZE = &H1
Public Const SWP_NOMOVE = &H2
Public Const SWP_SHOWWINDOW = &H40

Public Sub TranslateCoords(myform As Form, ByRef x As Long, ByRef y As Long)
    Dim myPoint As POINTAPI
    myPoint.x = x
    myPoint.y = y
    ClientToScreen myform.hwnd, myPoint
    x = myPoint.x
    y = myPoint.y
End Sub 

Just a rough example, but shows the basic principle.

RE: ListBox like Combo dropdown

strongm thank you.
I have your example running, albiet the first time in a session I have to click Command1 twice before Form2/ List1 is shown.
I had considered the extra Form approach (and your version is certainly more concise than mine would have been) but I am in the process of trying to streamline handling of the AcTextBox/ Listbox into a class, after hard coding it into several Forms; I'd like to keep it all wrapped in the class if possible without the need for another Form. My Forms thus far have been tall enough to cope and so it is not a current issue, however, you temp me with ...do it directly with the listbox ...please tell me more when you have a mo.

RE: ListBox like Combo dropdown

A Form IS a class ...

RE: ListBox like Combo dropdown

>A Form IS a class ...
>...wrapped in the class if possible...
Wrapped in one class if possible; although you've got me wondering now if the Methods I have in my class and the UI for the List could all be done on a Form2...

RE: ListBox like Combo dropdown

>please tell me more when you have a mo.

Sorry - been working on a project and without access to a VB6 box for the last couple of days. I'll see if I get a mo this afternoon.

RE: ListBox like Combo dropdown

No problem I've been hacking away at the 'Form2' approach which you suggested earlier; so far not as tricky as I suspected but I think it's about to get trickier!

RE: ListBox like Combo dropdown

Welol, here's a very messy prototype for just using a listbox to illustrate what I meant. It needs a LOT of work to make it work like the combobox dropdown, however. Consider it a proof of concept. Frankly I'd stick with the Form2 idea .

You'll neeeda form3, with two textboxes, a listbox and two command buttons, then the following code:


Option Explicit

Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
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 Const LB_ITEMFROMPOINT = &H1A9

Private Const GW_OWNER = 4
Private Const GWL_HWNDPARENT = (-8)
Private Const GWL_EXSTYLE = (-20)
Private Const WS_EX_APPWINDOW = &H40000
Private Const WS_EX_TOOLWINDOW = &H80&

Private Declare Function GetDesktopWindow Lib "user32" () As Long

Private Sub Command1_Click()
    Dim CurrentStyle As Long
    Dim lStyle As Long
    Dim X As Long
    Dim Y As Long
    Dim myControl As Control
    Set myControl = List1
    Form3.ScaleMode = vbPixels
    X = Text1.Left
    Y = Text1.Top + Text1.Height
    myControl.Width = Text1.Width
    TranslateCoords Form3, X, Y

    lStyle = GetWindowLong(myControl.hwnd, GWL_EXSTYLE)
   lStyle = lStyle Or WS_EX_TOOLWINDOW
   lStyle = lStyle And Not (WS_EX_APPWINDOW)
   SetWindowLong myControl.hwnd, GWL_EXSTYLE, lStyle
    SetParent myControl.hwnd, 0& 'GetDesktopWindow()
    Call SetWindowPos(myControl.hwnd, HWND_TOPMOST, X, Y, 1000, 1000, SWP_SHOWWINDOW Or SWP_NOSIZE)

    myControl.Visible = True

End Sub

Private Sub Command2_Click()
    SetParent List1.hwnd, Form3.hwnd
    List1.Move Text1.Left, Text1.Top + Text1.Height
End Sub

Private Sub Form_Load()
    List1.AddItem "this"
    List1.AddItem "is"
    List1.AddItem "a"
    List1.AddItem "simple"
    List1.AddItem "example"
    List1.Visible = False
End Sub

Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim lXPoint As Long
    Dim lYPoint As Long
    Dim lIndex As Long

    lXPoint = CLng(X / Screen.TwipsPerPixelX)
    lYPoint = CLng(Y / Screen.TwipsPerPixelY)
    With List1
        ' get selected item from list
        lIndex = SendMessage(.hwnd, LB_ITEMFROMPOINT, 0, ByVal ((lYPoint * 65536) + lXPoint))
        If (lIndex >= 0) And (lIndex <= .ListCount) Then
            Text2.Text = .List(lIndex)
            Text2.Text = ""
        End If
    End With
End Sub 

RE: ListBox like Combo dropdown

strongm thank you very much for that, some interesting stuff in there which I'm sure I will find useful. I'll be into it soon. The Form2 approach is tricky to manage because the two forms fight for focus (that does not happen when the ListBox is on Form1 because 1. the ListBox can be made visible without it gaining Focus and 2. the vertical scroll bar on the ListBox can be used without the ListBox gaining Focus); it also gets quirky if Form1 is moved when Form2 is visible...
Working on it.
Thanks again.

RE: ListBox like Combo dropdown

>it also gets quirky if Form1 is moved when Form2 is visible...

THis is true of any of the soloutions I've suggested - once you play with the parent (and the combo drop down certainly does) you have to manage all this sort of stuff( the combo doesn't because it is built into the control)

RE: ListBox like Combo dropdown

>..true of any of the soloutions...
Yes, I noticed that the Combo can be made to misbehave if say the Form or Combo's Left/ Top Properties are changed via a Timer in its DropDown Event; however under more usual circumstances the dropdown is withdrawn automatically when a MouseDown happens anywhere on its Parent Form, thus preventing it being visible when the Form is moved. Detecting a MouseDown anywhere on Form1 is currently beyond me.

RE: ListBox like Combo dropdown

strongm - Ok I went back to the (pretty well debugged) Class I described in my third post and applied the code in your Form3; all works fine except that I still need a way to make the ListBox disappear when I click anywhere on the Form containing the TextBox (except on the TextBox itself!). That could be handled manually by putting code to make the ListBox invisible in all the Form's containers, but then I'd still need code to detect if the Form was moved...The ListBox is already made invisible when focus leaves the TextBox to any control (other than the ListBox when it is made visible).
Any suggestions?

RE: ListBox like Combo dropdown

You might want to look at the SetCapture API. It might offer you some options.

RE: ListBox like Combo dropdown

Thanks strongm - I seem to have been a bit busy to get on with that, I expect I will get back to it soon.

RE: ListBox like Combo dropdown

>...faffing about with the API...
Well you did warn me!
So I applied SetCapture to the Form when the ListBox was made Visible and I got it all to work except for the vertical scroll bar on the ListBox (if shown). Your List1_MouseDown stopped working of course so had to handle that in Form_MouseDown.
I am begining to develop a serious respect for the code underlying a normal ComboBox! I think I'll have another look at using one of them, meantime the class I have does all I need albiet the Listbox gets clipped by the Forms borders. This has become something of a rainy day project and it has mercifully stopped raining over the past few days, hope to have time to get back to it soon. Thanks for your help.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close