CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Nov 2002
    Posts
    278

    ListView control question

    Does anyone know the most basic syntax to add an item to a listview control? Ive been looking at this for hours and cant get anywhere with it.

  2. #2
    Join Date
    Oct 2007
    Posts
    29

    Re: ListView control question

    Try this:

    Code:
    ListView1.Items.Add("List Item Text")

  3. #3
    Join Date
    Nov 2002
    Posts
    278

    Re: ListView control question

    That looks easy enough but. . . my ListView control does not have an (Items) method or property its enumeration.

    In fact, the only items in it that start with an (i) are (Icons) and (Index)
    ??

  4. #4
    Join Date
    Oct 2007
    Posts
    29

    Re: ListView control question

    Do you have a ListItems method?

  5. #5
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: ListView control question

    The correct Syntax is
    Code:
    ListView.ListItems.Add , ,"Item Text"
    The syntax of the .Add method is .Add [Index], [Key], [Text], [Icon] [SmallIcon]
    If you don't want a special position (Index) and don't need the Key parameter, you have to omit them, but have to include the commas like in the example above.

  6. #6
    Join Date
    Oct 2007
    Posts
    29

    Re: ListView control question

    Thanks Wof.

    That's what I was about to suggest. When I posted my original reply, I forgot which forum I was looking at and provided the syntax for the ListView control in VB.Net.

  7. #7
    Join Date
    Nov 2002
    Posts
    278

    Re: ListView control question

    Code:
        Me.ListView1.ListItems.Add , , "Stuff 1"
        Me.ListView1.ListItems.Add , , "Stuff 2"
        Me.ListView1.ListItems.Add , , "Stuff 3"
    Thats starting to work. The code above does add the (stuff) items in to the control but they are aligned horizontally or across the top of the control. I need a vertical list that goes down the left side? Do you guys know how to make them show up on the left side of the control, vertically?

    .

  8. #8
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: ListView control question

    You have to set the node.
    Code:
    Add [Index], [Key], [Text], [Icon] [SmallIcon]

    Here's a sample:

    Code:
    Option Explicit
    
    Private Sub Form_Load()
        ListView1.FullRowSelect = True
        ListView1.View = lvwReport
        Dim itmX As ListItem ' Create a variable to add ListItem objects.
        Dim clmX As ColumnHeader ' Create an object variable for the ColumnHeader object.
       ' Add ColumnHeaders.
        Set clmX = ListView1.ColumnHeaders.Add(, , "Column 1", ListView1.Width / 3)
        Set clmX = ListView1.ColumnHeaders.Add(, , "Column 2", ListView1.Width / 3)
        Set clmX = ListView1.ColumnHeaders.Add(, , "Column 3", ListView1.Width / 3)
        
        ListView1.BorderStyle = ccFixedSingle ' Set BorderStyle property.
        ListView1.View = lvwReport ' Set View property to Report.
        
        ' Add a main item
        Set itmX = ListView1.ListItems.Add(, , "First value")
        ' Add two subitems for that item
        itmX.SubItems(1) = "First value subitem 1"
        itmX.SubItems(2) = "First value subitem 2"
    
        ' Add another main item
        Set itmX = ListView1.ListItems.Add(, , "Second value")
        ' Add two subitems for that item
        itmX.SubItems(1) = "Second value subitem 1"
        itmX.SubItems(2) = "Second value subitem 2"
        
        ' Add another main item
        Set itmX = ListView1.ListItems.Add(, , "Third value")
        ' Add two subitems for that item
        itmX.SubItems(1) = "Third value subitem 1"
        itmX.SubItems(2) = "Third value subitem 2"
        
        ReDim Preserve clr(ListView1.ListItems.Count)
        'Initialise the subclassing
        g_MaxItems = ListView1.ListItems.Count - 1
        g_addProcOld = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc)
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
      SetWindowLong hWnd, GWL_WNDPROC, g_addProcOld
    End Sub
    
    Private Sub ListView1_Click()
      If ListView1.SelectedItem.Index < 0 Then Exit Sub
      SetLIBackColor ListView1, ListView1.SelectedItem.Index, vbRed
    End Sub
    Module1.bas

    Code:
    Option Explicit
    
    ' bas module code
    ' hook system and color the backcolor of rows different colors
    ' as with hooking if you unload using the stop button of Visual Basic
    ' and you don't use the unloadquerry (the X on the form)
    ' then windows will crash
    '
      
        Public Const GWL_EXSTYLE = -20
        Public Const GWL_HINSTANCE = -6
        Public Const GWL_HWNDPARENT = -8
        Public Const GWL_ID = -12
        Public Const GWL_STYLE = -16
        Public Const GWL_USERDATA = -21
        Public Const GWL_WNDPROC = -4
        Public Const DWL_DLGPROC = 4
        Public Const DWL_MSGRESULT = 0
        Public Const DWL_USER = 8
    
        Public Const NM_CUSTOMDRAW = (-12&)
        Public Const WM_NOTIFY As Long = &H4E&
        Public Const CDDS_PREPAINT As Long = &H1&
        Public Const CDRF_NOTIFYITEMDRAW As Long = &H20&
        Public Const CDDS_ITEM As Long = &H10000
        Public Const CDDS_ITEMPREPAINT As Long = CDDS_ITEM Or CDDS_PREPAINT
        Public Const CDRF_NEWFONT As Long = &H2&
          Public Type NMHDR
        hWndFrom As Long   ' Window handle of control sending message
        idFrom As Long        ' Identifier of control sending message
        code  As Long          ' Specifies the notification code
      End Type
         ' sub struct of the NMCUSTOMDRAW struct
      Public Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
      End Type
         ' generic customdraw struct
      Public Type NMCUSTOMDRAW
        hdr As NMHDR
        dwDrawStage As Long
        hDC As Long
        rc As RECT
        dwItemSpec As Long
        uItemState As Long
        lItemlParam As Long
      End Type
         ' listview specific customdraw struct
      Public Type NMLVCUSTOMDRAW
        nmcd As NMCUSTOMDRAW
        clrText As Long
        clrTextBk As Long
        ' if IE >= 4.0 this member of the struct can be used
        'iSubItem As Integer
      End Type
    
      Public g_addProcOld As Long
      Public g_MaxItems As Long
      Public clr() As Long
      Public nItem As Integer
    
    Public Declare Function SetWindowLong Lib "user32.dll" Alias _
    "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, _
    ByVal dwNewLong As Long) As Long
    
       Public Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" _
      (Destination As Any, Source As Any, ByVal Length As Long)
      
     Public Declare Function CallWindowProc Lib "user32.dll" Alias _
      "CallWindowProcA" (ByVal lpPrevWndFunc As Long, _
      ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, _
      ByVal lParam As Long) As Long
       Public Function WindowProc(ByVal hWnd As Long, _
    ByVal iMsg As Long, ByVal wParam As Long, _
    ByVal lParam As Long) As Long
    
      Select Case iMsg
        Case WM_NOTIFY
          Dim udtNMHDR As NMHDR
          CopyMemory udtNMHDR, ByVal lParam, 12&
                With udtNMHDR
            If .code = NM_CUSTOMDRAW Then
              Dim udtNMLVCUSTOMDRAW As NMLVCUSTOMDRAW
              CopyMemory udtNMLVCUSTOMDRAW, ByVal lParam, Len(udtNMLVCUSTOMDRAW)
                With udtNMLVCUSTOMDRAW.nmcd
      Select Case .dwDrawStage
          Case CDDS_PREPAINT
               WindowProc = CDRF_NOTIFYITEMDRAW
                    Exit Function
          Case CDDS_ITEMPREPAINT
               If clr(.dwItemSpec) <> 0 Then
    'change the color of the text display if wanted
    'udtNMLVCUSTOMDRAW.clrText = vbBlue
                   udtNMLVCUSTOMDRAW.clrTextBk = clr(.dwItemSpec)
                   CopyMemory ByVal lParam, udtNMLVCUSTOMDRAW, Len(udtNMLVCUSTOMDRAW)
           End If
               WindowProc = CDRF_NEWFONT
                  Exit Function
          End Select
              End With
            End If
          End With
      End Select
      WindowProc = CallWindowProc(g_addProcOld, hWnd, iMsg, wParam, lParam)
    End Function
    
    Public Sub SetLIBackColor(lv As ListView, nItem As Integer, BkColor As Long)
       clr(nItem - 1) = BkColor
       lv.Refresh
    End Sub
    
    Public Sub SetLIForeColor(lv As ListView, ForeColor As Long)
       clr(nItem - 1) = ForeColor
       lv.Refresh
    End Sub
    This colors the rows with alternate colors when you select it and click the button.
    Last edited by dglienna; October 16th, 2007 at 12:21 PM.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  9. #9
    Join Date
    Nov 2002
    Posts
    278

    Re: ListView control question

    As easy is this may seem, I am really struggling with this. Was the ListView made buy Microsoft or some other company because the little documentation that I can find about it seems incomplete and lacking examples and non-intuitive. Anyway, the URL below shows a picture of what I am trying to arrive at, except for now, I don’t even want to mess around with the icons. I just want to know how to get the listview control to take on a look similar to the picture below, where there are two columns at the top (with captions ‘Name’ and ‘Description’ and items vertically listed under the columns, with text in each column. If anyone has the most basic example of how to do this I would greatly appreciate it!? Or if there is a VB control that can do this that’s easier to use?

    http://i120.photobucket.com/albums/o...t/ListView.jpg

    .

  10. #10
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: ListView control question

    Try this:
    Attached Files Attached Files
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  11. #11
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: ListView control question

    Dino, as I figure from your description you simply want a different ViewStyle setting. Just go back to the first "Stuff" and set ListView.View = lvwReport

    You can do that as well in the ListViews properties when in design mode.
    Also in design mode you can determine the number of columns as wanted.
    Follow DGliennas code to enter data to the other columns.

  12. #12
    Join Date
    Nov 2004
    Posts
    8

    Re: ListView control question

    Code works well

    I need to know how can I revert back the original forecolor before applying the alternate row color. It was blue previously but changed to black after applying alternate row coloring..

  13. #13
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: ListView control question

    I think you are in the wrong thread.

  14. #14
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: ListView control question

    acpt, this thread is 3 Years old! Please start a new thread.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured