PropertyGrid实现文件选择项
原来公司的一段代码,现在给朋友写的软件里也用上了,看样用处挺多,所以保存一下。
自定义属性类:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using System.Windows.Forms; namespace MaoYoo.ChinaMobile.Core.PropertyGridUI
{
public class PropertyGridFileSelector:UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
} public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
{ IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)); if (edSvc != null)
{ // 可以打开任何特定的对话框
OpenFileDialog dialog = new OpenFileDialog
{
AddExtension = false,
Title = "打开文件",
Filter = "*.xls|*.xls|*.xlsx|*.xlsx"
};
if (dialog.ShowDialog().Equals(DialogResult.OK))
{
return dialog.FileName;
}
}
return value;
}
}
}
文件选择定义:
[Category("文件")]
[DisplayName("数据文件")]
[Description("用户数据列表文件路径")]
[ReadOnlyAttribute(false), Browsable(true)]
[Editor(typeof(PropertyGridFileSelector), typeof(UITypeEditor))]
public string File
{
get;
set;
}
操作属性的值:
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Forms; namespace MaoYoo.ChinaMobile.Core
{ public class PropertyGridHelper
{
/// <summary>
/// 设置指定PropertyGrid中已设置了SelectObject对象的属性控件中指定某个自定义特性的值
/// </summary>
/// <typeparam name="T">要设置的属性类型</typeparam>
/// <param name="grid">PropertyGrid对象</param>
/// <param name="name">要设置的属性的名称</param>
/// <param name="field">要设置的自定义特性名称</param>
/// <param name="value">要设置的自定义特性的值</param>
public static void SetPropertyValue<T>(PropertyGrid grid, string name, string field, object value)
{
if (grid.SelectedObject == null)
throw new ArgumentException(string.Format("指定的PropertyGrid对象不包含{0}对象", "SelectObject")); AttributeCollection attrs = TypeDescriptor.GetProperties(grid.SelectedObject)[name].Attributes; FieldInfo fld = typeof(T).GetField(field, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance); if (fld != null) fld.SetValue(attrs[typeof(T)], value); grid.Refresh();
}
/// <summary>
/// 设置指对象指定自定义特性或其相关特性的值
/// </summary>
/// <typeparam name="T">要设置的属性类型</typeparam>
/// <param name="graph">要设置的对象</param>
/// <param name="name">要设置的属性的名称</param>
/// <param name="field">要设置的自定义特性名称</param>
/// <param name="value">要设置的自定义特性的值</param>
public static void SetPropertyValue<T>(object graph, string name, string field, object value)
{
if (graph == null)
throw new ArgumentException(string.Format("指定的对象{0}不能为空", graph)); AttributeCollection attrs = TypeDescriptor.GetProperties(graph)[name].Attributes; FieldInfo fld = typeof(T).GetField(field, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance); fld.SetValue(attrs[typeof(T)], value);
}
}
}
设置某个属性的自定义特性:
[Category("余额")]
[Description("是否启用余额")]
[DisplayName("余额匹配")]
[ReadOnlyAttribute(false), Browsable(true)]
public bool Enabled
{
get { return mEnabledBalance; }
set
{
PropertyGridHelper.SetPropertyValue<ReadOnlyAttribute>(this, "Balance", "isReadOnly", !value);
mEnabledBalance = value;
}
} private bool mEnabledBalance = true;
PropertyGrid实现文件选择项的更多相关文章
- iOS开发——UI篇&下拉弹出列表选择项效果
下拉弹出列表选择项效果 右边菜单中的按键,点击弹出一个列表可选择,选择其中一个,响应相应的事件并把文字显示在右边的菜单上:弹出下拉效果使用LMDropdownView插件,可以用POD进行加载pod ...
- CentOS6.9-zabbix3.2启动失败原因及页面没有mysql选择项
环境内核信息: [root@zabbix- ~]# uname -a Linux lodboyedu- -.el6.x86_64 # SMP Tue Mar :: UTC x86_64 x86_64 ...
- 使ListView控件中的选择项高亮显示
实现效果: 知识运用: ListView控件的SelectedItems属性 //获取在ListView控件中被选中数据项的集合 public ListView.SelectedListViewIte ...
- 重读APUE(3)-dup与文件表项
下图为调用dup之后的文件指针状态,包含如下信息: 1. dup选择了一个最小的未使用的描述符3: 2. dup(1)之后,描述符1和描述符3指向同一个文件表项: 3. dup(1)的返回值即为复制得 ...
- 文件描述符、文件表项指针、inode节点的关系
内核使用3种数据结构表示打开的文件,他们之间的关系决定了在文件共享方面一个进程对另一个进程的影响. (1) 每个进程在进程表中都有一个纪录项,纪录项中包含一张打开文件描述符表,每个文件描述符各占一项, ...
- cordova加载层、进度条、文件选择插件
在做cordova项目的时候,感觉应用的响应速度跟原生应用比相差甚远,一个主要问题就是如加载层.进度条等弹出对话框的效率不行.毕竟项目中的这些弹框都是用dom拼成的,dom的渲染效率和原生控件比起来慢 ...
- js点击某个图标或按钮弹出文件选择框
<HTML> <head> <script type="text/javascript" src="script/jquery-1.6.2. ...
- Java文件选择对话框(文件选择器JFileChooser)的使用:以一个文件加密器为例
文件加密器,操作过程肯定涉及到文件选择器的使用,所以这里以文件加密器为例.下例为我自己写的一个文件加密器,没什么特别的加密算法,只为演示文件选择器JFileChooser的使用. 加密器界面如图: 项 ...
- Java——文件选择框:JFileChooser
import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import j ...
- Js获取下拉框当前选择项的文本和值
现在有一个Id为AreaId的下拉框,要获取它当前选择项的文本和值有以下方法: <span class="red">* </span> 地 区: ...
随机推荐
- centos7.6 挂载镜像配置本地yum源
镜像下载 http://mirrors.aliyun.com/centos-vault/7.6.1810/isos/x86_64/ 配置本地yum源 1.安装Centos后默认的Yum源如下 [roo ...
- Sqoop导入MySQL表中数据到Hive出现错误: ERROR hive.HiveConfig: Make sure HIVE_CONF_DIR is set correctly.ERROR tool.ImportTool: Import failed:
1.问题描述: (1)问题示例: [Hadoop@master TestDir]$ sqoop import --connect jdbc:mysql://master:3306/source?use ...
- idea plugins搜不出来东西
今天学习Vue要安装一个Vue.js的插件,在idea的plugins上搜死活搜不出来,参照了网上的关防火墙,勾选什么auto什么的选项还是不管用,最后瞎捣鼓弄好了,在博客上记录一下. 打开手机数据( ...
- vue3 ThreeJS 引入obj模型过暗的问题
当我单纯地用MTLLoader引入材质, OBJLoader引入模型并添加到场景中时, 发现模型非常得暗. 需要将环境光的强度设置到3.5左右看起来才比较正常. 但正常情况下环境光的值不应该超出1. ...
- spring-service.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- CSS函数var
/*全局变量保存的地方*/ :root { --main-bg-color: red; /* 变量名必须以--开头 */ } var(custom-property-name, value) 值 描述 ...
- Javaweb学习笔记第十四弹---对于Cookie和Filter的学习
Apache Tomcat - Tomcat Native Downloads 会话追踪技术 会话:打开浏览器,建立连接,直到一方断开连接,会话才会结束:在一次会议中,可以有多次请求. 会话追踪:在多 ...
- 声网推出首个完整实时合唱解决方案 即将上线“咪哒”全国线下K歌房
4月20日,声网Agora宣布对实时合唱技术方案全面升级,帮助国内知名迷你KTV品牌"咪哒"实现国内首个支持多终端.多人合唱.高音质的完整实时合唱解决方案的落地,结束了国内K歌行业 ...
- Web 开发的常规流程
Web 开发的常规流程 What is the Web? 简单地说,网络是一个遍布全球的网络,它连接大量设备并允许它们相互通信 Internet 上的网站托管在称为服务器的设备上,当您与 Intern ...
- GO实现Redis:GO实现Redis集群(5)
采用一致性hash算法将key分散到不同的节点,客户端可以连接到集群中任意一个节点 https://github.com/csgopher/go-redis 本文涉及以下文件: consistenth ...