Change treeview image

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anthony Boudouvas

    Change treeview image

    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 ChangeServantSt atus(string servant, ServantStatus status)
    {
    foreach (TreeNode n in treServants.Nod es[0].Nodes)
    {
    if (n.Text == servant)
    {
    n.ImageIndex = 4;
    n.SelectedImage Index = 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


  • Jacob

    #2
    Change treeview image

    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 ChangeServantSt atus(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.Image List = imageList2;

    foreach (TreeNode n in treServants.Nod es[0].Nodes)
    {
    if (n.Text == servant)
    {
    n.ImageIndex = 4;
    n.SelectedImage Index = 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



    [color=blue]
    >-----Original Message-----
    >Hi to all,
    >
    >i have a treeview that i put some nodes in it with their[/color]
    repsective images.[color=blue]
    >If i try to change the image and set it to some other[/color]
    ImageList index,[color=blue]
    >nothing happens.
    >
    >The code i use is:
    >
    >private void ChangeServantSt atus(string servant,[/color]
    ServantStatus status)[color=blue]
    >{
    > foreach (TreeNode n in treServants.Nod es[0].Nodes)
    > {
    > if (n.Text == servant)
    > {
    > n.ImageIndex = 4;
    > n.SelectedImage Index = 4; break;
    > }
    >}
    >return;
    >}
    >
    >
    >I search ms support and i found the following article
    >"PRB: Windows Forms TreeView Control Does Not Handle[/color]
    State Image Lists in[color=blue]
    >Visual C# .NET"
    >that contains stuff like Interop Services, IntPtr, and[/color]
    SendMessage calls.[color=blue]
    >
    >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[/color]
    TreeView.[color=blue]
    >
    >thanks a lot for any help
    >
    >
    >anthonyb
    >
    >
    >
    >[/color]

    Comment

    Working...