12 Replies - 2282 Views - Last Post: 26 July 2014 - 05:00 PM Rate Topic: -----

#1 rfresh   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 146
  • Joined: 23-August 12

Why is my node click event changing the node icon?

Posted 26 July 2014 - 02:01 PM

I'm having a hard time figuring how to get my imagelist index 3 icon to display in the nodes "N1" and "V Speeds" below?
So, as you can see in the attachment, the closed folder icon is currently shown which is index 0 in the imagelist. But I want index icon 2 to show in these two nodes.

Thank you for any help.

            treeView.BeginUpdate();
            treeView.Nodes.Clear();
            treeView.Nodes.Add(new TreeNode("Checklist"));
            treeView.Nodes.Add(new TreeNode("Tutorial #1"));
            treeView.Nodes.Add(new TreeNode("Tutorial #2"));
            treeView.Nodes[0].Nodes.Add(new TreeNode("Preflight"));
            treeView.Nodes[0].Nodes.Add(new TreeNode("Before Start"));
            treeView.Nodes[0].Nodes.Add(new TreeNode("Secure"));
            treeView.Nodes[1].Nodes.Add(new TreeNode("CDU Preflight"));
            treeView.Nodes[1].Nodes.Add(new TreeNode("POS Init"));
            treeView.Nodes[2].Nodes.Add(new TreeNode("N1"));
            treeView.Nodes[2].Nodes.Add(new TreeNode("V Speeds"));
            treeView.EndUpdate();

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Why is my node click event changing the node icon?

#2 andrewsw   User is online

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Why is my node click event changing the node icon?

Posted 26 July 2014 - 02:19 PM

Well I haven't tried it, but I suspect this'll work:
treeView.Nodes[2].Nodes.Add(new TreeNode("N1"));
treeView.Nodes[2].Nodes[0].ImageIndex = 2;
treeView.Nodes[2].Nodes.Add(new TreeNode("V Speeds"));
treeView.Nodes[2].Nodes[1].ImageIndex = 2;

Was This Post Helpful? 0
  • +
  • -

#3 rfresh   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 146
  • Joined: 23-August 12

Re: Why is my node click event changing the node icon?

Posted 26 July 2014 - 02:24 PM

Yes...that worked...thank you...
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is online

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Why is my node click event changing the node icon?

Posted 26 July 2014 - 02:31 PM

Cool :)
Was This Post Helpful? 0
  • +
  • -

#5 rfresh   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 146
  • Joined: 23-August 12

Re: Why is my node click event changing the node icon?

Posted 26 July 2014 - 03:02 PM

Why does the node icon change from the document icon (index 2) to the closed folder icon (index 1) when I click on a level 1 node?

I click on the CDU Preflight node and I don't want the icon to change. I want this node level to keep the doc icon but it doesn't. I believe I am setting the icon index 2 correctly in the code below for the CDU Preflight node but apparently I'm not doing something right?

I think I'm forcing the image index 2 icon to display but clearly that is not happening.

Thank you...

        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string tempString1 = e.Node.Level.ToString();
            string tempString2 = e.Node.Index.ToString();
            string tempString3 = e.Node.Text;
            if (e.Node.Level == 0) // Tutorial #1
            {
                e.Node.ImageIndex = 1; // open folder
            }
            if (e.Node.Level == 1) // CDU Preflight
            {
                e.Node.ImageIndex = 2; // doc
            }
        }

Attached image(s)

  • Attached Image

This post has been edited by rfresh: 26 July 2014 - 03:03 PM

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is online

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Why is my node click event changing the node icon?

Posted 26 July 2014 - 03:11 PM

On expand/collapse the images revert to a default (an assumed default). There isn't a property you can set to over-rule this behaviour; instead, you use expand/collapse events:
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
    {
        e.Node.ImageIndex = 2;
    }

Once this works you'll probably need to use all of these events:

Quote

BeforeCollapse
BeforeExpand
AfterCollapse
AfterExpand


There is also SelectedImageIndex to be aware of:
treeNode.SelectedImageIndex = 1;

I don't think this is suitable for your circumstance, unless you want the same image for whatever node is selected.

This post has been edited by andrewsw: 26 July 2014 - 03:14 PM

Was This Post Helpful? 1
  • +
  • -

#7 andrewsw   User is online

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Why is my node click event changing the node icon?

Posted 26 July 2014 - 03:18 PM

I see you've added some code to your previous post. You probably need to use BeforeExpand, etc., instead of AfterSelect. The Expand/Collapse events probably (I'm guessing) happen after AfterSelect, reverting the image again.
Was This Post Helpful? 0
  • +
  • -

#8 rfresh   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 146
  • Joined: 23-August 12

Re: Why is my node click event changing the node icon?

Posted 26 July 2014 - 03:29 PM

The problem is that these events

BeforeCollapse
BeforeExpand
AfterCollapse
AfterExpand



don't fire when I click on the CDU Preflight node. When I click on that node, the closed folder icon appears and I lose the doc icon. I'm not sure why that is.

When I looked at the Click and MouseClick events, there was no "TreeViewEventArgs e" parameters in which to determine the node.Level and node.Index properties.

This post has been edited by rfresh: 26 July 2014 - 03:30 PM

Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is online

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Why is my node click event changing the node icon?

Posted 26 July 2014 - 03:44 PM

Yes, Click and MouseClick don't have these values, the TreeView has a specific set of events that do provide these values.

Yes, of course, the node you are talking about isn't expandable - I didn't notice.

So it looks like you'll have to revisit your code for AfterSelect. Put a MessageBox in there to make sure it runs:
MessageBox.Show(e.Node.Level.ToString());


Also check the Properties Window for the full list of available events.

This code runs for me when I click on the CDU Preflight node:
    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) {
        MessageBox.Show(e.Node.Level.ToString());
    }

Was This Post Helpful? 0
  • +
  • -

#10 rfresh   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 146
  • Joined: 23-August 12

Re: Why is my node click event changing the node icon?

Posted 26 July 2014 - 03:53 PM

Well, I think its pretty odd.

The code below does not produce the imagelist index 2 icon which is the doc icon when I click on the CDU Preflight node.

When I click on the CDU Preflight node, the existing icon (the doc icon) disappears and is replaced by the closed folder icon.

So I'm really puzzled now.

        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            // these 3 lines let me see the values when I'm debugging
            string tempString1 = e.Node.Level.ToString(); // 1
            string tempString2 = e.Node.Index.ToString(); // 0
            string tempString3 = e.Node.Text; // == CDU Preflight
            if (e.Node.Level == 1) // CDU Preflight
            {
                e.Node.ImageIndex = 2; // doc
                treeView.Nodes[1].Nodes[e.Node.Index].ImageIndex = 2;
            }
        }


Was This Post Helpful? 0
  • +
  • -

#11 andrewsw   User is online

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Why is my node click event changing the node icon?

Posted 26 July 2014 - 04:17 PM

I'm puzzled too ;)/>

That code with e.Node.ImageIndex = 2; was changing the image but only when I click away from the node.

There is a StateImageList so I added one and used e.Node.StateImageIndex = 2;. This does add the image straight-away.. but it keeps the old image :helpsmilie: as well. (Invalidating the TreeView didn't help.)

So I removed the original ImageList and now it does change the image (AfterSelect) .. but it no longer has the default images.

Curious..

This post has been edited by andrewsw: 26 July 2014 - 04:18 PM

Was This Post Helpful? 0
  • +
  • -

#12 andrewsw   User is online

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Why is my node click event changing the node icon?

Posted 26 July 2014 - 04:28 PM

Right.. on form-load set a SelectedImageIndex. Then:
    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) {
        if (e.Node.Level == 1) {
            treeView1.SelectedImageIndex = 2;
        } else {
            treeView1.SelectedImageIndex = 1;   // the default
        }     
    }

Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Why is my node click event changing the node icon?

Posted 26 July 2014 - 05:00 PM

Its screwy behavior like this from old WinForms as to why it isn't used for new projects anymore and just relegated to supporting legacy applications. In *this* decade we use WPF and just write a Control Template to define how we want the control to look.

Please don't open multiple threads for the same issue. I'll combine these for you.

Please don't open multiple threads for the same issue. I'll combine these for you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1