龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C#开发 >

详解C#的WinForm增加treeView1控件

时间:2011-04-12 23:18来源:未知 作者:admin 点击:
分享到:
Winform中关于treeView控件的做法,大家都比较了解。本文将谈到的是treeView1控件的增加过程,希望对广大开发人员有所帮助。 使用TreeNode的tag属性放置编码,TreeNode的Text属性放置名称,最

Winform中关于treeView控件的做法,大家都比较了解。本文将谈到的是treeView1控件的增加过程,希望对广大开发人员有所帮助。

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

  1. using System;   
  2. using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;   
  3. namespace testtree   
  4. {   
  5. ///    
  6. /// Form1 的摘要说明。   
  7. ///    
  8. public class Form1 : System.Windows.Forms.Form   
  9. {   
  10. private System.Windows.Forms.TreeView treeView1;   
  11. ///    
  12. /// 必需的设计器变量。   
  13. ///    
  14. private System.ComponentModel.Container components = null;   
  15. public Form1()   
  16. {   
  17. //   
  18. // Windows窗体设计器支持所必需的   
  19. //   
  20. InitializeComponent();   
  21. //   
  22. // TODO:在InitializeComponent调用后添加任何构造函数代码   
  23. //   
  24. TreeNode tn = new TreeNode();   
  25. tn.Tag = "1001"; tn.Text = "hello";   
  26. TreeNode tn2 = new TreeNode();   
  27. tn2.Tag = "1002"; tn2.Text = "ok";   
  28. tn.Nodes.Add(tn2);   
  29. treeView1.Nodes.Add(tn);   
  30. }   
  31. ///    
  32. /// 清理所有正在使用的资源。   
  33. ///    
  34. protected override void Dispose( bool disposing )   
  35. {   
  36. if( disposing )   
  37. {   
  38. if (components != null)   
  39. {   
  40. components.Dispose();   
  41. }   
  42. }   
  43. base.Dispose( disposing );   
  44. }   
  45. #region Windows窗体设计器生成的代码   
  46. ///    
  47. ///设计器支持所需的方法-不要使用代码编辑器修改   
  48. /// 此方法的内容。   
  49. ///    
  50. private void InitializeComponent()   
  51. {   
  52. this.treeView1 = new System.Windows.Forms.TreeView(); this.SuspendLayout();   
  53. //   
  54. // treeView1控件   
  55. //   
  56. 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);   
  57. //   
  58. // Form1   
  59. //   
  60. 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);   
  61. }   
  62. #endregion   
  63. ///    
  64. /// 应用程序的主入口点。   
  65. ///    
  66. [STAThread]   
  67. static void Main()   
  68. {   
  69. Application.Run(new Form1());   
  70. }   
  71. private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)   
  72. {   
  73. string s = treeView1.SelectedNode.Tag.ToString(); string s2 = treeView1.SelectedNode.Text.ToString();   
  74. }   
  75. }   

关于C#的WinForm增加treeView1控件就总结到这里。


收藏文章
表情删除后不可恢复,是否删除
取消
确定
图片正在上传,请稍后...
评论内容为空!
还没有评论,快来抢沙发吧!

热评话题

按钮 内容不能为空!
立刻说两句吧! 查看0条评论
精彩图集

赞助商链接