我有一个TreeView控件的例子,但是不详细~~~~~
Treeview正在加载……效果演示
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TreeviewDemo
{
public partial class Form1 : Form
{
/*
* 增加一个子节点就提示一次.
* 然后开线程请求真实的子节点,完毕后替换提示节点。
*/
public class ThreadStartInfo
{
public TreeNode node = null;
//载入数据
public void LoadNode()
{
System.Threading.Thread.Sleep(5000);
string[] str = new string[]{"hgd","huih","kjhyiu","kuy","khio","jhgyut" };
this.AddNode(node,str);
}
delegate void newAddNode(TreeNode node,string[]str);
void AddNode(TreeNode node, string[] str)
{
if (this.node.TreeView.FindForm().InvokeRequired)
{
this.node.TreeView.FindForm().Invoke(new newAddNode(AddNode),new object[]{node,str});
}
else
{
node.Nodes.Clear();
foreach (string item in str)
{
node.Nodes.Add(item);
}
}
}
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.treeView1.Nodes.Add(new TreeNode("Root"));
this.treeView1.AfterSelect += new TreeViewEventHandler(treeView1_AfterSelect);
}
void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if (e.Node.Tag==null || e.Node.Tag.ToString()=="")
{
ThreadStartInfo tsi = new ThreadStartInfo();
tsi.node = e.Node;
//加载过的就不要在加载了
tsi.node.Tag = 1;
new System.Threading.Thread(new System.Threading.ThreadStart(tsi.LoadNode)).Start();
tsi.node.Nodes.Add("正在载入……");
}
}
}
}
Form1.Designer.cs
namespace TreeviewDemo
{
partial class Form1
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
this.treeView1 = new System.Windows.Forms.TreeView();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.Location = new System.Drawing.Point(12, 18);
this.treeView1.Name = "treeView1";
this.treeView1.Size = new System.Drawing.Size(243, 339);
this.treeView1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 369);
this.Controls.Add(this.treeView1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TreeView treeView1;
}
}
给你个我在用的 是带子节点的数。
private void BindPriceTreeView()
{
IList
TreeNode defaultnode = new TreeNode();
foreach (PricesetType item in list.Where(s => s.ParentId == null || s.ParentId == 0).ToList())
{
TreeNode node = new TreeNode();
node.Text = item.Name;
node.Value = item.TypeId.ToString();
node.ToolTip = string.Format("{0}", item.TypeId);
PriceCatalogTree.Nodes.Add(node);
this.GetPriceNode(node, list);
}
}
//添加子节点
protected void GetPriceNode(TreeNode node, IList
{
foreach (PricesetType item in list.Where(s => s.ParentId == int.Parse(node.Value)).ToList())
{
TreeNode leafNode = new TreeNode();
leafNode.Text = item.Name;
leafNode.Value = item.TypeId.ToString();
leafNode.ToolTip = string.Format("{0}", item.TypeId);
leafNode.Expanded = false;
node.ChildNodes.Add(leafNode);
//递归调用,添加子节点
this.GetPriceNode(leafNode, list);
}
}
只需要生成图上的效果吗