dcsimg
Results 1 to 4 of 4

Thread: VB6 - Listview Rows Backcolor (Without subclassing)

  1. #1

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    VB6 - Listview Rows Backcolor (Without subclassing)

    I have come across a piece of code that enables you to change the Backcolor property of any individual row, or rows, without the need for using subclassing. This is a big advantage for everybody, but especially those, like myself, with very limited knowledge of subclassing, but a better knowledge of the perils that it can bring.

    I have used this code and altered it a bit to make a fully commented example project to show how easy it is to use.

    It requires just the addition of a Picturebox control to your form and thats it, nothing more complicated than that.

    It uses the Line Method to fill in the colour in the specified places in the picturebox, i.e. the rows.
    See Here for information about the Line method (MSDN)

    You would then add a hidden column to the listview to store the value of the Checkbox, which would then be saved to, and loaded from your database when loading the other columns in your listview. Simple

    Limitations:
    I have noticed that when you get to more that approx 200 records, the colouring or the rows starts to go outside of the lines for each row.
    To try this, just change this line in the Form_Load event in the attached project:

    Code:
    '\\Load data into Listview
    For i = 0 To 200  <--Change to 1000
    The original author of this code is De ming Shang, who I give full credit to for such a simple, but effective alternative to subclassing.

    Screenshot of Listview with coloured rows.
    Attached Images Attached Images  
    Attached Files Attached Files
    Last edited by aikidokid; Feb 23rd, 2008 at 03:57 PM.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  2. #2
    Registered User
    Join Date
    Nov 2018
    Posts
    2

    Thumbs up Re: VB6 - Listview Rows Backcolor (Without subclassing)

    Quote Originally Posted by aikidokid View Post
    I have come across a piece of code that enables you to change the Backcolor property of any individual row, or rows, without the need for using subclassing. This is a big advantage for everybody, but especially those, like myself, with very limited knowledge of subclassing, but a better knowledge of the perils that it can bring.

    I have used this code and altered it a bit to make a fully commented example project to show how easy it is to use.

    It requires just the addition of a Picturebox control to your form and thats it, nothing more complicated than that.

    It uses the Line Method to fill in the colour in the specified places in the picturebox, i.e. the rows.
    See Here for information about the Line method (MSDN)

    You would then add a hidden column to the listview to store the value of the Checkbox, which would then be saved to, and loaded from your database when loading the other columns in your listview. Simple

    Limitations:
    I have noticed that when you get to more that approx 200 records, the colouring or the rows starts to go outside of the lines for each row.
    To try this, just change this line in the Form_Load event in the attached project:

    Code:
    '\\Load data into Listview
    For i = 0 To 200  <--Change to 1000
    The original author of this code is De ming Shang, who I give full credit to for such a simple, but effective alternative to subclassing.

    Screenshot of Listview with coloured rows.
    Thanks Its work for me.

  3. #3
    Registered User
    Join Date
    Nov 2018
    Posts
    2

    Re: VB6 - Listview Rows Backcolor (Without subclassing)

    Thank you very much this solution works form me.

  4. #4
    New Member
    Join Date
    Oct 2013
    Posts
    10

    Re: VB6 - Listview Rows Backcolor (Without subclassing)

    Thanks for posting.

    To keep the row colouring aligned with the rows, see below. The following sub will highlight checked items up until the maximum allowable height of the picturebox.

    Code:
    Private Sub SetListBackground()
        Dim i As Long
        Dim lw As Single  'list width
        Dim lh As Single  'list height
        Dim rh As Single  'row height
        Dim t1 As Single  'top of row 1
        Dim ti As Single  'top of row i
        Dim bh As Single  'background height
        Dim col As Long   'colour of row
        Dim cnt As Long   'number of rows
        
        cnt = lvwBackColour.ListItems.Count
        
        If cnt > 0 Then
        
            picBG.Cls
            picBG.ScaleMode = Me.ScaleMode
    
            lw = lvwBackColour.Width
            
            t1 = lvwBackColour.ListItems(1).Top
            rh = lvwBackColour.ListItems(1).Height
            lh = rh * cnt
            
            picBG.Width = lw
            picBG.Height = lh
            
            bh = picBG.Height
            
            'note: picturebox has a maximum possible height
            '      if list is too long, bh < lh (items at end of list will not be coloured)
            
            For i = 1 To cnt
                ti = lvwBackColour.ListItems(i).Top - t1
                If ti > bh Then
                    'past bottom of background picturebox.
                    'Debug.Print "Row " & i & " onwards cannot be highlighted."
                    Exit For
                Else
                    If lvwBackColour.ListItems(i).Checked = True Then
                        col = &H80FFFF
                    Else
                        col = &HFFFFFF
                    End If
                    picBG.Line (0, ti)-(lw, ti + rh), col, BF
                End If
            Next i
            
            lvwBackColour.Picture = picBG.Image
        Else
            lvwBackColour.Picture = Nothing
        End If
    End Sub

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