温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#的WinForm怎么增加treeView1控件

发布时间:2021-12-01 09:57:37 来源:亿速云 阅读:145 作者:iii 栏目:编程语言

这篇文章主要讲解了“C#的WinForm怎么增加treeView1控件”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C#的WinForm怎么增加treeView1控件”吧!

使用TreeNode的tag属性放置编码,TreeNode的Text属性放置名称,最简单的示例如下——先添加两个节点,然后处理AfterSelect事件:

using System;   using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;   namespace testtree   {   ///    /// Form1 的摘要说明。   ///    public class Form1 : System.Windows.Forms.Form   {   private System.Windows.Forms.TreeView treeView1;   ///    /// 必需的设计器变量。   ///    private System.ComponentModel.Container components = null;   public Form1()   {   //   // Windows窗体设计器支持所必需的   //   InitializeComponent();   //   // TODO:在InitializeComponent调用后添加任何构造函数代码   //   TreeNode tn = new TreeNode();   tn.Tag = "1001"; tn.Text = "hello";   TreeNode tn2 = new TreeNode();   tn2.Tag = "1002"; tn2.Text = "ok";   tn.Nodes.Add(tn2);   treeView1.Nodes.Add(tn);   }   ///    /// 清理所有正在使用的资源。   ///    protected override void Dispose( bool disposing )   {   if( disposing )   {   if (components != null)   {   components.Dispose();   }   }   base.Dispose( disposing );   }   #region Windows窗体设计器生成的代码   ///    ///设计器支持所需的方法-不要使用代码编辑器修改   /// 此方法的内容。   ///    private void InitializeComponent()   {   this.treeView1 = new System.Windows.Forms.TreeView(); this.SuspendLayout();   //   // treeView1控件   //   this.treeView1.ImageIndex = -1; this.treeView1.Location = new System.Drawing.Point(16, 8); this.treeView1.Name = "treeView1"; this.treeView1.SelectedImageIndex = -1; this.treeView1.TabIndex = 0; this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);   //   // Form1   //   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.Add(this.treeView1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false);   }   #endregion   ///    /// 应用程序的主入口点。   ///    [STAThread]   static void Main()   {   Application.Run(new Form1());   }   private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)   {   string s = treeView1.SelectedNode.Tag.ToString(); string s2 = treeView1.SelectedNode.Text.ToString();   }   }   }

感谢各位的阅读,以上就是“C#的WinForm怎么增加treeView1控件”的内容了,经过本文的学习后,相信大家对C#的WinForm怎么增加treeView1控件这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI