C#中Winform的Treeview節點”正在展開…”的效果

來源:互聯網
上載者:User

比較簡單的做法,增加一個子節點提示.然後開線程請求真實的子節點,完畢後替換提示節點。 

代碼如下

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("正在載入..");            }        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.