zoukankan
html css js c++ java
一个 TreeView 的派生类: TreeViewEx 实现 NodeShowToolTip、NodeDoubleClick 事件
//
playyuer@Microshaoft.com invent
//
一个 TreeView 的派生类: TreeViewEx 实现 NodeShowToolTip、NodeDoubleClick 事件
//
1.实现了 NodeShowToolTip 事件,结合键盘 Ctrl 键显示及设置 ToolTipText
//
2.实现了 NodeDoubleClick 事件,可在调用中只响应"叶子"节点
//
3.点击 TreeView 空白处不选中任何节点
//
Class1.cs
namespace
Microshaoft
{
using
System;
using
System.Windows.Forms;
public
class
TreeViewEx : TreeView
{
//
委托类型
public
delegate
void
TreeViewExEventHandler(
object
sender, TreeViewExEventArgs e);
//
事件
public
event
TreeViewExEventHandler NodeDoubleClick;
public
event
TreeViewExEventHandler NodeShowToolTip;
private
ToolTip toolTip;
public
TreeViewEx()
{
toolTip
=
new
ToolTip();
this
.toolTip.InitialDelay
=
300
;
this
.toolTip.ReshowDelay
=
0
;
}
private
void
OnNodeDoubleClick(TreeNode xx)
{
if
(
this
.NodeDoubleClick
!=
null
)
{
this
.NodeDoubleClick(
this
,
new
TreeViewExEventArgs(xx));
}
}
private
void
OnNodeShowToolTip(TreeNode xx, ToolTip yy)
{
if
(
this
.NodeShowToolTip
!=
null
)
{
if
((xx
!=
null
)
&&
(
this
.toolTip
!=
null
))
{
this
.NodeShowToolTip(
this
,
new
TreeViewExEventArgs(xx,
this
.toolTip));
}
}
}
protected
override
void
OnDoubleClick(EventArgs e)
{
if
(
this
.SelectedNode
!=
null
)
{
this
.OnNodeDoubleClick(
this
.SelectedNode);
}
base
.OnDoubleClick(e);
}
protected
override
void
OnClick(EventArgs e)
{
if
(
this
.GetNodeAt(TreeView.MousePosition.X, TreeView.MousePosition.Y)
==
null
)
{
this
.SelectedNode
=
null
;
}
base
.OnClick(e);
}
protected
override
void
OnMouseDown(MouseEventArgs e)
{
if
(
this
.GetNodeAt(e.X, e.Y)
==
null
)
{
this
.SelectedNode
=
null
;
}
base
.OnMouseDown(e);
}
//
用于保存上次节点,以判断是否更新 ToolTiptext
private
TreeNode LastTreeNode;
protected
override
void
OnMouseMove(MouseEventArgs e)
{
this
.Cursor
=
Cursors.Default;
TreeNode treeNode;
treeNode
=
this
.GetNodeAt(e.X, e.Y);
if
(treeNode
!=
null
)
{
if
((Control.ModifierKeys
&
Keys.Control)
!=
0
)
{
this
.Cursor
=
Cursors.Hand;
if
(LastTreeNode
==
null
|
treeNode
!=
LastTreeNode)
{
LastTreeNode
=
treeNode;
//
this.toolTip.Active = false;
this
.OnNodeShowToolTip(treeNode,
this
.toolTip);
this
.toolTip.Active
=
true
;
Console.WriteLine(treeNode.Text);
}
}
else
{
this
.Cursor
=
Cursors.Default;
this
.toolTip.Active
=
false
;
}
}
else
{
this
.Cursor
=
Cursors.Default;
this
.toolTip.Active
=
false
;
}
base
.OnMouseMove(e);
}
}
public
class
TreeViewExEventArgs : EventArgs
{
private
string
m_ToolTipText;
private
ToolTip m_NodeToolTip;
private
TreeNode m_SelectedNode;
private
TreeNode m_Node;
public
TreeViewExEventArgs(TreeNode SelectedNode)
{
this
.m_SelectedNode
=
SelectedNode;
}
public
TreeViewExEventArgs(TreeNode Node, ToolTip NodeToolTip)
{
this
.m_NodeToolTip
=
NodeToolTip;
this
.m_Node
=
Node;
}
public
TreeViewExEventArgs(TreeNode Node,
string
ToolTipText)
{
this
.m_ToolTipText
=
ToolTipText;
}
public
TreeNode SelectedNode
{
get
{
return
this
.m_SelectedNode;
}
}
public
string
ToolTipText
{
get
{
return
this
.ToolTipText;
}
set
{
m_ToolTipText
=
value;
}
}
public
TreeNode Node
{
get
{
return
this
.m_Node;
}
}
public
ToolTip NodeToolTip
{
get
{
return
this
.m_NodeToolTip;
}
}
}
public
class
TreeNodeEx : TreeNode
{
public
int
GetLevel()
{
int
i
=
0
;
TreeNode xx
=
this
.Parent;
while
((xx
=
xx.Parent)
!=
null
)
{
i
++
;
}
return
i;
}
}
}
//
using System;
//
测试====================
namespace
WindowsApplication1
{
using
System;
using
System.Drawing;
using
System.ComponentModel;
using
System.Windows.Forms;
using
Microshaoft;
/**/
///
<summary>
///
Form1 的摘要说明。
///
</summary>
public
class
Form1 : Form
{
private
Button button1;
/**/
///
<summary>
///
必需的设计器变量。
///
</summary>
private
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);
}
Windows 窗体设计器生成的代码
#region
Windows 窗体设计器生成的代码
/**/
///
<summary>
///
设计器支持所需的方法 - 不要使用代码编辑器修改
///
此方法的内容。
///
</summary>
private
void
InitializeComponent()
{
this
.button1
=
new
Button();
this
.SuspendLayout();
//
//
button1
//
this
.button1.Location
=
new
Point(
96
,
112
);
this
.button1.Name
=
"
button1
"
;
this
.button1.Size
=
new
Size(
88
,
32
);
this
.button1.TabIndex
=
0
;
this
.button1.Text
=
"
button1
"
;
this
.button1.Click
+=
new
EventHandler(
this
.button1_Click);
//
//
Form1
//
this
.AutoScaleBaseSize
=
new
Size(
6
,
14
);
this
.ClientSize
=
new
Size(
292
,
273
);
this
.Controls.Add(
this
.button1);
this
.Name
=
"
Form1
"
;
this
.Text
=
"
Form1
"
;
this
.Load
+=
new
EventHandler(
this
.Form1_Load);
this
.ResumeLayout(
false
);
}
#endregion
/**/
///
<summary>
///
应用程序的主入口点。
///
</summary>
[STAThread]
private
static
void
Main()
{
Application.Run(
new
Form1());
}
//
=================================================================
//
调用示例
private
TreeViewEx treeViewEx1;
private
void
Form1_Load(
object
sender, 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
TreeNode[]
{
new
TreeNode(
"
Root
"
,
new
TreeNode[]
{
new
TreeNode(
"
a
"
),
new
TreeNode(
"
b
"
)
}
),
new
TreeNode(
"
Root1
"
,
new
TreeNode[]
{
new
TreeNode(
"
a1
"
),
new
TreeNode(
"
b1
"
)
}
)
}
);
}
public
void
treeViewEx1_NodeShowToolTip(
object
sender, TreeViewExEventArgs e)
{
e.NodeToolTip.SetToolTip(
this
.treeViewEx1,
"
ToolTipText: [
"
+
e.Node.Text
+
"
]
"
);
//
e.NodeToolTip.SetToolTip (this.treeViewEx1,"ToolTipText: [" + e.Node.Text + "]" );
//
Console.WriteLine(e.Node.Text);
}
private
void
treeViewEx1_NodeDoubleClick(
object
sender, TreeViewExEventArgs e)
{
if
(e.SelectedNode.GetNodeCount(
true
)
==
0
)
MessageBox.Show(e.SelectedNode.Text);
}
private
void
button1_Click(
object
sender, EventArgs e)
{
}
}
}
查看全文
相关阅读:
更改EBSserver域名/IP
iOS Dev (60) 怎样实现 UITextView 中的 placeHolder
程序猿的量化交易之路(29)--Cointrader之Tick实体(16)
美团面试中被问到的问题汇总
汇报措辞:你懂得如何向领导汇报吗(审阅、审批、审阅、批示、查阅)?
九月份总结
Android 编程之第三方开发 MaoZhuaWeiBo微博开发演示样例-1
OLE操作Excel编译错误处理
在 VS2008 下操作 Excel 的方法总结
vs2008 ole excel
原文地址:https://www.cnblogs.com/Microshaoft/p/148479.html
最新文章
奔小康赚大钱 hdu 2255
ACM-最小生成树之畅通project——hdu1863
1034. Head of a Gang (30)
mysqladmin在SuSE linux系统中--sleep參数使用不准确问题
13、Cocos2dx 3.0游戏开发找小三之3.0中的Director :郝萌主,一统江湖
Java学习之道:jdk环境变量配置方法
HYSBZ 2243 染色 (树链剖分)
Java中String和byte[]间的 转换浅析
Java中String和byte[]间的转换浅析
java中String十六进制Stringyte[]之间相互转换函数
热门文章
Java中byte与16进制字符串的互相转换
Android蓝牙自动配对Demo,亲测好使!!!
android 蓝牙各种UUID
android蓝牙主动发起配对实例
Android实现主动连接蓝牙耳机
Android 蓝牙扫描代码
IOS蓝牙项目总结
设计模式(4)-对象创建型模式-Prototype模式
cronjob不跑得原因
UPX 加壳工具:The Ultimate Packer for eXecutables
Copyright © 2011-2022 走看看