Listviewcontrol and background color
-
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!
-
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!
-
Here's one good example what you can do with ownerdraw Extended ListView[^]
-
Hi, Is there no alternative than to use ownerdraw? It would be nice to catch the repaint event and put there the coloring stuff.
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(); }
-
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(); }
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.
-
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.
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.
-
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.
-
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?
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.
-
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.
-
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.
-
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.
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
-
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
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.