Greetings.
I have searched high and low for the above topic but to no avail :confused:
Is it possible to add a combo box into one of the columns of a listview control?
Please advise.

Dani AI

Generated

Quick, practical summary that builds on , and : reparenting combo boxes into a ListView will work, but you must compute each subitem's real rectangle from the control (not by stacking Heights or using ListItems as windows), then move/size the combo into that rectangle and handle scrolling. The Win32 SetParent call is the right tool to make the combo a child of the ListView, and the ListView messages let you get exact cell coordinates. (learn.microsoft.com)

ListView items and subitems are not separate window handles, so you cannot address a subitem by hWnd; you must ask the control for the bounding rectangle (LVM_GETSUBITEMRECT / ListView_GetSubItemRect) and then position the child window there. Also remember that MoveWindow/SetWindowPos expect coordinates relative to the parent client area once you reparent the control. (stackoverflow.com)

A robust, simpler pattern is to use one in-place ComboBox and move it over the clicked subitem (show it, write value back on leave/OK, hide it on scroll). That avoids creating N child windows and avoids z-order/scrolling headaches; you must hide or reposition the editor when the ListView scrolls (watch WM_VSCROLL / WM_HSCROLL or override WndProc). Microsoft provides a worked example of this approach. (learn.microsoft.com)

Example (VB6-style) — get the cell rect, reparent and move the combo:

' API decls and types omitted for brevity
' Use LVM_GETSUBITEMRECT and pass a RECT where .Top = iSubItem (1-based) and .Left = LVIR_BOUNDS,
' then call SetParent(combo.hWnd, listview.hWnd) and MoveWindow(combo.hWnd, rect.Left, rect.Top, rect.Right-rect.Left, rect.Bottom-rect.Top, True).

If things still pile up, the usual causes are: using the wrong coordinate origin (screen vs. client), passing the wrong item/subitem indexes, or not calling MoveWindow/SetWindowPos after SetParent. The docs above give exact parameter details and examples. (learn.microsoft.com)

Recommended Answers

All 8 Replies

It is possible. Look into the API "SetParent"

You might want to download the API-Guide Network. (Sorry, can't recall the URL off hand)

commented: Thanks for the advice! :-) +2

Greetings.
Hey, thanks so much for the "direction".
Did a search in google and I guess I've got a rough picture already.
I'll come back with codes when I'm stuck. ;)
Thanks a lot!

Greetings.
Right, I have came up with a few lines of codes.

I have 6 rows in the listview, and I need to add 1 comboBox to each row at subItem number 2.
At runtime, I could see 1 comboBox at row 1.
I guess all 6 combo boxes are piled up there. :D

Do Until myRS.EOF = True 
        Set objItem = listviewEIP.ListItems.Add(, "A" & myRS!com_code, myRS!com_name) 
        objItem.SubItems(1) = "Something" 
        objItem.SubItems(3) = "Anything" 
        myRS.MoveNext 
    Loop 

    comboID(0).Visible = False 
    With listviewEIP.ColumnHeaders 
    
        Load comboID(1) 
        Call fillCombo(1) 
        comboID(1).Top = listviewEIP.ListItems.Item(1).Height 
        comboID(1).Left = .Item(3).Left 
        comboID(1).Width = .Item(3).Width 
        comboID(1).Visible = True 
        SetParent comboID(1).hWnd, listviewEIP.hWnd 
        comboID(1).ZOrder 
            
        For i = 2 To 6 
            Load comboID(i) 
            fillCombo (i) 
            comboID(i).Top = listviewEIP.ListItems.Item(i - 1).Height 
            comboID(i).Left = .Item(3).Left 
            comboID(i).Width = .Item(3).Width 
            comboID(i).Visible = True 
            SetParent comboID(i).hWnd, listviewEIP.hWnd 
            comboID(i).ZOrder 
        Next i 
    End With

How can I solve that? Please help.
Thanks.

Well, looks like you have the right idea for the SetParent API. In all honesty, I've never tried adding controls to a list view or anything else that has multiple places to put them (aside from a toolbar, but that's slightly different).

You might try to find a way to address subItem2's handle. Every control in Windows has a control (or atleast has the opportunity to apply for one). If you want to think about it in a very basic concept, imagine everything is a picturebox, except not everything has a Picture property. From there, you can see that everything has a Height, Width, and Handle. Beyond that, the controls just add their own properties as needed.

So if you can find a way to retrieve subItem2's hWnd, then you should be in business.

Greetings.
Ok, I got you.
By the way, I could not fully understand wat does hWnd do.
Could you please explain a little if possible?
In addition, I tried using the combo box's Height property but there is an error stating that combo box's height is read-only, n thus I can't use that parameter to do things with it. I'm not modifying the parameter, but I just want that value.
Please advise.

Greetings.
Ok, I got you.
By the way, I could not fully understand wat does hWnd do.
Could you please explain a little if possible?
In addition, I tried using the combo box's Height property but there is an error stating that combo box's height is read-only, n thus I can't use that parameter to do things with it. I'm not modifying the parameter, but I just want that value.
Please advise.

Try taking a look at http://www.vbaccelerator.com . He has a whole pile of stuff there about sub classing and so on. In fact I'd be amazed if his freely downloadable enhanced listview didn't already do exactly what you want.

Greetings.
Ok, I got you.
By the way, I could not fully understand wat does hWnd do.
Could you please explain a little if possible?

Everything in windows is an object. Whenever an object is created, it is given a handle by which windows (or any other program that has access to that handle) can address that object. Forms and controls both have handles, although not all controls register to receive them. That's basically what the hWnd (Some weird way of saying handle for a Window.

Greetings.
Thanks for the url, mnemtsas! Can't really get what you meant by this though:-

In fact I'd be amazed if his freely downloadable enhanced listview didn't already do exactly what you want.

Well, I've browsed thru vbaccelerator , I found something about subclassing.
[1] (Currently looking into this as I guess the concept is quite similar.)
[2] (This is indeed very confusing for me!)
Cypher, thanks a lot for the explanation! Understood, but now's time to apply the theory (toughest part! :sad: ).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.