// playyuer@Microshaoft.com invent // a TreeView derived class: TreeViewEx achieve NodeShowToolTip, NodeDoubleClick event // 1 implements NodeShowToolTip event, combined with the keyboard Ctrl key to display and set the ToolTipText // 2 achieved NodeDoubleClick event can be. call the only response "leaf" node // 3 click TreeView does not select any blank node //Class1.csnamespace Microshaoft {using System; public class TreeViewEx: System.Windows.Forms.TreeView {public delegate void TreeViewExEventHandler (object sender, TreeViewexEventArgs E); Public Event TreeViewexeventHandler NodeoubleClick; Public Event TreeViewExeventHandler NodeshowTooltip;
private void OnNodeDoubleClick (System.Windows.Forms.TreeNode xx) {if (this.NodeDoubleClick = null!) {this.NodeDoubleClick (this, new TreeViewExEventArgs (xx));}} private void OnNodeShowToolTip (System.Windows.Forms.TreeNode xx, System.Windows.Forms.ToolTip yy) {if (this.NodeShowToolTip! = null) {if ((xx! = null) && (this.toolTip! = null)) {this.NodeShowToolTip (this, new TreeViewExEventArgs ( xx, this.toolTip));}}} protected override void OnDoubleClick (System.EventArgs e) {if (this.SelectedNode = null) {this.OnNodeDoubleClick (this.SelectedNode);!} base.OnDoubleClick (e);} private System.Windows.Forms.ToolTip toolTip; public TreeViewEx () {toolTip = new System.Windows.Forms.ToolTip (); this.toolTip.InitialDelay = 300; this.toolTip.ReshowDelay = 0;} protected override void OnClick ( System.Eventargs e) {if (this.Getnodet (System.Windows.Forms.treeView.MousePosition.x, System.Windows.Forms.treeView.MousePosition.y) == NULL) { this.SelectedNode = null;} base.OnClick (e);} protected override void OnMouseDown (System.Windows.Forms.MouseEventArgs e) {if (this.GetNodeAt (eX, eY) == null) this.SelectedNode = null; Base.onmousedown (e);
} Private System.Windows.Forms.TreeNode LastTreeNode; protected override void OnMouseMove (System.Windows.Forms.MouseEventArgs e) {this.Cursor = System.Windows.Forms.Cursors.Default; System.Windows.Forms.TreeNode treeNode; treeNode = this.Getnodeat (EX, EY); if (Treenode! = null) {IF ((System.Windows.Forms.Control.ModifierKeys.Modows.Forms.keys.Control)! = 0) {this.cursor = System.Windows.Forms.Cursors.Hand; if (treeNode = LastTreeNode | LastTreeNode == null!) {LastTreeNode = treeNode; this.toolTip.Active = false; this.OnNodeShowToolTip (treeNode, this.toolTip); this.toolTip. Active = true;}} else {this.Cursor = System.Windows.Forms.Cursors.Default; this.toolTip.Active = false;}} else {this.Cursor = System.Windows.Forms.Cursors.Default; this. Tooltip.Active = false;} base.onmousemove (e);}}
public class TreeViewExEventArgs: System.EventArgs {public TreeViewExEventArgs (System.Windows.Forms.TreeNode SelectedNode) {this.m_SelectedNode = SelectedNode;} public TreeViewExEventArgs (System.Windows.Forms.TreeNode Node, System.Windows.Forms.ToolTip NodeToolTip) { this.m_NodeToolTip = NodeToolTip; this.m_Node = Node;} public TreeViewExEventArgs (System.Windows.Forms.TreeNode Node, string ToolTipText) {this.m_ToolTipText = ToolTipText;} private string m_ToolTipText; private System.Windows.Forms.ToolTip m_NodeToolTip; private System.Windows.Forms.TreeNode m_SelectedNode; private System.Windows.Forms.TreeNode m_Node; public System.Windows.Forms.TreeNode SelectedNode {get {return this.m_SelectedNode;}} public string ToolTipText {get {return this.ToolTipText; } Set {m_tooltiptext = value;}} public system.windows.Forms.treenode node {get {return this.m_node;}} public system.windows.forms.tooltip nodetooltip {Get {Return this.m_NodeToolTip;}}} public class TreeNodeEx: System.Windows.Forms.TreeNode {public int GetLevel () {int i = 0; System.Windows.Forms.TreeNode xx = this.Parent; while ((xx = XX.Parent)! = NULL) i ; Return I;}}} // use system;
// Test ===================== Namespace windowSapplication1 {
Using system.collections; using system.componentmodel; using system.windows.form; using system.data; using microshaoft; ///
// // Todo: Add any constructor code after INITIALIZEComponent call //}
///
#REGION Windows Form Designer The code ///
} #Endregion
///
Public void TreeViewEx1_nodeshowTooltip (Object Sender, TreeViewexEventArgs E) {E.NodeTooltip.SetTooltip (this.treeViewEx1, "Tooltiptext: [" E.Node.Text "]");}
private void treeViewEx1_NodeDoubleClick (object sender, TreeViewExEventArgs e) {if (e.SelectedNode.GetNodeCount (true) == 0) System.Windows.Forms.MessageBox.Show (e.SelectedNode.Text);} private void button1_Click (object sender, System.eventargs e) {}}}