TREEVIEW拖拽对应修改目录
TREEVIEW拖拽对应修改目录

| using System.IO; |
|
private static string RootPath = @"D:\Administrator\Documents\TestData";
//返回 D:\Administrator\Documents
private string myPath = GetDirectoryParentPath(RootPath);
private void Form1_Load(object sender, EventArgs e)
{
treeView1.AllowDrop = true;
treeView1.HideSelection = false;
DirectoryToTreeNode(RootPath,treeView1);
} |
|
private void DirectoryToTreeNode(string RootDir, TreeView treeView)
{
treeView.Nodes.Clear();
DirectoryInfo di = new DirectoryInfo(RootDir);
TreeNode rootNode = new TreeNode(di.Name);
GetDirs(di.GetDirectories(), rootNode);
treeView.Nodes.Add(rootNode);
rootNode.Expand();
} |
|
private void GetDirs(DirectoryInfo[] subDirs, TreeNode treeNode)
{
foreach (DirectoryInfo d in subDirs)
{
TreeNode node = new TreeNode(d.Name, 0, 0);
DirectoryInfo[] subSubDirs = d.GetDirectories();
GetDirs(subSubDirs, node);
treeNode.Nodes.Add(node);
}
} |
|
/*问题
C:\Program Files\\\
C:\Program Files///
*/
private bool DirectoryExists(string path)
{
DirectoryInfo d = new DirectoryInfo(path);
return d.Exists;
} |
|
//目录 D:\Administrator\Documents\TestData
//返回 D:\Administrator\Documents
private static string GetDirectoryParentPath(string path)
{
DirectoryInfo d = new DirectoryInfo(path);
return d.Parent.FullName;
} |
|
//目录 D:\Administrator\Documents\TestData
//返回 TestData
private string ExtractDirectoryName(string path)
{
DirectoryInfo d = new DirectoryInfo(path);
return d.Name;
} |
|
//C:\Windows\a
//C:\Windows\b
//实际运行过程 Directory.Move( "C:\Windows\a" , "C:\Windows\b\a");
private bool MoveDirectory(string sourceDirName, string destDirName)
{
destDirName = String.Format("{0}\\{1}", destDirName, ExtractDirectoryName(sourceDirName));
//源目录存在 但目标目录不存在
if (DirectoryExists(sourceDirName) && !DirectoryExists(destDirName))
{
Directory.Move(sourceDirName, destDirName);
//源目录不存在 但目标目录存在
if (!DirectoryExists(sourceDirName) && DirectoryExists(destDirName))
return true;
}
else
return false;
return false;
} |
|
private void treeView1_DragOver(object sender, DragEventArgs e)
{
TreeNode node2 = treeView1.GetNodeAt(treeView1.PointToClient(new Point(e.X, e.Y)));
//节点2不存在
//节点1 = 节点2
//节点1 往它的父一级节点拖拽
if ((node2 == null) ||(node1 == node2)|| (node1.Parent == node2))
{
treeView1.SelectedNode = node1;
SetTreeNodeColorDefault();
e.Effect = DragDropEffects.None;
return;
}
else
{
e.Effect = DragDropEffects.Move;
treeView1.SelectedNode = node2;
SetTreeNodeColorBlue();
//当一个父节点往它的子节点中拖拽时
while (node2.Parent != null)
{
if (node2.Parent == node1)
{
e.Effect = DragDropEffects.None;
return;
}
node2 = node2.Parent;
}
}
} |
|
private void treeView1_DragDrop(object sender, DragEventArgs e)
{
//拖拽的节点指向目标的 这个目标节点
TreeNode node2 = treeView1.GetNodeAt( treeView1.PointToClient(new Point(e.X, e.Y)) );
if (node1 != node2)
{
if (node1.Parent != node2)
{
string dir1 = myPath + "\\" + node1.FullPath;
string dir2 = myPath + "\\" + node2.FullPath;
textBox1.Text = dir1;
textBox2.Text = dir2;
if (MoveDirectory(textBox1.Text, textBox2.Text))
{
//MessageBox.Show("Finished");
// Remove drag node from parent
if (node1.Parent == null)
treeView1.Nodes.Remove(node1);
else
node1.Parent.Nodes.Remove(node1);
node2.Nodes.Add(node1);
treeView1.SelectedNode = node1;
node2.Expand();
}
}
}
} |
|
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
SetTreeNodeColorDefault();
} |
|
private void SetTreeNodeColorBlue()
{
if (node1 != null)
{
node1.BackColor = Color.FromArgb(51, 153, 255);//蓝色
node1.ForeColor = Color.White;
}
}
|
|
private void SetTreeNodeColorDefault()
{
if (node1 != null)
{
node1.BackColor = SystemColors.Window;
node1.ForeColor = Color.Black;
}
} |
附件列表
TREEVIEW拖拽对应修改目录的更多相关文章
- C# TreeView 拖拽节点到另一个容器Panel中简单实现
C# TreeView 拖拽节点到另一个容器Panel中简单实现 用了这么久C#拖拽功能一直没有用到也就没用过,今天因为项目需要,领导特地给我简单讲解了下拖拽功能,真是的大师讲解一点通啊.特地写一篇博 ...
- MFC拖拽、选择目录、遍历文件
1.选择目录 void CDecryptFileDlg::OnBnClickedSel() { std::wstring selectedDir; WCHAR szDir[MAX_PATH]; Zer ...
- TreeView 拖拽 增删改
using Endv.Tools; using System; using System.Data; using System.Drawing; using System.IO; using Syst ...
- Qt之股票组件-自选股--列表可以拖拽、右键常用菜单
目录 一.开头嘴一嘴 二.效果展示 三.自选股列表 1.列表初始化 2.添加Item 3.右键菜单 4.拖拽Item 5.刷新数据 四.相关文章 原文链接:Qt之股票组件-自选股--列表可以拖拽.右键 ...
- ListView 多行拖拽排序
核心代码:修改ListView的属性,及绑定事件 // 初始化listView1. private void InitializeListView() { listView1.AllowDrop = ...
- Jquery 可拖拽的Ztree
比较懒,就只贴关键代码吧,自己把有用的属性全部打印出来了,也加了不少注释. 保存后涉及到的排序问题,刷新问题还未考虑到,后面有的话再加. $.fn.zTree.init($("#ztree& ...
- WPF拖拽文件(拖入拖出),监控拖拽到哪个位置,类似百度网盘拖拽
1.往wpf中拖文件 // xaml <Grid x:Name="grid_11" DragOver="Grid_11_DragOver" Drop=&q ...
- Delphi Treeview 用法(概念、属性、添加编辑插入节点、定位节点、拖拽等)
今天再细研究了一下Treeview的用法,网上虽然总结了很多,但是还是有很多节点没有讲到了,也给使用中遇到很多问题.特地总结一下: 1.概念 Treeview用于显示按照树形结构进行组织的数据.Tre ...
- winform 两个TreeView间拖拽节点
/// <summary> /// 正在拖拽的节点 /// </summary> private TreeNode DragNode = null; /// <summa ...
随机推荐
- http协议请求规则与dotNet的解析
请求方法URI协议/版本 请求的第一行是"方法URL议/版本":GET/sample.jsp HTTP/1.1 以上代码中"GET"代表请求方法,"/ ...
- VIM一些常用命令,方法,配置
配置文件地址 github 工具只是为了更好的工具,选择一种,坚持使用学习记忆,熬过瓶颈期就可以了. 现在我基本大的项目是IDE+vim 插件,写小代码是VIM. 常用的操作,便捷的方法 1.如何选中 ...
- mysql创建自定义函数与存储过程
mysql创建自定义函数与存储过程 一 创建自定义函数 在使用mysql的过程中,mysql自带的函数可能不能完成我们的业务需求,这时就需要自定义函数,例如笔者在开发过程中遇到下面这个问题 mysql ...
- 矩阵分解(rank decomposition)文章代码汇总
矩阵分解(rank decomposition)文章代码汇总 矩阵分解(rank decomposition) 本文收集了现有矩阵分解的几乎所有算法和应用,原文链接:https://sites.goo ...
- Ubuntu使用wget下载jdk问题
使用以下命令可下载成功,否则下载下来的可能是一个html文档. wget --no-cookies --no-check-certificate --header "Cookie:gpw_e ...
- Android应用架构
Android开发生态圈的节奏非常之快.每周都会有新的工具诞生,类库的更新,博客的发表以及技术探讨.如果你外出度假一个月,当你回来的时候可能已经发布了新版本的Support Library或者Play ...
- Android UI:机智的远程动态更新策略
问题描述 做过Android开发的人都遇到过这样的问题:随着需求的变化,某些入口界面通常会出现 UI的增加.减少.内容变化.以及跳转界面发生变化等问题.每次发生变化都要手动修改代码,而入口界面通常具有 ...
- Android调用MediaScanner进行新产生的媒体文件扫描
有时候,我们拍了一张图片或录制了一段视频,图库应用默认没有将这些新产生的文件识别出来所以打开图库或视频播放器发现没有找到这些文件,需要调用MediaScanner扫描一下才会出来.从FFMPEG中找了 ...
- BZOJ3323: [Scoi2013]多项式的运算
3323: [Scoi2013]多项式的运算 Time Limit: 12 Sec Memory Limit: 64 MBSubmit: 128 Solved: 33[Submit][Status ...
- team geek
1. 转载自http://book.douban.com/review/6007037/,版权归丸子(^.^)v所有. New Google employees (we call “Nooglers” ...