C#中,我想在结点上点击鼠标右键获得该结点的路径,应该怎么写代码?谢谢!
C#中,我想在结点上点击鼠标右键获得该结点的路径,应该怎么写代码?谢谢!
下面是我以前写过的东西,希望能对你有所帮助
//处理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;
}
}
呵呵,谢谢,可是我还是不太懂,我没有用到数据库.就只是做了一个本机文件的treeView,现在就是想在结点上右击,弹出上下文菜单,获得被点击的结点的路径,以便对其进行一些别的操作.
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);
}
}
就做了驱动器那一级目录
下面的子目录你自己遍历一下吧
基本要求实现了
恩,好方法,可是我对文件树的加载是这样进行的,以下是我的代码,怎么才能实现我的功能呢.
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编辑过]