代码
public partial class FrmTreeView : Form
{
public FrmTreeView()
{
InitializeComponent();
}
private void FrmTreeView_Load(object sender, EventArgs e)
{
this.treeView1.Nodes.Add(new TreeNode("root"));
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if ( (e.Node.Tag == null || e.Node.Tag.ToString() == "") && e.Node.Text != "正在载入...")
{
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("正在载入...");
}
}
}
{
public FrmTreeView()
{
InitializeComponent();
}
private void FrmTreeView_Load(object sender, EventArgs e)
{
this.treeView1.Nodes.Add(new TreeNode("root"));
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if ( (e.Node.Tag == null || e.Node.Tag.ToString() == "") && e.Node.Text != "正在载入...")
{
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("正在载入...");
}
}
}
代码
public class ThreadStartInfo
{
public TreeNode node = null;
public void LoadNode()
{
System.Threading.Thread.Sleep(5000);
string[] str = { "jin", "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 TreeNode node = null;
public void LoadNode()
{
System.Threading.Thread.Sleep(5000);
string[] str = { "jin", "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);
}
}
}
}