TreeView's derived class: TreeViewEx Realize NodeshowTooltip, NodeDoubleClick event

zhaozj2021-02-08  284

// 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; ///

/// form1 summary description. /// public class form1: system.windows.forms.form {private system.windows.forms.button button1; /// // / / The designer variable. /// private system.componentmodel.container Components = NULL; public form1 () {// // Windows Form Designer Support for // InitializationComponent ();

// // Todo: Add any constructor code after INITIALIZEComponent call //}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose (4);

#REGION Windows Form Designer The code ///

/// designer supports the required method - do not use the code editor to modify the // / this method. /// private () {this.button1 = new system.windows.forms.button (); this.suspendlayout (); // // button1 // this.button1.location = new system. Drawing.point (96, 112); this.button1.name = "button1"; this.button1.size = new system.drawing.size (88, 32); this.button1.tabindex = 0; this.button1.text = "Button1"; this.button1.click = new system.eventhandler (this.button1_click); // // form1 // this.autoscalebasesize = new system.drawing.size (6, 14); this.clientsize = new System.drawing.size (292, 273); this.controls.add (this.button1); this.name = "form1"; this.text = "form1"; this.load = new system.EventHandler (this. Form1_load); this.ResumeLayout (false);

} #Endregion

///

/// The primary entry point of the application. /// [stathread] static void main () {Application.run (new form1 ());} // ===================== ============================================================= 调 调 示 示treeViewEx1; private void Form1_Load (object sender, System.EventArgs e) {treeViewEx1 = new TreeViewEx (); this.Controls.Add (treeViewEx1); treeViewEx1.HideSelection = false; treeViewEx1.NodeDoubleClick = new TreeViewEx.TreeViewExEventHandler (treeViewEx1_NodeDoubleClick); treeViewEx1.NodeShowToolTip = new TreeViewEx.TreeViewExEventHandler (this.treeViewEx1_NodeShowToolTip); treeViewEx1.Nodes.AddRange (new System.Windows.Forms.TreeNode [] {new System.Windows.Forms.TreeNode ( "Root", new System.Windows. Forms.treenode [] {new system.windows.forms.treenode ("a"), new system.windows.forms.treenode ("b")}), new system.windows.Forms.treenode ("root1", New System.windows.Forms.treenode [] {New system.windows.Forms.treenode ("a1"), new system.windows.forms.treenode ("b1")}};

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) {}}}

转载请注明原文地址:https://www.9cbs.com/read-1104.html

New Post(0)