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

    List View alternate row back color

    I have a solution to set alternate row back color in a ListView control.
    But the problem here is that the alternate row color gets applied to entire listview i.e. alternate row color also gets applied to rows that do not have the data.
    How do I prevent this from happening.

    Below is the code I am using:

    Code:
     Private Sub AltLVBackground(lv As ListView, _
        ByVal BackColorOne As OLE_COLOR, _
        ByVal BackColorTwo As OLE_COLOR)
    
    Dim lSM     As Byte
    Dim picAlt  As PictureBox
        With lv
            If .View = lvwReport And .ListItems.Count Then
                Set picAlt = Me.Controls.Add("VB.PictureBox", "picAlt")
                lSM = .Parent.ScaleMode
                .Parent.ScaleMode = vbTwips
                .PictureAlignment = lvwTile
                lH = .ListItems(1).Height
                With picAlt
                    .BackColor = BackColorOne
                    .AutoRedraw = True
                    .Height = lH * 2
                    .BorderStyle = 0
                    .Width = 10 * Screen.TwipsPerPixelX
                    picAlt.Line (0, lH)-(.ScaleWidth, lH * 2), BackColorTwo, BF
                    Set lv.Picture = .Image
                End With
                Set picAlt = Nothing
                Me.Controls.Remove "picAlt"
                lv.Parent.ScaleMode = lSM
            End If
        End With
    End Sub
    Last edited by HanneSThEGreaT; May 18th, 2010 at 10:33 AM.

  2. #2
    Join Date
    Nov 2004
    Posts
    8

    Re: List View alternate row back color

    I have a code that uses sublclassing but the problem is that I am using a dll and passing the listview as object.

    Can't find a way to implement it using Subclassing as Windows calls is not working ?

    Can you help if this can be done ?

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

    Re: List View alternate row back color

    Well, this a rather clever and unconventional way of coloring rows alternately.
    I have attached a little sample (got it from the web somewhere), which uses customdrawing and subclassing to achieve back-coloring. Can be adapted easily to alternately color the rows.
    Attached Files Attached Files

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: List View alternate row back color

    acpt, please make use of [CODE] tags when posting code next time.

    Thanks

  5. #5
    Join Date
    Nov 2004
    Posts
    8

    Re: List View alternate row back color

    WoF,

    I tried implementing it using a DLL. Doesn't work.

    Do you have a sample code with a DLL ?

    Regards,

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

    Re: List View alternate row back color

    Well, I don't have a sample with a dll. Why do you want a dll? The sample I gave does not need any dll. Everything is self contained within the project. No dll reqired.
    Did you try the sample at all?

    Or maybe I do not understand you corrctly concerning the dll...

  7. #7
    Join Date
    Nov 2004
    Posts
    8

    Re: List View alternate row back color

    WoF,

    Well the reason I am using DLL because we have multiple projects ( exes) which are interlinked and use user controls, dll etc. All projects have list views in them.

    I wanted a single source of applying alternate row coloring to ease the process of applying and later if someone wants to change the alternate row colors it can be done easily.

    Would appreciate if you can provide a sample source using dll

    I am stuck here for the past few days.

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

    Re: List View alternate row back color

    It is not easy to write a standard dll with VB6.
    However you could write an ActiveX-dll. The idea would be to write a class of a ListViewColorExtender object provided by this dll. You'd have then to instantiate such an object for each ListView you want to color.
    In the end, if you want to use this within different projects, you'd have to change their code anyway. There is no chance of writing a dll which automatically does this coloring in any of your projects.

    If you look at the sample project, all relevant code is in Module1. You only have to add that module to your project and make the apropriate calls in your code.

  9. #9
    Join Date
    May 2010
    Posts
    12

    Re: List View alternate row back color

    If you want to have a solution for all of your projects, try creating a control that warps the original ListView and adds the AlternateRow coloring.

    Maybe have a look at http://www.vbaccelerator.com/home/index.asp.

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