Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

insert combobox in listview column

Status
Not open for further replies.

sal21

Programmer
Apr 26, 2004
425
IT
Possible to call a combobox when i click on a listsubitem of column 3.
example,...

click on cell 4 of column 3, appear a combobox, with same width of cell, select value, click on, save value of combobox in current cell, hide combobox...
 
Not simply,

The most elegant way to do this would be via OwnerDraw, which we hjave previoulsy illustrated in this forum for you. The only other way is pretty much to fake it by calculating the location of the bounding box of the listsubitem, and moving the combobox over it. But limitations of the listview control (and of the VB6 combo box) make even this a pain. here's an illustrative example of the idea - but I've knocked this together in about 20 mins, and it is NOT production code, and certainly does not deal properly with all conditions (e.g, if you have hidden any columns, or cloick where there is no column, or in a column header).

You'll need a form (Foprm1) with a Listview (ListView1) and a Combobox (Combo1):

Code:
[blue]Option Explicit
Public mySubitem As ListSubItem

Private Sub Form_Load()
    Dim itmX As ListItem   
    
    [COLOR=green]' Set up a l of items with subitems[/color]
    Set itmX = ListView1.ListItems.Add(, , "hello")
    itmX.ListSubItems.Add , , "just"
    itmX.ListSubItems.Add , , "testing"
    
    Set itmX = ListView1.ListItems.Add(, , "goodbye")
    itmX.ListSubItems.Add , , "more"
    itmX.ListSubItems.Add , , "stuff"
    
   [COLOR=green] ' Now set up combobox - make sure it uses same font as listview to try and ensure similar heights,
    ' as neither the listview's item height, nor the combobox height can be directly set; they are reliant on the currently selected font[/color]
    Set Combo1.Font = ListView1.Font
    Combo1.AddItem "tell"
    Combo1.AddItem "me"
    Combo1.Visible = False
End Sub

Private Sub ListView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    Dim lp As Long
    Dim myitem As ListItem
    
    lp = 1
    Do Until x > ListView1.ColumnHeaders(lp).Left And x < ListView1.ColumnHeaders(lp).Left + ListView1.ColumnHeaders(lp).Width
        lp = lp + 1
    Loop

    Set myitem = ListView1.HitTest(30, y)
    If Not myitem Is Nothing And lp > 1 Then
        Form1.Combo1.Move ListView1.Left + ListView1.ColumnHeaders(lp).Left + 15, ListView1.Top + myitem.Top '
        Form1.Combo1.Width = ListView1.ColumnHeaders(lp).Width
        Set mySubitem = myitem.ListSubItems(lp - 1)
        Combo1.Visible = True
    End If
End Sub[/blue]
 
STRONGM!!!!
The code is exactlly wath i need!
Tks as usual.

note:
i have set mySubitem As ListSubItem, correct?

I have option explicit, in the head of module:)

and i can set the hight of combobox?
 
>and i can set the hight of combobox?

' Now set up combobox - make sure it uses same font as listview to try and ensure similar heights,
' neither the listview's item height, nor the combobox height can be directly set; they are reliant on the currently selected font
 
Strongm,
i have used you code, about combobox and listview. work.

But only a prob...
when i scroll the vertical bar of listview the combo remain locked to the last position and dont scroll together the rows!!!!

 
>But only a prob...

1) Not a requirement you originally mentioned
2) I warned that I had not provided prodcution code, not a complete code solution solutions for you here

But is does mean you already have all the code you need to position the combobox (basically the following two lines)

Code:
Form1.Combo1.Move ListView1.Left + ListView1.ColumnHeaders(lp).Left + 15, ListView1.Top + myitem.Top '
Form1.Combo1.Width = ListView1.ColumnHeaders(lp).Width

So now 'all' you need to do is subclass the listview, and respond to scroll messages in order to position the combo correctly. I suggest you download and examine the example code found here for this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top