Change treeview image

  • Thread starter Thread starter Anthony Boudouvas
  • Start date Start date
A

Anthony Boudouvas

Hi to all,

i have a treeview that i put some nodes in it with their repsective images.
If i try to change the image and set it to some other ImageList index,
nothing happens.

The code i use is:

private void ChangeServantStatus(string servant, ServantStatus status)
{
foreach (TreeNode n in treServants.Nodes[0].Nodes)
{
if (n.Text == servant)
{
n.ImageIndex = 4;
n.SelectedImageIndex = 4; break;
}
}
return;
}


I search ms support and i found the following article
"PRB: Windows Forms TreeView Control Does Not Handle State Image Lists in
Visual C# .NET"
that contains stuff like Interop Services, IntPtr, and SendMessage calls.

I do not think that there is no other way to handle this,
can anyone help me with this?
I just need a simple way to change an image in a TreeView.

thanks a lot for any help


anthonyb
 
If you are trying to specify image indices in a DIFFERENT
ImageList, than you need to change the ImageList property
for the TreeView to the ImageList your trying to get the
images from.

ie.

private void ChangeServantStatus(string servant,
ServantStatus status)
{
// Change the TreeView's ImageList Property.
// If your TreeView is named treeView1, and the
// second ImageList is imageList2, then it would be...
treeView1.ImageList = imageList2;

foreach (TreeNode n in treServants.Nodes[0].Nodes)
{
if (n.Text == servant)
{
n.ImageIndex = 4;
n.SelectedImageIndex = 4; break;
}
}
return;
}


If that is infact what you are trying to do, I would
suggest putting all the images in the same ImageList and
just reference the index you want and not worry about
having to change the ImageList.

Keep in mind that in your sample code, it will only
change the first occurance of a TreeNode with the text
to "servant" (don't forget your quotes). If you wanted to
change all TreeNode's with that text, then remove the
break in the if statement.

Hope this helps,
Jacob
 
Back
Top