Hi,
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,
Marc.
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()
{
InitializeComponent();
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)
{
Console.WriteLine("HandleCreated");
}
void treeView1_HandleDestroyed(object sender, EventArgs e)
{
Console.WriteLine("HandleDestroyed");
}
void treeView1_AfterCollapse(object sender, TreeViewEventArgs e)
{
Console.WriteLine("AfterCollapse");
}
void treeView1_BeforeCollapse(object sender, TreeViewCancelEventArgs e)
{
Console.WriteLine("AfterCollapse");
}
}
}