| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2769 人关注过本帖
标题:[求助]怎么获取TreeView的结点的路径
只看楼主 加入收藏
leely_lly
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2007-4-11
收藏
 问题点数:0 回复次数:5 
[求助]怎么获取TreeView的结点的路径

C#中,我想在结点上点击鼠标右键获得该结点的路径,应该怎么写代码?谢谢!

搜索更多相关主题的帖子: 结点 TreeView 路径 获取 
2007-04-13 11:08
liuminghui
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:2882
专家分:0
注 册:2007-1-26
收藏
得分:0 

下面是我以前写过的东西,希望能对你有所帮助


//处理TreeView中的事件
private void TreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
DataSet ds = new DataSet();


SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=student;Integrated Security=True");
switch (TreeView.SelectedNode.Name)
{
case "girl":
SqlDataAdapter daAuthors = new SqlDataAdapter("Select ID ,sno,sname,sex From student where sex=0", conn);
conn.Open();
daAuthors.Fill(ds, "student");

// conn.Close() '在填充完ds后关闭连接,接着对ds进行操作
dataGrid1.DataSource = ds.Tables[0];//vb.net上使用这样的语句:ds.Tables("student");
conn.Close();
break;

case "boy":

SqlDataAdapter daAuthor = new SqlDataAdapter("Select ID ,sno,sname,sex From student where sex=1", conn);
conn.Open();
daAuthor.Fill(ds, "student");

// conn.Close() '在填充完ds后关闭连接,接着对ds进行操作
dataGrid1.DataSource = ds.Tables[0];//vb.net上使用这样的语句:ds.Tables("student");

conn.Close();
break;

}

}


海鸽 is My Lover!!
2007-04-13 11:11
leely_lly
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2007-4-11
收藏
得分:0 

呵呵,谢谢,可是我还是不太懂,我没有用到数据库.就只是做了一个本机文件的treeView,现在就是想在结点上右击,弹出上下文菜单,获得被点击的结点的路径,以便对其进行一些别的操作.

2007-04-13 11:41
loki
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2006-4-25
收藏
得分:0 

public partial class Form1 : Form
{
private string nodeName;
public Form1()
{
InitializeComponent();
}

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
contextMenuStrip1.Show(this, e.X, e.Y);
nodeName = e.Node.Name;
}
}

private void Form1_Load(object sender, EventArgs e)
{
DriveInfo[] drives = DriveInfo.GetDrives();
treeView1.PathSeparator = @"\";
foreach (DriveInfo drive in drives)
{
TreeNode parent=new TreeNode();
parent.Text = drive.Name;
parent.Name = drive.Name;
treeView1.Nodes.Add(parent);
}
}

private void menupath_Click(object sender, EventArgs e)
{
MessageBox.Show("你选择的节点路径为:" + nodeName);
}
}

就做了驱动器那一级目录
下面的子目录你自己遍历一下吧
基本要求实现了

2007-04-13 23:11
loki
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2006-4-25
收藏
得分:0 
menupath为contextMenuStrip1的菜单项
2007-04-13 23:14
leely_lly
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2007-4-11
收藏
得分:0 

恩,好方法,可是我对文件树的加载是这样进行的,以下是我的代码,怎么才能实现我的功能呢.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace Compress
{
public partial class FormFile : Form
{
private string nodeName;
public FormFile()
{
InitializeComponent();
}

private DirectoryInfo folder;


private void FormFile_Load(object sender, EventArgs e)
{
LoadTree();
}


private void LoadTree()
{
DirectoryInfo directory;

//clear the tree
//treeExplore.Nodes.Clear();


//Loop through the drive letter and find the available drive

foreach (string drive in Environment.GetLogicalDrives())
{
try
{
// get the directory information for this path
directory = new DirectoryInfo(drive);

//if the retrived directory is valid ,add it to the tree view
if (directory.Exists == true)
{
TreeNode newNode = new TreeNode(directory.FullName);
treeExplore.Nodes.Add(newNode);

//add the new node to the root level
GetSubDirectories(newNode);

}
}

catch
{
return;
}
}
}


private void GetSubDirectories(TreeNode parent)
{
DirectoryInfo directory;
try
{
if (parent.Nodes.Count == 0)
{
directory = new DirectoryInfo(parent.FullPath);
foreach (DirectoryInfo dir in directory.GetDirectories())
{
TreeNode newNode = new TreeNode(dir.Name);
parent.Nodes.Add(newNode);
}

}
foreach (TreeNode Node in parent.Nodes)
{
if (Node.Nodes.Count == 0)
{
directory = new DirectoryInfo(Node.FullPath);
foreach (DirectoryInfo dir in directory.GetDirectories())
{
TreeNode newnode = new TreeNode(dir.Name);
Node.Nodes.Add(newnode);
}
}
}
}
catch
{
return;
}
}


private void treeExplore_AfterSelect(object sender, TreeViewEventArgs e)
{
DirectoryInfo dirInfo = new DirectoryInfo(e.Node.FullPath);
listExplorer.Items.Clear();
if (dirInfo.Exists)
{
FileInfo[] fileInfo = dirInfo.GetFiles();
foreach (FileInfo info in fileInfo)
{
ListViewItem item = new ListViewItem();
item = listExplorer.Items.Add(info.Name);
item.SubItems.Add(info.LastAccessTime.ToString());
item.SubItems.Add(info.Length.ToString());
}
}



private void onMouseDown(object sender, MouseEventArgs e)
{
TreeNode newSelectedNode = treeExplore.GetNodeAt(e.X, e.Y);
if (newSelectedNode != null)
{
treeExplore.SelectedNode = newSelectedNode;
treeExplore.SelectedNode.ContextMenuStrip = contextMenuStrip; //哈哈,终于找到你了!
}

if (e.Button == MouseButtons.Right)
{
treeExplore.SelectedNode = treeExplore.GetNodeAt(e.X, e.Y);
contextMenuStrip.Visible = true;
contextMenuStrip
MessageBox.Show("abc");
}

}


   private void compressMenuItem_Click(object sender,
                TreeNodeMouseClickEventArgs e) //compressMenuItem是contextmenu的列表
{                              
string[] FileProperties = new string[2];          
FileProperties[0] = "???";   //待解压的文件 这是最关键的,怎么才是正确获的文件的路径
FileProperties[1] = "???";//解压后放置的目标目录
ZipClass Zc = new ZipClass();            //调用一个压缩函数,实现对文件的压缩,
Zc.ZipFileMain(FileProperties);  //函数的参数是文件的路径,现在的问题是怎么取的文件的路径
}

}

}

[此贴子已经被作者于2007-4-16 11:22:26编辑过]

2007-04-16 11:10
快速回复:[求助]怎么获取TreeView的结点的路径
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029406 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved