dcsimg
Results 1 to 3 of 3

Thread: center and move combobox in listview

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Italy-Napoli
    Posts
    1,934

    center and move combobox in listview

    Based the event ListView1_ItemClick, i need to move and center me.combotest in Me.LABICAB.ListItems(L).ListSubItems(3), and assign the with based the cell clicked...
    Possible?

    L is:
    L = Me.LABICAB.SelectedItem.Index

    my poor test:

    Combo1.Move ListView1.Left + Item.Left, ListView1.Top + Item.Top,
    ListView1.Width

    but naturally dont work:-)

  2. #2
    Hyperactive Member
    Join Date
    Aug 2017
    Posts
    307

    Re: center and move combobox in listview

    Do you mean like in the following example?

    Code:
    Option Explicit 'Add an MSComctlLib.ListView to a blank Form
    
    Private Type RECT
        Left   As Long
        Top    As Long
        Right  As Long
        Bottom As Long
    End Type
    
    Private Declare Function MoveWindow Lib "user32.dll" (ByVal hWnd As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, Optional ByVal bRepaint As Long) As Long
    Private Declare Function SendMessageW Lib "user32.dll" (ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
    Private Declare Function SetParent Lib "user32.dll" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    
    Private m_CB As VB.ComboBox
    
    Private Sub Form_Activate()
        Dim I As Long
    
        With ListView1
            .FullRowSelect = True
            .GridLines = True
            .View = lvwReport
    
            For I = 0& To 4&
                .ColumnHeaders.Add , , CStr(I)
            Next
    
            For I = 1& To 10&
                .ListItems.Add , , CStr(I)
            Next
    
            Set m_CB = Controls.Add("VB.ComboBox", "m_CB")
            I = SetParent(m_CB.hWnd, .hWnd): Debug.Assert I = Me.hWnd
        End With
    
        For I = 1& To 10&
            m_CB.AddItem I
        Next
    End Sub
    
    Private Sub Form_Resize()
        Dim GapX As Single, GapY As Single
    
        GapX = ScaleX(10!, vbPixels, ScaleMode)
        GapY = ScaleY(10!, vbPixels, ScaleMode)
        On Error Resume Next
        ListView1.Move ScaleLeft + GapX, ScaleTop + GapY, ScaleWidth - GapX - GapX, ScaleHeight - GapY - GapY
    End Sub
    
    Private Sub ListView1_ItemClick(ByVal Item As MSComctlLib.ListItem)
        Const LVIR_BOUNDS = 0&, LVM_GETSUBITEMRECT = &H1038&
        Dim R As RECT
    
        R.Left = LVIR_BOUNDS
        R.Top = 3&              '<-- 1-based SubItem Index
    
        If SendMessageW(ListView1.hWnd, LVM_GETSUBITEMRECT, Item.Index - 1&, R) Then
            R.Right = R.Right - R.Left + 1&
            R.Bottom = R.Bottom - R.Top + 1&
            m_CB.Visible = MoveWindow(m_CB.hWnd, R.Left, R.Top, R.Right, R.Bottom, -True) <> 0&
        End If
    End Sub
    
    Private Sub ListView1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = vbLeftButton Then
            If ListView1.HitTest(X, Y) Is Nothing Then
                If Not ListView1.SelectedItem Is Nothing Then
                    m_CB.Visible = False
                    ListView1.SelectedItem.Selected = False
                    Set ListView1.SelectedItem = Nothing
                End If
            End If
        End If
    End Sub

    If that isn't what you want, then you need to explain yourself more clearly.

  3. #3
    Addicted Member
    Join Date
    Mar 2017
    Posts
    195

    Re: center and move combobox in listview

    Really cool, Victor. Now, what if I only wanted the CB text and drop down button but not the CB borders around the text and button

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width