比較簡單的做法,增加一個子節點提示.然後開線程請求真實的子節點,完畢後替換提示節點。
代碼如下
view plaincopy to clipboardprint?
01.using System; 02.using System.Collections.Generic; 03.using System.ComponentModel; 04.using System.Data; 05.using System.Drawing; 06.using System.Text; 07.using System.Windows.Forms; 08. 09.namespace WindowsApplication11 10.{ 11. public partial class Form1 : Form 12. { 13. 14. //用於控制線程的方法 15. public class ThreadStartInfo 16. { 17. public TreeNode node=null; 18. 19. //模擬載入數據 20. public void LoadNode() 21. { 22. System.Threading.Thread.Sleep(5000); 23. string[] str = new string[] { "jinjazz", "csdn" ,"sina","google","yahoo","nba","fifa"}; 24. this.AddNode(node, str); 25. } 26. delegate void dAddNode(TreeNode node, string[] str); 27. 28. //添加節點 29. void AddNode(TreeNode node, string[] str) 30. { 31. if (this.node.TreeView.FindForm().InvokeRequired) 32. { 33. this.node.TreeView.FindForm().Invoke(new dAddNode(AddNode), new object[] { node, str }); 34. } 35. else 36. { 37. node.Nodes.Clear(); 38. foreach (string strNode in str) 39. { 40. node.Nodes.Add(strNode); 41. } 42. } 43. 44. } 45. } 46. 47. public Form1() 48. { 49. InitializeComponent(); 50. } 51. 52. private void Form1_Load(object sender, EventArgs e) 53. { 54. //添加根節點 55. this.treeView1.Nodes.Add(new TreeNode("root")); 56. this.treeView1.AfterSelect += new TreeViewEventHandler(treeView1_AfterSelect); 57. } 58. 59. void treeView1_AfterSelect(object sender, TreeViewEventArgs e) 60. { 61. if (e.Node.Tag==null||e.Node.Tag.ToString() == "") 62. { 63. ThreadStartInfo s = new ThreadStartInfo(); 64. s.node = e.Node; 65. //已經載入過的就不要載入了。 66. s.node.Tag = 1; 67. new System.Threading.Thread(new System.Threading.ThreadStart(s.LoadNode)).Start(); 68. e.Node.Nodes.Add("正在載入.."); 69. } 70. } 71. } 72.}