场景

使用DevExpress的TreeList显示本磁盘下文件目录并在树节点上右键实现删除与添加文件。

效果

自定义右键效果

实现

首先在包含Treelist的窗体的load方法中对treelist进行初始化

Common.DataTreeListHelper.RefreshTreeData(this.treeList1, );

其中this.treeList1就是当前窗体的treelist对象

然后第二个参数是默认展开级别。

public static void RefreshTreeData(DevExpress.XtraTreeList.TreeList treeList, int expandToLevel)
{
string rootNodeId = Common.Global.AppConfig.TestDataDir;
string rootNodeText = ICSharpCode.Core.StringParser.Parse(ResourceService.GetString("Pad_DataTree_RootNodeText")); //"全部实验数据";
string fieldName = "NodeText";
string keyFieldName = "Id";
string parentFieldName = "ParentId";
List<DataTreeNode> data = new List<DataTreeNode>();
data = DataTreeListHelper.ParseDir(Common.Global.AppConfig.TestDataDir, data);
data.Add(new DataTreeNode() { Id = rootNodeId, ParentId = String.Empty, NodeText = rootNodeText, NodeType = DataTreeNodeTypes.Folder });
DataTreeListHelper.SetTreeListDataSource(treeList, data, fieldName, keyFieldName, parentFieldName);
treeList.ExpandToLevel(expandToLevel);
}

在上面方法中新建根节点,根节点的Id就是要显示的目录,在配置文件中读取。
根节点的显示文本就是显示“全部实验数据”,从配置文件中获取。

然后调用工具类将目录结构转换成带父子级关系的节点的list,然后再将根节点添加到list。

然后调用设置treeList数据源的方法。

在上面方法中存取节点信息的DataTreeNode

public class DataTreeNode
{
private string id;
private string parentId;
private string nodeText;
private string createDate;
private string fullPath;
private string taskFile;
private string barcode;
private DataTreeNodeTypes nodeType = DataTreeNodeTypes.Folder; public string Id
{
get { return id; }
set { id = value; }
} public string ParentId
{
get { return parentId; }
set { parentId = value; }
} public string NodeText
{
get { return nodeText; }
set { nodeText = value; }
} public string CreateDate
{
get { return createDate; }
set { createDate = value; }
} public string FullPath
{
get { return fullPath; }
set { fullPath = value; }
} public string TaskFile
{
get { return taskFile; }
set { taskFile = value; }
} public string Barcode
{
get { return barcode; }
set { barcode = value; }
} public DataTreeNodeTypes NodeType
{
get { return nodeType; }
set { nodeType = value; }
}
}

在上面方法中将目录结构转换为节点list的方法

public static List<DataTreeNode> ParseDir(string dataRootDir, List<DataTreeNode> data)
{
if (data == null)
{
data = new List<DataTreeNode>();
} if (!System.IO.Directory.Exists(dataRootDir))
{
return data;
} DataTreeNode node = null; System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(dataRootDir); System.IO.DirectoryInfo[] subDirs = dir.GetDirectories();
foreach(System.IO.DirectoryInfo subDir in subDirs)
{
node = new DataTreeNode();
node.Id = subDir.FullName;
node.ParentId = dir.FullName;
node.NodeText = subDir.Name;
node.CreateDate = String.Format("{0:yyyy-MM-dd HH:mm:ss}", subDir.CreationTime);
node.FullPath = subDir.FullName;
node.TaskFile = String.Empty; //任务文件名
node.NodeType = DataTreeNodeTypes.Folder;
data.Add(node); ParseDir(subDir.FullName, data);
} System.IO.FileInfo[] subFiles = dir.GetFiles(); return data;
}

通过递归将上面传递过来的目录下的结构构造成节点的list并返回。

通过解析实验目录的方法返回list后再调用刷新treelist节点的方法

SetTreeListDataSource

public static void SetTreeListDataSource(DevExpress.XtraTreeList.TreeList treeList, List<DataTreeNode> data, string fieldName, string keyFieldName, string parentFieldName)
{
#region 设置节点图标 System.Windows.Forms.ImageList imgList = new System.Windows.Forms.ImageList();
imgList.Images.AddRange(imgs); treeList.SelectImageList = imgList; //目录展开
treeList.AfterExpand -= treeList_AfterExpand;
treeList.AfterExpand += treeList_AfterExpand; //目录折叠
treeList.AfterCollapse -= treeList_AfterCollapse;
treeList.AfterCollapse += treeList_AfterCollapse; //数据节点单击,开启整行选中
treeList.MouseClick -= treeList_MouseClick;
treeList.MouseClick += treeList_MouseClick; //数据节点双击选中
treeList.MouseDoubleClick -= treeList_MouseDoubleClick;
treeList.MouseDoubleClick += treeList_MouseDoubleClick; //焦点离开事件
treeList.LostFocus -= treeList_LostFocus;
treeList.LostFocus += treeList_LostFocus; #endregion #region 设置列头、节点指示器面板、表格线样式 treeList.OptionsView.ShowColumns = false; //隐藏列标头
treeList.OptionsView.ShowIndicator = false; //隐藏节点指示器面板 treeList.OptionsView.ShowHorzLines = false; //隐藏水平表格线
treeList.OptionsView.ShowVertLines = false; //隐藏垂直表格线
treeList.OptionsView.ShowIndentAsRowStyle = false; #endregion #region 初始禁用单元格选中,禁用整行选中 treeList.OptionsView.ShowFocusedFrame = true; //设置显示焦点框
treeList.OptionsSelection.EnableAppearanceFocusedCell = false; //禁用单元格选中
treeList.OptionsSelection.EnableAppearanceFocusedRow = false; //禁用正行选中
//treeList.Appearance.FocusedRow.BackColor = System.Drawing.Color.Red; //设置焦点行背景色 #endregion #region 设置TreeList的展开折叠按钮样式和树线样式 treeList.OptionsView.ShowButtons = true; //显示展开折叠按钮
treeList.LookAndFeel.UseDefaultLookAndFeel = false; //禁用默认外观与感觉
treeList.LookAndFeel.UseWindowsXPTheme = true; //使用WindowsXP主题
treeList.TreeLineStyle = DevExpress.XtraTreeList.LineStyle.Percent50; //设置树线的样式 #endregion #region 添加单列 DevExpress.XtraTreeList.Columns.TreeListColumn colNode = new DevExpress.XtraTreeList.Columns.TreeListColumn();
colNode.Name = String.Format("col{0}", fieldName);
colNode.Caption = fieldName;
colNode.FieldName = fieldName;
colNode.VisibleIndex = ;
colNode.Visible = true; colNode.OptionsColumn.AllowEdit = false; //是否允许编辑
colNode.OptionsColumn.AllowMove = false; //是否允许移动
colNode.OptionsColumn.AllowMoveToCustomizationForm = false; //是否允许移动至自定义窗体
colNode.OptionsColumn.AllowSort = false; //是否允许排序
colNode.OptionsColumn.FixedWidth = false; //是否固定列宽
colNode.OptionsColumn.ReadOnly = true; //是否只读
colNode.OptionsColumn.ShowInCustomizationForm = true; //移除列后是否允许在自定义窗体中显示 treeList.Columns.Clear();
treeList.Columns.AddRange(new DevExpress.XtraTreeList.Columns.TreeListColumn[] { colNode }); #endregion #region 绑定数据源 treeList.DataSource = null;
treeList.KeyFieldName = keyFieldName;
treeList.ParentFieldName = parentFieldName;
treeList.DataSource = data;
treeList.RefreshDataSource(); #endregion #region 初始化图标 SetNodeImageIndex(treeList.Nodes.FirstOrDefault()); #endregion
}

如果不考虑根据文件还是文件夹设置节点图标和绑定其他双击事件等。

直接关注鼠标单击事件的绑定和下面初始化样式的设置。

在单击鼠标节点绑定的方法中

private static void treeList_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
DevExpress.XtraTreeList.TreeList treeList = sender as DevExpress.XtraTreeList.TreeList;
if (treeList != null && treeList.Selection.Count == )
{
object idValue = null;
string strIdValue = String.Empty;
DataTreeNode nodeData = null;
List<DataTreeNode> datasource = treeList.DataSource as List<DataTreeNode>;
if (datasource != null)
{
idValue = treeList.Selection[].GetValue("Id");
strIdValue = idValue.ToString();
nodeData = datasource.Where<DataTreeNode>(p => p.Id == strIdValue).FirstOrDefault<DataTreeNode>();
if (nodeData != null)
{
if (nodeData.NodeType == DataTreeNodeTypes.File)
{ treeList.OptionsSelection.EnableAppearanceFocusedRow = true; //启用整行选中
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
System.Windows.Forms.ContextMenu ctxMenu = new System.Windows.Forms.ContextMenu(); #region 右键弹出上下文菜单 - 删除数据文件 System.Windows.Forms.MenuItem mnuDelete = new System.Windows.Forms.MenuItem();
mnuDelete.Text = "删除";
mnuDelete.Click += delegate(object s, EventArgs ea) {
DialogResult dialogResult = DevExpress.XtraEditors.XtraMessageBox.Show(String.Format("确定要删除此实验数据吗[{0}]?\r\n删 除后无法恢复!", nodeData.Id), "标题", System.Windows.Forms.MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dialogResult == DialogResult.Yes)
{
try
{
string fileName = String.Empty; #region 删除数据文件 fileName = String.Format("{0}{1}", nodeData.Id, Global.MAIN_EXT);
if (System.IO.File.Exists(fileName))
{
System.IO.File.Delete(fileName);
} #endregion #region 删除对应的树节点 DevExpress.XtraTreeList.Nodes.TreeListNode selectedNode = treeList.FindNodeByKeyID(nodeData.Id);
if (selectedNode != null)
{
selectedNode.ParentNode.Nodes.Remove(selectedNode);
} #endregion treeList.OptionsSelection.EnableAppearanceFocusedRow = false; //禁用整行选中
}
catch(Exception ex)
{
ICSharpCode.Core.LoggingService<DataTreeListHelper>.Error("删除实验数据异常:" + ex.Message, ex);
DevExpress.XtraEditors.XtraMessageBox.Show("删除实验数据异常:" + ex.Message, "标题", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
};
ctxMenu.MenuItems.Add(mnuDelete);
#endregion #region 右键弹出上下文菜单 - 重命名数据文件 System.Windows.Forms.MenuItem mnuReName = new System.Windows.Forms.MenuItem();
mnuReName.Text = "重命名";
mnuReName.Click += delegate(object s, EventArgs ea)
{
//获取当前文件名
string oldName = Path.GetFileNameWithoutExtension(strIdValue); Dialog.FrmReName frmReName = new FrmReName(oldName);
frmReName.StartPosition = FormStartPosition.CenterScreen;
DialogResult result = frmReName.ShowDialog();
if (result == DialogResult.OK)
{
//刷入框新设置的文件名
string newName = frmReName.FileName;
//获取原来路径
string filePath = Path.GetDirectoryName(strIdValue);
//使用原来路径加 + 新文件名 结合成新文件路径
string newFilePath = Path.Combine(filePath, newName);
DialogResult dialogResult = DevExpress.XtraEditors.XtraMessageBox.Show(String.Format("确定要将实验数据[{0}]重命名为 [{}]吗?", nodeData.Id, newName), "标题", System.Windows.Forms.MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dialogResult == DialogResult.Yes)
{
try
{
string fileName = String.Empty;
string newFileName = String.Empty; #region 重命名主通道数据文件 fileName = String.Format("{0}{1}", nodeData.Id, Global.MAIN_EXT);
newFileName = String.Format("{0}{1}", newFilePath, Global.MAIN_EXT);
if (System.IO.File.Exists(fileName))
{
FileInfo fi = new FileInfo(fileName);
fi.MoveTo(newFileName);
} #endregion //刷新树
Common.DataTreeListHelper.TriggerRefreshDataEvent();
XtraMessageBox.Show("重命名成功");
treeList.OptionsSelection.EnableAppearanceFocusedRow = false; //禁用整行选中
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<DataTreeListHelper>.Error("删除实验数据异常:" + ex.Message, ex);
DevExpress.XtraEditors.XtraMessageBox.Show("删除实验数据异常:" + ex.Message, "标题", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
} };
ctxMenu.MenuItems.Add(mnuReName);
#endregion #endregion ctxMenu.Show(treeList, new System.Drawing.Point(e.X, e.Y));
} return;
}
}
}
treeList.OptionsSelection.EnableAppearanceFocusedRow = false; //禁用整行选中
}
}

其中在进行重命名时需要弹出一个窗体

具体实现参照:

Winform巧用窗体设计完成弹窗数值绑定-以重命名弹窗为例:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103155532

DevExpress的TreeList实现显示本地文件目录并自定义右键实现删除与重命名文件的更多相关文章

  1. Winforn中DevExpress的TreeList中显示某路径下的所有目录和文件(附源码下载)

    场景 Winform中DevExpress的TreeList的入门使用教程(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  2. git 本地重命名文件夹大小写并提交到远程分支

    git branch 查看本地分支 git branch -a 查看本地 本地分支可直接切换:git checkout name 进入正题: 1.文件夹备份 2.git config core.ign ...

  3. Java+JQuery实现网页显示本地文件目录(含源码)

    原文地址:http://www.cnblogs.com/liaoyu/p/uudisk.html 源码地址:https://github.com/liaoyu/uudisk 前段时间为是练习JQuer ...

  4. DevExpress的TreeList实现自定义右键菜单打开文件选择对话框

    场景 DevExpress的TreeList实现节点上添加自定义右键菜单并实现删除节点功能: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/det ...

  5. DevExpress的TreeList怎样设置数据源使其显示成单列树形结构

    场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...

  6. 关于 DevExpress.XtraTreeList.TreeList 树形控件 的操作

    作为一个C#程序员,在写程序时一直以来都使用的微软那一套控件,用起来特别爽,可是最近公司的一个项目用到了DevExpress框架,不用不知道,一用吓一跳,不得不承认这个框架确实很强大,效果也很炫,但是 ...

  7. 在Winform开发框架中使用DevExpress的TreeList和TreeListLookupEdit控件

    DevExpress提供的树形列表控件TreeList和树形下拉列表控件TreeListLookupEdit都是非常强大的一个控件,它和我们传统Winform的TreeView控件使用上有所不同,我一 ...

  8. DevExpress中TreeList树样式调整

    DevExpress的TreeList默认是没有树状线的,修改TreeLineStyle属性无效,这对于Tree并不好看. 解决方案一 官方解释说对于DevExpress的标准主题是不支持TreeLi ...

  9. DevExpress的TreeList怎样给树节点设置图标

    场景 DevExpress的TreeList怎样设置数据源使其显示成单列树形结构: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/ ...

随机推荐

  1. 2、手写Unity容器--第一层依赖注入

    这个场景跟<手写Unity容器--极致简陋版Unity容器>不同,这里构造AndroidPhone的时候,AndroidPhone依赖于1个IPad 1.IPhone接口 namespac ...

  2. Winform中使用Timer实现滚动字幕效果(附代码下载)

    场景 效果 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 新建一个Fo ...

  3. 利用百度AI快速开发出一款“问答机器人”并接入小程序

    先看实现效果: 利用百度UNIT预置的智能问答技能和微信小程序,实现语音问答机器人.这里主要介绍小程序功能开发实现过程,分享主要功能实现的子程序模块,都是干货! 想了解UNIT预置技能调用,请参看我之 ...

  4. Eclipse——关联源代码

    Eclipse——关联源代码 摘要:本文主要说明了如何在Eclipse里关联源代码. 下载源码包 首先去想要关联的jar包的官网下载对应jar包的源代码,拿Tomcat的类库举例,先去官网下载源码包: ...

  5. JavaWeb 实现简单登录、注册功能

    1.首先创建一个简单的动态Javaweb项目 2.然后手动建立文件目录: 项目创建好之后,在src下建几个包: controller:控制器,负责转发请求,对请求进行处理,主要存放servlet: d ...

  6. [小技巧]你真的了解C#中的Math.Round么?

    今天在某.NET Core 群中看到有人在问Math.Round的问题.其实这个问题之前有很多人遇到了,在此总结一下. 开发者为了实现小数点后 2 位的四舍五入,编写了如下代码, var num = ...

  7. fastjson又被发现漏洞,这次危害可能会导致服务瘫痪

    0x00 漏洞背景 2019年9月5日,fastjson在commit 995845170527221ca0293cf290e33a7d6cb52bf7上提交了旨在修复当字符串中包含\\x转义字符时可 ...

  8. Spring 关于ResponseBody注解的作用

    //responseBody一般是作用在方法上的,加上该注解表示该方法的返回结果直接写到Http response Body中,常用在ajax异步请求中, //在RequestMapping中 ret ...

  9. Mvc导入

    [HttpPost] public void Import() { //获取文件 HttpPostedFileBase fileBase = Request.Files["file" ...

  10. Class文件结构-常量池

    常量池里存放:1.字面量(Literal) • 文本字符串 • 声明为final的常量值(final的8种基本类型) • 非final的基本类型也可能进(doublefloatlong)2.符号引用( ...