469,934 Members | 2,706 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

Clarifying creation/destruction of TreeVew.Handle and collapse behaviour


I have a WinForms application (C#, VS 2005) that consists of a treeview control and a notify icon. Below is a complete example of this application. The treeview has one root node and one child node. When I close the form, it will not be closed but minimised to the system tray. A double-click on the notify icon wakes it up and displays it again.

My expection was that the treeview handle would be destroyed when the form is sent to the system tray and re-created when shown again. Also, I expected my treeview to display my nodes the way they were before I minimized the form. Here is what I actually observed:

1. When the form is sent to the system tray the treeview handle will be destroyed and re-created.
2. When the form is shown again the treeview handle will also be destroyed and re-created.
3. When all tree nodes are expanded and the focus is on the root node, minimising and showing the form leads to a collapsed treeview.
4. When all tree nodes are expanded and the focus is on the child node, minimising and showing the form does not collapse the treeview. I assume that showing the treeview again results in a collapse of only the focused node. Is that correct?

Are there any reasons why the handle will be destroyed and re-created so frequently?
Also, why is there a built in logic to collapse tree nodes at all? It should be mentioned that the BeforeCollapse or AfterCollapse events will not be raised.

This scenario does not produce any errors but I find it annoying that my treeview does not come back from the system tray the way it was before and I would like to understand why this is not the case.

Thanks a lot,


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
public partial class Form1 : Form
public Form1()

this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
this.notifyIcon1.DoubleClick += new EventHandler(notifyIcon1_DoubleClick);
this.treeView1.HandleDestroyed += new EventHandler(treeView1_HandleDestroyed);
this.treeView1.HandleCreated += new EventHandler(treeView1_HandleCreated);
this.treeView1.BeforeCollapse += new TreeViewCancelEventHandler(treeView1_BeforeCollaps e);
this.treeView1.AfterCollapse += new TreeViewEventHandler(treeView1_AfterCollapse);

this.treeView1.Nodes.Add(new TreeNode("Parent", new TreeNode[] { new TreeNode("Child") }));

void notifyIcon1_DoubleClick(object sender, EventArgs e)
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;

void Form1_FormClosing(object sender, FormClosingEventArgs e)
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;

void treeView1_HandleCreated(object sender, EventArgs e)

void treeView1_HandleDestroyed(object sender, EventArgs e)

void treeView1_AfterCollapse(object sender, TreeViewEventArgs e)

void treeView1_BeforeCollapse(object sender, TreeViewCancelEventArgs e)
Aug 9 '07 #1
0 1259

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by | last post: by
3 posts views Thread by SK | last post: by
1 post views Thread by Ken Dopierala Jr. | last post: by
5 posts views Thread by Parapura Rajkumar | last post: by
2 posts views Thread by Joe Stateson | last post: by
31 posts views Thread by Tom P. | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.