Popełniałem błąd przy tworzeniu węzłów drzewa, nie można korzystać z konstruktora pobierającego String lecz tego biorącego TreeNode.
Przykład widziałem... ale prosty to on nie jest, łatwiej jest wykorzystać istniejące pole Tag obiektu TreeNode.
Jak by ktoś na przyszłość potrzebował wiedzieć to załączam kod, który wszystko wyjaśnia:
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.treeView1 = new System.Windows.Forms.TreeView();
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(8, 8);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(184, 248);
this.treeView1.TabIndex = 0;
this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
//
// button1
//
this.button1.Location = new System.Drawing.Point(208, 120);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 24);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 264);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(272, 20);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "textBox1";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(8, 288);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(272, 20);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "textBox2";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 317);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.treeView1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
// Create a new ArrayList to hold the Customer objects.
private ArrayList customerArray = new ArrayList();
private void FillMyTreeView()
{
// Add customers to the ArrayList of Customer objects.
for(int x=0; x<1000; x++)
{
customerArray.Add(new Customer("Customer" + x.ToString()));
}
// Add orders to each Customer object in the ArrayList.
foreach(Customer customer1 in customerArray)
{
for(int y=0; y<15; y++)
{
customer1.CustomerOrders.Add(new Order("Order" + y.ToString()));
}
}
// Display a wait cursor while the TreeNodes are being created.
// Cursor.Current = new Cursor("MyWait.cur");
// Suppress repainting the TreeView until all the objects have been created.
treeView1.BeginUpdate();
// Clear the TreeView each time the method is called.
treeView1.Nodes.Clear();
// Add a root TreeNode for each Customer object in the ArrayList.
TreeNode tmp;
foreach(Customer customer2 in customerArray)
{
tmp = new TreeNode(customer2.CustomerName);
tmp.Tag = customer2;
treeView1.Nodes.Add(tmp);
// Add a child treenode for each Order object in the current Customer object.
foreach(Order order1 in customer2.CustomerOrders)
{
tmp = new TreeNode(customer2.CustomerName + "." + order1.OrderID);
tmp.Tag = order1;
treeView1.Nodes[customerArray.IndexOf(customer2)].Nodes.Add(
tmp);
}
}
// Reset the cursor to the default for all controls.
Cursor.Current = Cursors.Default;
// Begin repainting the TreeView.
treeView1.EndUpdate();
}
private void button1_Click(object sender, System.EventArgs e)
{
FillMyTreeView();
}
private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
if (e.Node.Tag is Customer) textBox1.Text = ((Customer)e.Node.Tag).CustomerName;
if (e.Node.Tag is Order) textBox1.Text = ((Order)e.Node.Tag).OrderID;
if (e.Node.Tag is Order) textBox2.Text = e.Node.Parent.Text;
}
}
public class Order
{
public String OrderID;
public Order (String ON)
{
OrderID = ON;
}
}
public class Customer
{
public String CustomerName;
public ArrayList CustomerOrders;
public Customer ()
{
CustomerOrders = new ArrayList();
}
public Customer (String CN)
{
CustomerName = CN;
CustomerOrders = new ArrayList();
}
}