65.9K
CodeProject is changing. Read more.
Home

.NET dropdown in listview item / subitem... etc.

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (1 vote)

Jan 25, 2012

CPOL
viewsIcon

36677

Quick and easy way to add dropdown functionality to the listview.

I had the need for a dropdown in my listview... did a bit of research, I did find some references, but all of it was pages of code with really complicated methods. I played around a bit, and found that the listview (at least on .NET 4), gives us more than enough to work with to easily achieve this.

Here ya go...

Private Sub ListView1_DoubleClick(ByVal sender As Object, _
              ByVal e As System.EventArgs) Handles ListView1.DoubleClick
    ListView1.SelectedItems(0).BeginEdit()
    drop_combo()
End Sub

Private Sub drop_combo()
    Dim rect As New Rectangle
    rect = ListView1.SelectedItems(0).GetBounds(ItemBoundsPortion.Label)
    cbo_test.Visible = True
    cbo_test.Parent = ListView1
    cbo_test.Left = rect.Left - 2  'Normal left is just a little bit off, can see the abck label sticking out...
    cbo_test.Top = rect.Top
    cbo_test.Width = rect.Width
End Sub

Private Sub hide_combo()
    cbo_test.Visible = False
End Sub

Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, _
              ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
    hide_combo()
End Sub