有时候我们对树控件的双击默认操作不太满意,比如我们在双击一有子节点的节点的时候不希望打开或关闭它的子节点而想执行一个特定的操作,这时怎么来写呢,我这里有一个办法可以做到这一点:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data; namespace TreeViewS
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
  private System.Windows.Forms.TreeView treeView1;
   /// <summary>
   /// 必需的设计器变量。
   /// </summary>
  private System.ComponentModel.Container components = null;   public Form1()
   {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
   }    /// <summary>
   /// 清理所有正在使用的资源。
   /// </summary>
  protected override void Dispose( bool disposing )
   {
    if( disposing )
    {
    if (components != null)
     {
      components.Dispose();
     }
    }
   base.Dispose( disposing );
   }   #region Windows 窗体设计器生成的代码
   /// <summary>
   /// 设计器支持所需的方法 - 不要使用代码编辑器修改
   /// 此方法的内容。
   /// </summary>
  private void InitializeComponent()
   {
   this.treeView1 = new System.Windows.Forms.TreeView();
   this.SuspendLayout();
   //
    // treeView1
    //
   this.treeView1.ImageIndex = -1;
   this.treeView1.Location = new System.Drawing.Point(8, 8);
   this.treeView1.Name = "treeView1";
   this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
                      new System.Windows.Forms.TreeNode("节点0"),
                      new System.Windows.Forms.TreeNode("节点1", new System.Windows.Forms.TreeNode[] {
                                           new System.Windows.Forms.TreeNode("节点0", new System.Windows.Forms.TreeNode[] {
                                                             new System.Windows.Forms.TreeNode("节点1", new System.Windows.Forms.TreeNode[] {
                                                                                  new System.Windows.Forms.TreeNode("节点2", new System.Windows.Forms.TreeNode[] {
                                                                                                       new System.Windows.Forms.TreeNode("节点8")})}),
                                                             new System.Windows.Forms.TreeNode("节点3", new System.Windows.Forms.TreeNode[] {
                                                                                  new System.Windows.Forms.TreeNode("节点4", new System.Windows.Forms.TreeNode[] {
                                                                                                       new System.Windows.Forms.TreeNode("节点9")})}),
                                                             new System.Windows.Forms.TreeNode("节点5", new System.Windows.Forms.TreeNode[] {
                                                                                  new System.Windows.Forms.TreeNode("节点6", new System.Windows.Forms.TreeNode[] {
                                                                                                       new System.Windows.Forms.TreeNode("节点7")})})})}), new System.Windows.Forms.TreeNode("节点2", new System.Windows.Forms.TreeNode[] {
                                           new System.Windows.Forms.TreeNode("节点0")}),
                      new System.Windows.Forms.TreeNode("节点3")});
   this.treeView1.SelectedImageIndex = -1;
   this.treeView1.Size = new System.Drawing.Size(216, 232);
   this.treeView1.TabIndex = 0;
   this.treeView1.Click += new System.EventHandler(this.treeView1_Click);
   this.treeView1.BeforeCollapse += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeCollapse);
   this.treeView1.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeExpand);
    //
    // Form1
    //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(256, 266);
   this.Controls.Add(this.treeView1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);    }
   #endregion   /// <summary>
   /// 应用程序的主入口点。
   /// </summary>
   [STAThread]
  static void Main()
   {
    Application.Run(new Form1());
   }  private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
   {
   string path = null;    TreeNode node = this.treeView1.SelectedNode;
   if (node != null)
    {
     path = node.Text;
    while (node.Parent != null )
     {
      node = node.Parent;
      path = node.Text + "\\"+ path;
     }
    }
   }  private bool tmpBool;//用来决定是否打开或关闭树的节点  private void treeView1_Click(object sender, System.EventArgs e)
   {
   object tmpObj = this.treeView1.GetNodeAt(this.treeView1.PointToClient(Cursor.Position));
   if (tmpObj!=null)
    {
    if ((tmpObj as TreeNode).Bounds.Contains(this.treeView1.PointToClient(Cursor.Position)))
     {
      tmpBool = true;
     }
    }
   }  private void treeView1_BeforeExpand(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
   {
   if (tmpBool)
    {
     e.Cancel = tmpBool;
    }
    tmpBool = false;
   }
  private void treeView1_BeforeCollapse(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
   {
   if (tmpBool)
    {
     e.Cancel = tmpBool;
    }
    tmpBool = false;
   }
}
}