比较简单的做法,增加一个子节点提示.然后开线程请求真实的子节点,完毕后替换提示节点。
代码如下
using System;using System.Collections.Generic;
using System.ComponentModel;using System.Data;
using System.Drawing;using System.Text;
using System.Windows.Forms;
namespace WindowsApplication11{
public partial class Form1 : Form
{
//用于控制线程的方法
public class ThreadStartInfo
{
public TreeNode node=null;
//模拟载入数据
public void LoadNode()
{
System.Threading.Thread.Sleep(5000);
string[] str = new string[] { "jinjazz", "csdn" ,"sina","google","yahoo","nba","fifa"};
this.AddNode(node, str);
}
delegate void dAddNode(TreeNode node, string[] str);
//添加节点
void AddNode(TreeNode node, string[] str)
{
if (this.node.TreeView.FindForm().InvokeRequired)
{
this.node.TreeView.FindForm().Invoke(new dAddNode(AddNode), new object[] { node, str });
}
else
{
node.Nodes.Clear();
foreach (string strNode in str)
{
node.Nodes.Add(strNode);
}
}
}
}
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 s = new ThreadStartInfo();
s.node = e.Node;
//已经载入过的就不要载入了
s.node.Tag = 1;
new System.Threading.Thread(new System.Threading.ThreadStart(s.LoadNode)).Start();
e.Node.Nodes.Add("正在载入..");
}
}
}