比較簡單的做法,增加一個子節點提示.然後開線程請求真實的子節點,完畢後替換提示節點。
代碼如下
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("正在載入.."); } } }}