Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Listviewcontrol and background color

Listviewcontrol and background color

Scheduled Pinned Locked Moved C#
helptutorialannouncement
12 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yustme
    wrote on last edited by
    #1

    Hi, I am trying to change the background color of the listview items depending on their text. The problem is, the items don't keep their background color after every repaint. I tried the refresh() and update() function to make the items keep their color, but that didn't work. I don't see any other function or property which might fix this problem i'm having. I have heard about 'onwerdraw' but i have no idea how to use that, googeling this up hasn't showed a few interesting examples or if it fixes this problem for that matter. If anyone has a success story about 'onwerdraw' with an example code, i'd really appreciate it. Or a suggestion about how to fix this issue. Thanks in advance!

    W 1 Reply Last reply
    0
    • Y Yustme

      Hi, I am trying to change the background color of the listview items depending on their text. The problem is, the items don't keep their background color after every repaint. I tried the refresh() and update() function to make the items keep their color, but that didn't work. I don't see any other function or property which might fix this problem i'm having. I have heard about 'onwerdraw' but i have no idea how to use that, googeling this up hasn't showed a few interesting examples or if it fixes this problem for that matter. If anyone has a success story about 'onwerdraw' with an example code, i'd really appreciate it. Or a suggestion about how to fix this issue. Thanks in advance!

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      Here's one good example what you can do with ownerdraw Extended ListView[^]

      Y 1 Reply Last reply
      0
      • W Wendelius

        Here's one good example what you can do with ownerdraw Extended ListView[^]

        Y Offline
        Y Offline
        Yustme
        wrote on last edited by
        #3

        Hi, Is there no alternative than to use ownerdraw? It would be nice to catch the repaint event and put there the coloring stuff.

        W 1 Reply Last reply
        0
        • Y Yustme

          Hi, Is there no alternative than to use ownerdraw? It would be nice to catch the repaint event and put there the coloring stuff.

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          Setting BackColor for each item is another way (which you have used). I made a little test and forcing refresh didn't make the items to loose their color. In what situation you face the problem? Test was (two buttons on a form and a listview):

            private void button1\_Click(object sender, EventArgs e) {
               ListViewItem lvItem;
          
               for (int i = 0; i < 10; i++) {
                  lvItem = new ListViewItem();
                  lvItem.Text = lvItem.GetHashCode().ToString();
                  lvItem.BackColor = Color.LightBlue;
                  listView1.Items.Add(lvItem);
               }
          
               lvItem = new ListViewItem();
               lvItem.Text = lvItem.GetHashCode().ToString();
               lvItem.BackColor = Color.Red;
               listView1.Items.Add(lvItem);
          
            }
            private void button2\_Click(object sender, EventArgs e) {
               this.Refresh();
            }
          
          Y 1 Reply Last reply
          0
          • W Wendelius

            Setting BackColor for each item is another way (which you have used). I made a little test and forcing refresh didn't make the items to loose their color. In what situation you face the problem? Test was (two buttons on a form and a listview):

              private void button1\_Click(object sender, EventArgs e) {
                 ListViewItem lvItem;
            
                 for (int i = 0; i < 10; i++) {
                    lvItem = new ListViewItem();
                    lvItem.Text = lvItem.GetHashCode().ToString();
                    lvItem.BackColor = Color.LightBlue;
                    listView1.Items.Add(lvItem);
                 }
            
                 lvItem = new ListViewItem();
                 lvItem.Text = lvItem.GetHashCode().ToString();
                 lvItem.BackColor = Color.Red;
                 listView1.Items.Add(lvItem);
            
              }
              private void button2\_Click(object sender, EventArgs e) {
                 this.Refresh();
              }
            
            Y Offline
            Y Offline
            Yustme
            wrote on last edited by
            #5

            Hi, I'll try to describe the situation. The items in the listview control can have several states. The states are defined in the 3rd column. Depending on that status text, i am giving the items a color. They do get the color but as soon as i select an item, the color is gone. And any other event on the listview control is making this happen too. I am looping through a switch statement and assigning there the back color for the items. After that, i refresh the listview control.

            W 1 Reply Last reply
            0
            • Y Yustme

              Hi, I'll try to describe the situation. The items in the listview control can have several states. The states are defined in the 3rd column. Depending on that status text, i am giving the items a color. They do get the color but as soon as i select an item, the color is gone. And any other event on the listview control is making this happen too. I am looping through a switch statement and assigning there the back color for the items. After that, i refresh the listview control.

              W Offline
              W Offline
              Wendelius
              wrote on last edited by
              #6

              I tested clicking and refreshing with the code I posted and no problems. Test case was:

                private void listView1\_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
                   foreach (ListViewItem lvItem in listView1.Items) {
                      lvItem.BackColor = Color.Purple;
                   }
                   listView1.Refresh();
                }
              

              If I understood correctly, this would be similar to your case So I suspect that the problem is somehow in the loop where you define (and possibly reset) the colors for each item.

              Y 1 Reply Last reply
              0
              • W Wendelius

                I tested clicking and refreshing with the code I posted and no problems. Test case was:

                  private void listView1\_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
                     foreach (ListViewItem lvItem in listView1.Items) {
                        lvItem.BackColor = Color.Purple;
                     }
                     listView1.Refresh();
                  }
                

                If I understood correctly, this would be similar to your case So I suspect that the problem is somehow in the loop where you define (and possibly reset) the colors for each item.

                Y Offline
                Y Offline
                Yustme
                wrote on last edited by
                #7

                Hi, I forgot to mention that i have to use delegates to access the listview control. I only assign the color at one place. That shouldn't be the problem right?

                W 1 Reply Last reply
                0
                • Y Yustme

                  Hi, I forgot to mention that i have to use delegates to access the listview control. I only assign the color at one place. That shouldn't be the problem right?

                  W Offline
                  W Offline
                  Wendelius
                  wrote on last edited by
                  #8

                  Delegates themselves are not the problem. However if you receive events in a wrong order, that could cause the problem (event to set colors before event to reset colors etc). Use debugger to confirm that your code is run in desired order. You could also use conditional breakpoints so that whenever a listviewitems background color changes, the debugger breaks. That could give you idea what's going wrong.

                  Y 1 Reply Last reply
                  0
                  • W Wendelius

                    Delegates themselves are not the problem. However if you receive events in a wrong order, that could cause the problem (event to set colors before event to reset colors etc). Use debugger to confirm that your code is run in desired order. You could also use conditional breakpoints so that whenever a listviewitems background color changes, the debugger breaks. That could give you idea what's going wrong.

                    Y Offline
                    Y Offline
                    Yustme
                    wrote on last edited by
                    #9

                    Hi, The coloring of the items happens in another class. The listview control is passed by ref to that other class. It shouldn't be a problem if i don't color the items in the Form class right? The delegates run in the right order.

                    W 1 Reply Last reply
                    0
                    • Y Yustme

                      Hi, The coloring of the items happens in another class. The listview control is passed by ref to that other class. It shouldn't be a problem if i don't color the items in the Form class right? The delegates run in the right order.

                      W Offline
                      W Offline
                      Wendelius
                      wrote on last edited by
                      #10

                      No, that isn't a problem as long as you work with the original ListViewItems (i.e. don't make copies). Did you try to break on refresh or use conditional breakpoints. I believe they would clarify this greatly.

                      Y 1 Reply Last reply
                      0
                      • W Wendelius

                        No, that isn't a problem as long as you work with the original ListViewItems (i.e. don't make copies). Did you try to break on refresh or use conditional breakpoints. I believe they would clarify this greatly.

                        Y Offline
                        Y Offline
                        Yustme
                        wrote on last edited by
                        #11

                        Hi, I did both, the colors really get assigned. But they are just not visible. If i minimize the window and bringing it back to front, i can see the colors but disappear really fast again. [EDIT 12:33h] Hi, It seems that i managed to fix this a bit. I created a paint event from the Form. I do all the coloring stuff in there now. It seems that it keeps the colors, but if i select an item, it loses its color. If i use the selectedIndexChanged event for the listview control, and i select an item, i lose all the colors on every item. I need a way to call the Paint event inside the selectedIndexChanged event of the listview control. This might do the trick. I tried this: this.frmMain_Paint(this.listviewcontrol, null); But that didn't work... [/Edit]

                        modified on Sunday, August 31, 2008 6:33 AM

                        W 1 Reply Last reply
                        0
                        • Y Yustme

                          Hi, I did both, the colors really get assigned. But they are just not visible. If i minimize the window and bringing it back to front, i can see the colors but disappear really fast again. [EDIT 12:33h] Hi, It seems that i managed to fix this a bit. I created a paint event from the Form. I do all the coloring stuff in there now. It seems that it keeps the colors, but if i select an item, it loses its color. If i use the selectedIndexChanged event for the listview control, and i select an item, i lose all the colors on every item. I need a way to call the Paint event inside the selectedIndexChanged event of the listview control. This might do the trick. I tried this: this.frmMain_Paint(this.listviewcontrol, null); But that didn't work... [/Edit]

                          modified on Sunday, August 31, 2008 6:33 AM

                          W Offline
                          W Offline
                          Wendelius
                          wrote on last edited by
                          #12

                          For some reason the coloring is reset after you assign the color. Did the example I posted work corectly? If it did, I suggest that you comment code out so that you get painting correclty and then try to bring back code only in small pieces. Also check the form's DoubleBuffered property. I believe it should be false.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups