C#基础系列:开发自己的窗体设计器(PropertyGrid显示中文属性名)

C# 报表设计器 (winform 设计端)开发与实现生成网页的HTML报表 开放源码及调试 记得2000年之前,公司的项目基本上都要用到报表,以前我们常用的方法就是针对客户的需求来定制化开发(基本上是死写代码)来实现,经常导致项目经常性的延期,因为客户的需求经常会变化,随着用户的使用认知度的提高,对报表的要求越来越高,导致程序员不停的修改代码来实现,效率不高、结束遥遥无期。。。非常的痛苦;当然市面上有很多报表开发工具可以实现,但是针对小公司来说采购一套这样的系统的成本也非常的高,所以我们 阅读详情

既然是一个窗体设计器,那就应该能够设置控件的属性,设置属性最好的当然是PropertyGrid了,我们仅仅需要使用一个PropertyGrid.SelectedObject = Control就可以搞定,让PropertyGrid显示Control的所有属性。可是这里显示的属性名是英文的。对于我们开发人员来说这无可厚非,我们也乐于接受。并且让PropertyGrid显示中文属性名,这对于我们开发人员的使用来说显得多此一举。可是,对于我这种类型的一个应用工具,英文属性名对于很多客户来说可能就很难懂了。所以应该让PrpertyGrid能够显示中文属性名。

如图:

 

另外,对于这样的一个系统。并不是控件的所有属性都是用户希望的,可能用户希望看到的仅仅是控件高度、控件宽度、控件文本。。等等的属性,但是如果直接将一个控件属性全部显示给用户的话,估计对用户造成的干扰和困惑是很大的。如何解决这个问题呢?其实用户控件开发的时候,如果我们不希望此属性在PropertyGrid中显示,我们可以设置这个控件的Attribute,如:

[Browsable(false)]

publicintWidth

{

   get{ }

   set{ }

}

通过使用BrowsableAttribute就可以设置将此属性对PropertyGrid隐藏。

 

你可能要问到了,对于控件来说,其中的很多属性都是直接继承来的,我们并没有办法控制是否对PropertyGrid隐藏啊?呵呵,对啊,这就是我下面要说的解决方法(当然此方法显得不是很灵活,但是对于这种类型的系统的确相当有用)。

在我的解决方式中,我不直接这样PropertyGrid.SelectedObject = Control,而是把这个Control替换成一个专门为此类型的Control设计的类对象上。比如我对TextBox设计了一个TextBoxProperty,这样我们使用的是PropertyGrid.SelectedObject =TextBoxProperty的一个对象。

下面就是TextBoxProperty的代码:

  1. public class TextBoxProperty : PropertyBase
  2. {
  3.     private TextBox _Control;
  4.     public TextBoxProperty()
  5.     {
  6.     }
  7.     public TextBoxProperty(TextBox control)
  8.     {
  9.         this._Control = control;
  10.     }
  11.     [MyControlAttibute("文本", "获取或者设置控件文本", "")]
  12.     public string Text
  13.     {
  14.         get { return this._Control.Text; }
  15.         set
  16.         {
  17.             this._Control.Text = value;
  18.         }
  19.     }
  20.     [MyControlAttibute("宽度", "获取或者设置控件宽度", "")]
  21.     public int Width
  22.     {
  23.         get { return this._Control.Width; }
  24.         set
  25.         {
  26.             this._Control.Width = (int)value;
  27.         }
  28.     }
  29.     [MyControlAttibute("高度", "获取或者设置控件高度", "")]
  30.     public int Height
  31.     {
  32.         get { return this._Control.Height; }
  33.         set
  34.         {
  35.             this._Control.Height = (int)value;
  36.         }
  37.     }
  38.     [MyControlAttibute("上边距", "获取或者设置控件上边位置", "")]
  39.     public int Top
  40.     {
  41.         get { return this._Control.Top; }
  42.         set
  43.         {
  44.             this._Control.Top = value;
  45.         }
  46.     }
  47.     [MyControlAttibute("左边距", "获取或者设置控件左边位置", "")]
  48.     public int Left
  49.     {
  50.         get { return this._Control.Left; }
  51.         set
  52.         {
  53.             this._Control.Left = value;
  54.         }
  55.     }
  56.     [MyControlAttibute("背景色", "获取或者设置控件背景颜色", "")]
  57.     public Color BackColor
  58.     {
  59.         get { return this._Control.BackColor; }
  60.         set
  61.         {
  62.             this._Control.BackColor = value;
  63.         }
  64.     }
  65.     [MyControlAttibute("前景色", "获取或者设置控件的前景颜色", "")]
  66.     public Color ForeColor
  67.     {
  68.         get { return this._Control.ForeColor; }
  69.         set
  70.         {
  71.             this._Control.ForeColor = value;
  72.         }
  73.     }
  74. }

你从代码里面已经看出了一些端倪了,在TextBoxProperty中每个要属性都增加了MyControlAttibute,具体Attribute的概念和使用方法您可以参考我的另一篇Blog文《C#基础系列:实现自己的ORM(反射以及Attribute在ORM中的应用)》。这里对Attribute做了详细的介绍(对初学者有效)。所以我就直接贴出MyControlAttibute的代码好了:

  1. public class MyControlAttibute : Attribute
  2. {
  3.     private string _PropertyName;
  4.     private string _PropertyDescription;
  5.     private object _DefaultValue;
  6.     public MyControlAttibute(string Name, string Description, object DefalutValue)
  7.     {
  8.         this._PropertyName = Name;
  9.         this._PropertyDescription = Description;
  10.         this._DefaultValue = DefalutValue;
  11.     }
  12.     public MyControlAttibute(string Name, string Description)
  13.     {
  14.         this._PropertyName = Name;
  15.         this._PropertyDescription = Description;
  16.         this._DefaultValue = "";
  17.     }
  18.     public MyControlAttibute(string Name)
  19.     {
  20.         this._PropertyName = Name;
  21.         this._PropertyDescription = "";
  22.         this._DefaultValue = "";
  23.     }
  24.     public string PropertyName
  25.     {
  26.         get { return this._PropertyName; }
  27.     }
  28.     public string PropertyDescription
  29.     {
  30.         get { return this._PropertyDescription; }
  31.     }
  32.     public object DefaultValue
  33.     {
  34.         get { return this._DefaultValue; }
  35.     }
  36. }

通过上面的两段代码,你已经初步看出了在PropertyGrid中显示中文属性以及仅仅显示我们需要的属性的基本思路。下面就介绍的就是怎么让MyControlAttibute中定义了的中文属性名在PropertyGrid中显示出来。下面这段代码,我仅仅贡献了PropertyStub中这个函数的实现。所以不做过多解释了:

  1. public override string DisplayName
  2. {
  3.     get
  4.     {
  5.         if (info != null)
  6.         {
  7.             MyControlAttibute uicontrolattibute = (MyControlAttibute)Attribute.GetCustomAttribute(info, typeof(MyControlAttibute));
  8.             if (uicontrolattibute != null)
  9.                 return uicontrolattibute.PropertyName;
  10.             else
  11.             {
  12.                 return info.Name;
  13.             }
  14.         }
  15.         else
  16.             return "";
  17.     }
  18. }

整个代码如下,里面包含两个类,你直接拷贝出来就可以使用了:

  1. public delegate void PropertyChanged(object Value);
  2. /// <summary>
  3. /// 主要是实现中文化属性显示
  4. /// </summary>
  5. public class PropertyBase : ICustomTypeDescriptor
  6. {
  7.     /// <summary>
  8.     /// 下面这段代码来源于:http://www.bluespace.cn/Html/Csdn/2_47/View_4702219.html
  9.     /// </summary>
  10.     /// <returns></returns>
  11.     #region ICustomTypeDescriptor 显式接口定义
  12.     AttributeCollection ICustomTypeDescriptor.GetAttributes()
  13.     {
  14.         return TypeDescriptor.GetAttributes(this, true);
  15.     }
  16.     string ICustomTypeDescriptor.GetClassName()
  17.     {
  18.         return TypeDescriptor.GetClassName(this, true);
  19.     }
  20.     string ICustomTypeDescriptor.GetComponentName()
  21.     {
  22.         return TypeDescriptor.GetComponentName(this, true);
  23.     }
  24.     TypeConverter ICustomTypeDescriptor.GetConverter()
  25.     {
  26.         return TypeDescriptor.GetConverter(this, true);
  27.     }
  28.     EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
  29.     {
  30.         return TypeDescriptor.GetDefaultEvent(this, true);
  31.     }
  32.     PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
  33.     {
  34.         return null;
  35.     }
  36.     object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
  37.     {
  38.         return TypeDescriptor.GetEditor(this, editorBaseType, true);
  39.     }
  40.     EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
  41.     {
  42.         return TypeDescriptor.GetEvents(this, true);
  43.     }
  44.     EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
  45.     {
  46.         return TypeDescriptor.GetEvents(this, attributes, true);
  47.     }
  48.     PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
  49.     {
  50.         return ((ICustomTypeDescriptor)this).GetProperties(new Attribute[0]);
  51.     }
  52.     PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
  53.     {
  54.         ArrayList props = new ArrayList();
  55.         Type thisType = this.GetType();
  56.         PropertyInfo[] pis = thisType.GetProperties();
  57.         foreach (PropertyInfo p in pis)
  58.         {
  59.             if (p.DeclaringType == thisType || p.PropertyType.ToString() == "System.Drawing.Color")
  60.             {
  61.                 //判断属性是否显示
  62.                 BrowsableAttribute Browsable = (BrowsableAttribute)Attribute.GetCustomAttribute(p, typeof(BrowsableAttribute));
  63.                 if (Browsable != null)
  64.                 {
  65.                     if (Browsable.Browsable == true || p.PropertyType.ToString() == "System.Drawing.Color")
  66.                     {
  67.                         PropertyStub psd = new PropertyStub(p, attributes);
  68.                         props.Add(psd);
  69.                     }
  70.                 }
  71.                 else
  72.                 {
  73.                     PropertyStub psd = new PropertyStub(p, attributes);
  74.                     props.Add(psd);
  75.                 }
  76.             }
  77.         }
  78.         PropertyDescriptor[] propArray = (PropertyDescriptor[])props.ToArray(typeof(PropertyDescriptor));
  79.         return new PropertyDescriptorCollection(propArray);
  80.     }
  81.     object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
  82.     {
  83.         return this;
  84.     }
  85.     #endregion
  86. }
  87. /// <summary>
  88. /// 下面这段代码来源于:http://www.bluespace.cn/Html/Csdn/2_47/View_4702219.html
  89. /// </summary>
  90. #region PropertyStub 定义
  91. public class PropertyStub : PropertyDescriptor
  92. {
  93.     PropertyInfo info;
  94.     public PropertyStub(PropertyInfo propertyInfo, Attribute[] attrs)
  95.         : base(propertyInfo.Name, attrs)
  96.     {
  97.         this.info = propertyInfo;
  98.     }
  99.     public override Type ComponentType
  100.     {
  101.         get { return this.info.ReflectedType; }
  102.     }
  103.     public override bool IsReadOnly
  104.     {
  105.         get { return this.info.CanWrite == false; }
  106.     }
  107.     public override Type PropertyType
  108.     {
  109.         get { return this.info.PropertyType; }
  110.     }
  111.     public override bool CanResetValue(object component)
  112.     {
  113.         return false;
  114.     }
  115.     public override object GetValue(object component)
  116.     {
  117.         //Console.WriteLine("GetValue: " + component.GetHashCode());
  118.         try
  119.         {
  120.             return this.info.GetValue(component, null);
  121.         }
  122.         catch
  123.         {
  124.             return null;
  125.         }
  126.     }
  127.     public override void ResetValue(object component)
  128.     {
  129.     }
  130.     public override void SetValue(object component, object value)
  131.     {
  132.         //Console.WriteLine("SetValue: " + component.GetHashCode());
  133.         this.info.SetValue(component, value, null);
  134.     }
  135.     public override bool ShouldSerializeValue(object component)
  136.     {
  137.         return false;
  138.     }
  139.     //通过重载下面这个属性,可以将属性在PropertyGrid中的显示设置成中文
  140.     public override string DisplayName
  141.     {
  142.         get
  143.         {
  144.             if (info != null)
  145.             {
  146.                 MyControlAttibute uicontrolattibute = (MyControlAttibute)Attribute.GetCustomAttribute(info, typeof(MyControlAttibute));
  147.                 if (uicontrolattibute != null)
  148.                     return uicontrolattibute.PropertyName;
  149.                 else
  150. &nbs

Winform可视化表单设计器:Smart.FormDesigner与动态表单实现 在传统信息管理系统的开发中,表单字段频繁变更是常见的痛点,每次调整都需重新编译发布,效率低下。低代码与可视化拖拽理念的兴起,为解决这一问题提供了新思路:通过设计时与运行时分离,让界面布局由配置驱动。在Winform技术栈中,利用DesignSurface、DesignerHost等设计时服务,可构建类似Visual Studio的拖拽式表单设计器。控件属性通过PropertyGrid暴露,布局及扩展信息序列化为XML,运行时再通过反射动态创建控件、绑定事件与数据,从而实现表单的灵活配置与即时生效。这一方案不 阅读详情

相关推荐

C#利用DesignSurface如何实现简单的窗体设计器

主要介绍了C#利用DesignSurface如何实现简单窗体设计器的相关资料,文中通过图文及示例代码介绍的很详细,对大家具有一定的参考价值,需要的朋友们下面来一起学习学习吧。

写Form设计器尝试(三) 在窗体上添加控件

新建一个继承自ToolboxService的类,名为DemoToolboxService,加上必要的using语句,在所继承的类名上按鼠标右键,点实现抽象类,已经帮我们自动完成了DemoToolboxService的框架,由于我们需要在设计器窗体上显示一个工具箱,就像VS左侧的那个工具面板,不过我们现在做一个简单一点的,就用ListBox,在DemoToolboxService中添加一个类型为Li...

ins_white的博客 1170

加密视频如何破解 如何解除机器码绑定的视频

这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入 如何破解加密...

Q-980767270的博客 5万+

基于c#winform的可视化打印标签模板设计器及Labview与C#调用模板Demo

基于c#winform的可视化打印标签模板设计器及Labview与C#调用模板Demo

qiangpi6057的博客 1万+

C#.NET斗地主服务端核心架构与高性能实现

斗地主作为典型的实时对战棋牌逻辑,本质是状态驱动的分布式协同问题。其技术内核涵盖有限状态机建模、低延迟网络同步、无GC内存操作与轻量级AI决策等关键能力。在.NET平台下,借助Span<T>、ImmutableArray、命令/事件总线与内存池等特性,可构建出高吞吐(200+ QPS)、低延迟(<150ms)、强一致的可落地服务端。本文聚焦C#.NET生态中斗地主核心模块的设计取舍与压测验证,深入解析状态机驱动、CardSet结构体优化、Kestrel中间件锁、规则+概率混合AI等实战方案,为实时对战类业务

weixin_29726381的博客 350

AI测试实战:用文心一言5分钟生成登录功能测试用例(附Excel模板)

本文详细介绍了如何利用AI工具(如文心一言)在5分钟内高效生成登录功能测试用例,并提供了可直接应用的Excel模板。通过对比传统手工编写与AI生成的效率差异,展示了AI在软件测试领域的巨大优势,包括减少重复劳动、避免边界条件遗漏和提升格式标准化。文章还包含实战技巧、高级应用方案和行业最佳实践,助力测试人员快速提升工作效率。

weixin_30379531的博客 414

C#基础系列:开发自己的窗体设计器(总纲)

我所处的行业,所使用的业务系统都不大,但是个性需求极其多,每个地区,每家公司都可能存在很大的差别。所以我就想能够弄一个类似于vs2005的WinForm窗体设计器的开发平台,让用户拖拖拽拽就可以添加自己需要的控件,或者删除不需要的控件,从而完成大部分的工作,实现功能的扩展,而我需要做的,只不过是写写插件代码。 这种类型的系统目前已经有很多,但是那些都是属于别人的,即使我能够免费下载到一个,

老田低代码 1万+

别再混淆了!一文彻底搞懂STM32的BootROM、BootLoader和启动模式(附启动流程图)

本文深入解析STM32的启动机制,详细讲解BootROM、BootLoader与启动模式的区别与联系,并提供实战指南。通过清晰的启动流程图和配置示例,帮助开发者彻底理解STM32启动流程,解决常见的启动问题,提升嵌入式开发效率。

weixin_33728268的博客 546

开发自己的窗体设计器(PropertyGrid显示中文属性名)

http://blog.csdn.net/ronotian/article/details/3183907

Java_c# 1910

Grok 3国内怎么用?超详细使用指南+实用指令模板

主要是用于快速分析学术论文,提取关键信息,总结研究成果。Analyze the following research paper: [粘贴或上传论文内容]1. Extract key findings, methodologies, and conclusions. (提取主要发现、方法和结论。2. Identify statistical significance and data points. (识别统计显著性和数据点。

Survivor_sl的博客 1万+

高效爬取懂车帝车型数据:从静态解析到动态反爬实战

本文详细解析了如何高效爬取懂车帝车型数据,从静态HTML解析到动态接口反爬的完整实战路径。文章重点介绍了使用浏览器开发者工具定位数据、应对CSS混淆与参数加密等反爬策略,并提供了整合静态与动态抓取逻辑的Python代码示例,帮助开发者稳健地获取结构化车型信息。

wind6的博客 946

大家用了这么久的.net有没有发现C#中可以用中文变量呀!!可用这个特性来彻底改变程序的易读性(搞笑).但其实有一个非常独特的用处的

举个例子:using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace WindowsApplication1{ ///  /// Form1 的摘要说明。 ///  public class For

雪越艳阳的专栏 2112

基于改进Unet的多场景水果图像分割与分类研究

本文针对复杂场景下的水果图像分割问题,提出了一种改进Unet模型。通过构建包含8类水果、3种成熟度及多种干扰场景的数据集,采用ResNet50与MobileNetV3作为骨干网络,并在跳跃连接层集成CBAM注意力机制。实验表明,改进模型在测试集上达到91.6%的mIoU,推理速度提升23%,特别在重叠水果和低光照场景表现突出。研究为农业视觉检测提供了有效解决方案。

AI的内部世界的博客 310

用c# 实现设计器 DesignSurface<转>

DesignSurface 实现设计器问题?http://topic.csdn.net/u/20090419/02/4c0fe387-c019-4159-ac60-71c04495e2b2.html DesignSurface 实现设计器问题?http://topic.csdn.net/u/20090419/02/4c0fe387-c019-4159-a...

weixin_34037515的博客 1103

【四足机器人项目实战】四足机器人腿部机械结构设计

系列文章目录 提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加 TODO:写完再整理 文章目录系列文章目录前言 前言 认知有限,望大家多多包涵,有什么问题也希望能够与大家多交流,共同成长! 本文先对四足机器人–腿部机械结构设计做个简单的介绍,具体内容后续再更,其他模块可以参考去我其他文章 提示:以下是本篇文章正文内容 为了减轻腿部惯量,使用有限元分析着重进行轻量化设计,小腿采用工字梁方案,大腿采用内部镂方案,并使用1.5 mm的AL 6061硬质材料,轻量化的腿部设了腿部运动时摆动

qq_35635374的博客 7549

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

C# 原型设计器

关键字:表单设计器、流程设计器、程序发布         该原型设计器,主要用于应用系统开发前,用户界面和系统功能的模拟。提供了用户界面设计器和流程界面设计器,设计好的项目可发布成独立运行的程序。        响应:设计器提供了事件响应机制,控件之间通过事件和方法的绑定,实现相互调用。        扩展:控件采用注册方式,注册到程序中的控件即可在用户界面中使用。本版本提供的所有控件都

welliu的专栏 4182

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

C#界面编程5——标签控件、表格控件、自定义控件

标签控件 表格控件 添加一行数据 获取行数据 删除行数据,foreach是遍历的意思 单元格的编辑 添加编辑确认事件 判断第三列

qq_42832272的博客 759

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

如何用C#写所见即所得的设计器

摘要    本文讨论了如何使用C#编写一个所见即所得的设计器,分析了设计器的基本原理,可能遇到的技术问题,以及如何调用.NET框架来实现一个设计器。版权声明  本文是XDesigner软件工作室撰写,XDesigner软件工作室拥有本文版权,转载请注明出处,并保留本版权声明。前言         随着计算机信息系统不断深入发展,其系统结构要求越来越灵活,这种灵活性就是表现为程

lmhine的专栏 2174

C# ReportViewer报表 详解

C#报表控件ReportViewer rdlc 例(1)   启动VS2005新建一个窗体项目,命名为TestProj 在左边的窗体内选择“添加新数据源”或在菜单上操作“添加新数据源”: 选择后出现对话窗体,选择“数据库”,进入“下一步”: 本数据源以SqlServer2000所提供的Northwind数据库为例,因此在数据库内选择驱动程序为SqlServer,具体操作视所用...

weixin_34112181的博客 1978
上一篇: VC MFC 如何删除文件,目录,文件夹
下一篇: C#中使用Property Grid 控件 学习笔记
nic_911
博客等级 码龄22年 2粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值