Node("节点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;
}
}
}
上一页 [1] [2]