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

    Question ListViewItems background colors

    Hi,

    I create file list and place it in a listview. When I'm adding ListViewItems to the list, I make one line white background, next line grey for easy viewing. I use this code to do so:

    Code:
    ListViewItem l_lviFileData = new ListViewItem ( new string [] { fi.Name.ToString(), fi.LastWriteTime.ToString() } );
                if ( l_iAddedItems%2 == 0 ) l_lviFileData.BackColor =  Color.FromArgb ( 245 , 245 , 245 ); //VERY LIGHT GREY
                p_lvListview.Items.Add ( l_lviFileData );
    Works like a charm! But then I want to sort this list and after the sort, the coloring is (of course) completely whack. I get several lines of grey or white after another.

    Does anyone has a good idea how to do this? I was thinking about a loop after the sort that would repeat the background coloring but it didn't work.

    This, I tried:

    Code:
       int i = 0;
          foreach ( ListViewItem lvi in lstFiles.Items)
          {
            lstFiles.Items.RemoveAt(i);
            if ( i%2 == 0 ) lvi.BackColor =  Color.FromArgb ( 245 , 245 , 245 ); //VERY LIGHT GREY
            lstFiles.Items.Insert(i , lvi);
            i++;
          }
    Any help or hints?

  2. #2
    Join Date
    Oct 2001
    Posts
    80

    Re: ListViewItems background colors

    hahahaaaaa!!! I found it!

    Half of the lines were already grey and I was adding more grey lines to it without making the other white again.

    This was the was to go:

    Code:
    int i = 0;
          foreach ( ListViewItem lvi in lstFiles.Items)
          {
            
            if ( i%2 == 0 ) 
              lvi.BackColor =  Color.FromArgb ( 245 , 245 , 245 ); //VERY LIGHT GREY
            else
              lvi.BackColor =  Color.FromArgb ( 255 , 255 , 255 ); //WHITE
            i++;
          }
    That was a bit silly of me...

  3. #3
    Join Date
    Jul 2005
    Posts
    3

    Exclamation ListView Item loses backcolor

    Hi all,

    I have a list view with HideSelection set to False.I have changed the backcolors of the listview items alternately(i.e zebra effect).Now if the user clicks in the list view itself in some place where there are no items , the selected listview item loses its backcolor.However it gets restored if someother item is selected in the list view.
    This is reproducible in VS2003 on an XP machine.I tried overriding the OnPaint() method so that explicitly the backcolors are assigned to the list view items.But it was of no use.So is it a bug in ListView???

    It wud really help if any body could tell me as to how to go about this problem
    Thanks and Regards
    Amita

  4. #4
    Join Date
    Jul 2005
    Posts
    3

    Question Re: ListViewItems background colors

    hi ,
    I am trying to do something similar to what u have done above, altertnating the backcolors of the listview item.But the problem is if i click o0n the empty space in the list view the colors go fora toss.Now if i add a new entry to the list view the colors are not restored????

  5. #5
    Andy Tacker is offline More than "Just Another Member"
    Join Date
    Jun 2001
    Location
    55°50' N 37°39' E
    Posts
    1,503

    Re: ListViewItems background colors

    You can also refer to this thread
    If you think you CAN, you can, If you think you CAN'T, you are probably right.

    Have some nice Idea to share? Write an Article Online or Email to us and You may WIN a Technical Book from CG.

  6. #6
    Andy Tacker is offline More than "Just Another Member"
    Join Date
    Jun 2001
    Location
    55°50' N 37°39' E
    Posts
    1,503

    Re: ListViewItems background colors

    Merged two threads... I think that's ok with you ABK?
    If you think you CAN, you can, If you think you CAN'T, you are probably right.

    Have some nice Idea to share? Write an Article Online or Email to us and You may WIN a Technical Book from CG.

  7. #7
    Join Date
    Jul 2005
    Posts
    3

    Question Re: ListView Item loses backcolor

    Hi Andy,
    The code listed shows how to change the color for the subitems.But my problem is a different one.Let me explain a little more
    I change the list view item backcolors(alternately)i.e control and control light colors.This works fine.My requirement is that I have a button to add a new row.So when this is done the back color for the newly added row is updated accordingly.This too works fine
    But the problem is before adding a new row if the selection is on the item having controls light color, if i have clicked on an empty space in the list view the selected item loses its backcolor.It will be restored if I click on some other item in the listview.I tried explicitly updating or refreshing the list view.But it was of no use.
    So is this a bug in ListView???

  8. #8
    Join Date
    Sep 2007
    Posts
    4

    Re: ListView Item loses backcolor

    hello friends,

    i am also same kind of problem,

    when i change a color of any row of list view, it will bw shown only when select other one and then select it back,

    i have tried this with making hide selection true also,

    but does get,

    Do anyone has answer?

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