问题一:怎样为“Treeview”的Node节点绑定onmousedown事件???
是不是为“TreeView”绑定“onmousedown”,有关“Node”的事件只有“NodeMouseClick”、“NodeMouseDoubleClick”、“NodeMouseHover”???问题二:怎样获取点击的是“鼠标右键”还是“左键”???
是不是为“TreeView”绑定“onmousedown”,有关“Node”的事件只有“NodeMouseClick”、“NodeMouseDoubleClick”、“NodeMouseHover”???问题二:怎样获取点击的是“鼠标右键”还是“左键”???
解决方案 »
- word无法读取文档 文档可能损坏 打开并修复文件
- 请教list select 满足2个字段的值都要存在同一个表中
- 我想把这段程序变成能对目录下面所有文件都能操作的,咋弄!我改后不行,求高手指点
- C#能实现与以太网的通信吗?怎么实现?
- asp.net 如何从网页上传图片到数据库,具体怎么实现?
- “WindowsApplication1.Class1”的类型初始值设定项引发异常。(帮帮忙,100分求这个问题的解决方案)
- 求一正则表达式,谢了
- 在程序里打一个外接程序,如何让他一直位于顶端
- 如何获得某进程的用户名和 CPU 使用率????
- 简单问题,在线等,忙了半天了,晕~~~(c#)
- 没分了,datagrid问题
- 怎样为“Treeview”的Node节点绑定onmousedown事件
1.TreeNode tn = new TreeNode();
2.private void treeView1_MouseDown(object sender, MouseEventArgs e)
3.{
4. Point p = new Point();
5. p.X = e.X;
6. p.Y = e.Y;
7. tn = this.treeView1.GetNodeAt(p);
8.}
TreeNode tn = new TreeNode();
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
Point p = new Point();
p.X = e.X;
p.Y = e.Y;
tn = this.treeView1.GetNodeAt(p);
}这段代码里面的GetNodeAt(P)方法是检索位于指定点(以坐标表示)的树节点;如果有节点则会得到该节点,否则返回Null;
{
if (e.Button == MouseButtons.Left)
{
MessageBox.Show("你按下鼠标左键", "提示");
}
else
{
MessageBox.Show("你按下鼠标右键","提示");
}
}
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
//设置属性
treeView1.AllowDrop = true;
treeView1.Dock = DockStyle.Fill;
this.treeView1.Nodes.AddRange(new TreeNode[] {
new TreeNode("IT技术", new TreeNode[] {
new TreeNode(".Net", new TreeNode[] {
new TreeNode("Silverlight"), new TreeNode("ASP.NET"),new TreeNode("Winform")}),
new TreeNode("Java技术", new TreeNode[] {new TreeNode("Java EE"),
new TreeNode("Java SE"), new TreeNode("Java ME")}),
new TreeNode("其它技术", new TreeNode[] {
new TreeNode("Web开发", new TreeNode[] {new TreeNode("ASP"),
new TreeNode("PHP", new TreeNode[] {new TreeNode("AJAX"),new TreeNode("JS")})}),
new TreeNode("数据库"),new TreeNode("移动设备")})})}); treeView1.ExpandAll(); } private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
{
// Move the dragged node when the left mouse button is used.
if (e.Button == MouseButtons.Left)
{
DoDragDrop(e.Item, DragDropEffects.Move);
} // Copy the dragged node when the right mouse button is used.
else if (e.Button == MouseButtons.Right)
{
DoDragDrop(e.Item, DragDropEffects.Copy);
}
} private void treeView1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = e.AllowedEffect;
} private void treeView1_DragOver(object sender, DragEventArgs e)
{
// Retrieve the client coordinates of the mouse position.
Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y)); // Select the node at the mouse position.
treeView1.SelectedNode = treeView1.GetNodeAt(targetPoint);
} private void treeView1_DragDrop(object sender, DragEventArgs e)
{
// Retrieve the client coordinates of the drop location.
Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y)); // Retrieve the node at the drop location.
TreeNode targetNode = treeView1.GetNodeAt(targetPoint); // Retrieve the node that was dragged.
TreeNode draggedNode = (TreeNode)e.Data.GetData(typeof(TreeNode)); // Confirm that the node at the drop location is not
// the dragged node or a descendant of the dragged node.
if (!draggedNode.Equals(targetNode) && !ContainsNode(draggedNode, targetNode))
{
// If it is a move operation, remove the node from its current
// location and add it to the node at the drop location.
if (e.Effect == DragDropEffects.Move)
{
draggedNode.Remove();
targetNode.Nodes.Add(draggedNode);
} // If it is a copy operation, clone the dragged node
// and add it to the node at the drop location.
else if (e.Effect == DragDropEffects.Copy)
{
targetNode.Nodes.Add((TreeNode)draggedNode.Clone());
} // Expand the node at the location
// to show the dropped node.
targetNode.Expand();
}
}
// Determine whether one node is a parent
// or ancestor of a second node.
private bool ContainsNode(TreeNode node1, TreeNode node2)
{
// Check the parent node of the second node.
if (node2.Parent == null) return false;
if (node2.Parent.Equals(node1)) return true; // If the parent node is not null or equal to the first node,
// call the ContainsNode method recursively using the parent of
// the second node.
return ContainsNode(node1, node2.Parent);
} }