Windows Develop Bookmark and Share   
 index > Windows Forms General > Repainting A ListView Items
 

Repainting A ListView Items

I have a program that saves the items that the user typed in (because the list can get rather large) to disk. Once the user has selected the item the background turns a color to let the user know that the item has been used (some items can be used more then once which is why I do not remove it completely)

My question is...how can i force a repaint of the control and change the backcolors on the appropriate list items?

1. calling repainInputControl() does not work after opening up the users saved document (if I manually click an item then it repaints correctly)
2. calling lstInput.Refresh() does not work
3. calling lstInput.Invalidate() or .Invalidate(true) doesnt work





private void repaintInputControl()
{
string sAlreadyMapped = "";
string sCurrentItem = "";
int iPos = 0;

if (lstInput.SelectedItems.Count > 0)
{
lstInput.BeginUpdate();
for (int j = 0; j < lstMappedVariables.Items.Count; j++)
{
sAlreadyMapped += lstMappedVariables.Items[j].SubItems[0].Text + ", ";
}

for (int j = 0; j < lstInput.Items.Count; j++)
{
sCurrentItem = lstInput.Items[j].Text;
iPos = sAlreadyMapped.IndexOf(sCurrentItem + ",");
if (iPos > -1)
{
lstInput.Items[j].BackColor = colorAlreadyUsed;
}
else
{
lstInput.Items[j].BackColor = colorNotUsed;
}
}
lstInput.SelectedItems[0].BackColor = colorCurrentlySelected;
}
lstInput.EndUpdate();
}
  • Moved byHarry ZhuMSFTFriday, September 11, 2009 4:26 AMrelating to listview control (From:Visual C# General)
  •  
LearningVisualC2005  Thursday, September 10, 2009 4:05 AM
Hello,

Do you mean when you update something, the ListView's items content should be changed accordingly?

When you call ListView.Refresh method, it does force ListView to repaint. But if you don't change the content of these items, it will remain unchanged.

According to your code, I think "colorAlreadyUsed" is the color represent the used items and "colorNotUsed" represent unused items. After the loop, these item's color have been set. Then the first item of ListView always show "colorCurrentlySelected". Right? You can try to call lstInput.Refresh at the end of repaintInputControl method. Does it work?

Sincerely,
Kira Qian
Please remember to mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework!
Kira Qian  Monday, September 14, 2009 6:08 AM

You can use google to search for other answers

Custom Search

More Threads

• How to increase the clipping area over the amount given by onpaint?
• transactions?
• How to implement 30 day trial period?
• Collect all app progress info to show on one status window thru event/message pump?
• Control message processing question
• Context Menu Strip with Windows Vista look and feel (I also want to use icons)
• Ascii File Save Problems
• Cursor is Where?
• Double visibility between a form and a class that manages events
• Upgrading to Windows Server 2008 w/ "Servers in the Cloud"