All right....here is a repro:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace Proben
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.MainMenu mainMenu1;
private TreeNode node;
private EventHandler add, clear;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
add = new EventHandler(this.AddNode);
clear = new EventHandler(this.ClearColl);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.treeView1 = new System.Windows.Forms.TreeView();
//
// treeView1
//
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(8, 16);
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(224, 208);
//
// Form1
//
this.Controls.Add(this.treeView1);
this.Menu = this.mainMenu1;
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
Thread t = new Thread(new ThreadStart(this.Start));
t.Start();
}
private void AddNode(object sender, EventArgs e)
{
treeView1.Nodes.Add(node);
}
private void ClearColl(object sender, EventArgs e)
{
treeView1.BeginUpdate();
treeView1.SelectedNode = null;//bugfix for .net cf v1
treeView1.Nodes.Clear();
treeView1.EndUpdate();
}
void Start()
{
treeView1.Invoke(this.clear);
node = new TreeNode("");
TreeNode child = new TreeNode();
node.Nodes.Add(child);
treeView1.Invoke(add);
}
}
}
Actually, I found out that the behavior I described is caused by calling
the Nodes.Clear() method on an empty three before adding something to
the nodes collection. When I comment out the
treeView1.Invoke(this.clear);
line everything works fine.
Let me know how it works for you.
Best regards,
Tsviatko
Daniel Moth ÐÉÛÅÔ: