Add ComboBox inside ListView

0

I would like to be supported to be able to add a ComboBox control inside a ListView.

The image shows the Listview, I want to add a comboBox in the second column for each item that exists in the first column.

    
asked by Julio Moreno Dev 16.02.2017 в 17:45
source

1 answer

-1

There is an article from Microsoft where it teaches how to do step by step: How to use a ComboBox control to modify data in a ListView control in Visual Basic 2005 or in Visual Basic .NET

The idea is to create a control inherited from ListView that will have your event MouseUp and that will include a container of controls, for this case a ComboBox where you will play with your property Visible in your events SelectedValueChanged , KeyPress and Leave .

Creating User Control Create a Windows Library project that contains that control so that it is then referenced in your form .

Imports SystemImports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Windows.Forms

Public Class MyListView
    Inherits System.Windows.Forms.ListView

    Public Sub New()
        MyBase.New()        
        InitializeComponent()
    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    Private components As System.ComponentModel.IContainer

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container()
    End Sub

    Private Const WM_HSCROLL As Integer = &H114
    Private Const WM_VSCROLL As Integer = &H115

    Protected Overrides Sub WndProc(ByRef msg As Message)
        If ((msg.Msg = WM_VSCROLL) Or (msg.Msg = WM_HSCROLL)) Then
            Me.Focus()
        End If

        MyBase.WndProc(msg)
    End Sub
End Class

Application for Windows You will have your form where it will contain the necessary controls and you will define the control events MyListView (control inherited from ListView ) and% Combobox .

In your form you add a control Combobox (change name to cbListViewComboy ) with its property Visible in False .

You add a global variable in your form:

Private lvItem As ListViewItem

In the event Load of the form you add:

Me.cbListViewCombo.Items.Add("NC")
Me.cbListViewCombo.Items.Add("WA")    

Me.MyListView1.View = View.Details
Me.MyListView1.FullRowSelect = True    

Dim columnheader As ColumnHeader
Dim lviewitem As ListViewItem    

lviewitem = New ListViewItem("NC")
lviewitem.SubItems.Add("North Carolina")
Me.MyListView1.Items.Add(lviewitem)

lviewitem = New ListViewItem("WA")
lviewitem.SubItems.Add("Washington")
Me.MyListView1.Items.Add(lviewitem)    

columnheader = New ColumnHeader()
columnheader.Text = "State Abbr."
Me.MyListView1.Columns.Add(columnheader)

columnheader = New ColumnHeader()
columnheader.Text = "State"
Me.MyListView1.Columns.Add(columnheader)    

Dim ch As ColumnHeader

For Each ch In Me.MyListView1.Columns
   ch.Width = -2
Next

You add in the event SelectedValueChanged of ComboBox

lvItem.Text = Me.cbListViewCombo.Text
Me.cbListViewCombo.Visible = False

You add in the event Leave of ComboBox

lvItem.Text = Me.cbListViewCombo.Text
Me.cbListViewCombo.Visible = False

You add in the event KeyPress of ComboBox

Select Case (e.KeyChar)
   Case ChrW(CType(Keys.Escape, Integer))
      Me.cbListViewCombo.Text = lvItem.Text
      Me.cbListViewCombo.Visible = False

   Case ChrW(CType(Keys.Enter, Integer))
      Me.cbListViewCombo.Visible = False
End Select

And finally, you add in the event MouseUp of myListView1

lvItem = Me.MyListView1.GetItemAt(e.X, e.Y)

If Not (lvItem Is Nothing) Then
   Dim ClickedItem As Rectangle = lvItem.Bounds

   If ((ClickedItem.Left + Me.MyListView1.Columns(0).Width) < 0) Then
      Return
   ElseIf (ClickedItem.Left < 0) Then
      If ((ClickedItem.Left + Me.MyListView1.Columns(0).Width) > Me.MyListView1.Width) Then                   
         ClickedItem.Width = Me.MyListView1.Width
         ClickedItem.X = 0
      Else
         ClickedItem.Width = Me.MyListView1.Columns(0).Width + ClickedItem.Left
         ClickedItem.X = 2
      End If

   ElseIf (Me.MyListView1.Columns(0).Width > Me.MyListView1.Width) Then
      ClickedItem.Width = Me.MyListView1.Width

   Else
      ClickedItem.Width = Me.MyListView1.Columns(0).Width
      ClickedItem.X = 2
   End If

   ClickedItem.Y += Me.MyListView1.Top
   ClickedItem.X += Me.MyListView1.Left

   Me.cbListViewCombo.Bounds = ClickedItem

   Me.cbListViewCombo.Text = lvItem.Text

   Me.cbListViewCombo.Visible = True
   Me.cbListViewCombo.BringToFront()
   Me.cbListViewCombo.Focus()
End If

If necessary for C# , there is also an article: How to use a ComboBox control to modify data in a ListView control in Visual C #

    
answered by 19.02.2017 в 09:59