【NET】Winform用户控件的初步封装之编辑控件
编辑控件
public abstract partial class TEditorBase <TEntity, TRepository, TSqlStrConstruct> : UserControl
where TEntity:Yom.Extend.Entity.EntityBase
where TRepository : Yom.Extend.Repository.RepositoryBaseRepository<TEntity, TSqlStrConstruct>
where TSqlStrConstruct : Huawei.Data.SqlStrConstruct
{
protected TRepository repository = System.Activator.CreateInstance<TRepository>();
public TEditorBase()
{
InitializeComponent(); this.gpxTitle.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
}
string key;
public string Key
{
get {
return key;
}
set {
if (string.IsNullOrEmpty(value)) {
return;
}
key = value;
Entity = this.repository.FindBy(value) as Yom.Extend.Entity.EntityBase;
}
}
protected abstract Action<EntityDataBindEventArgs> AfterMapDataToEntityAction
{
get;
}
protected abstract Action<EntityDataBindEventArgs> AfterMapDataToControlAction
{
get;
}
protected virtual Yom.Extend.Entity.EntityBase Entity
{
get {
TEntity entity = System.Activator.CreateInstance<TEntity>();
entity.PropertyValueSet(this.repository.GetEntityKeyName(), this.Key);
DataDetailGetFromControlChildren.MapDataToEntity(entity, this, AfterMapDataToEntityAction);
return entity;
}
set {
if (value == null)
{
return;
} DataDetailGetFromControlChildren.MapDataToControl(value, this, this.AfterMapDataToControlAction);
}
} protected virtual string Title
{
get {
return string.Empty;
}
}
protected virtual bool IsValid(out string msg)
{
msg = string.Empty;
return true;
}
protected virtual void SaveCallBack()
{
string msg = string.Empty;
if (!IsValid(out msg))
{
MessageBox.Show(msg);
return;
}
try
{
if (string.IsNullOrEmpty(this.Key))
{
do
{
this.key = Guid.NewGuid().ToString();
} while (this.repository.Exists(this.key));
this.repository.Add(this.Entity);
}
else
{
this.repository.Update(this.Entity);
}
MessageBox.Show("保存成功!");
this.Hide();
this.Parent.Controls.Add(Lister);
this.Parent.Controls.Remove(this);
this.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(string.Format("保存失败:\r\n{0}", ex.Message));
}
} protected virtual void CancelCallBack()
{
//this.Hide();
//this.Parent.Controls.Add(GetListControl());
//this.Parent.Controls.Remove(this);
//this.Dispose();
//this = null;
if (this.Parent.Controls.Count <= )
{
this.Parent.Controls.Add(this.Lister);
this.Parent.Controls.RemoveAt();
}
else
{
this.Parent.Controls[].Show();
this.Parent.Controls.RemoveAt();
}
} private void bSave_Click(object sender, EventArgs e)
{
SaveCallBack();
} private void TEditorBase_Load(object sender, EventArgs e)
{
this.gpxTitle.Text = this.Title;
}
public abstract TListPager<TEntity, TRepository, TSqlStrConstruct> Lister
{
get;
} private void bCancel_Click(object sender, EventArgs e)
{
CancelCallBack();
}
}
属性赋值:
public class EntityDataBindEventArgs : EventArgs
{ public Yom.Extend.Entity.EntityBase CurrentEntity
{
get;
set;
}
public System.Reflection.PropertyInfo PropertyInfo
{
get;
set;
}
public System.Windows.Forms.Control BindControl
{
get;
set;
}
}
public class DataDetailGetFromControlChildren
{
public static void MapDataToEntity(Yom.Extend.Entity.EntityBase entity, System.Windows.Forms.Control parent, Action<EntityDataBindEventArgs> afterDataGet)
{ System.Reflection.PropertyInfo[] pis = entity.GetType().GetProperties();
if (pis != null && pis.Length > ) {
System.Windows.Forms.Control c;
foreach (System.Reflection.PropertyInfo pi in pis)
{
c = null;
try
{
c = parent.Controls.Find(pi.Name, true)[];
}
catch {
continue;
}
if (c != null)
{
entity.PropertyValueSet(pi.Name, c.Text);
if (afterDataGet != null)
{
afterDataGet(new EntityDataBindEventArgs() { CurrentEntity = entity, PropertyInfo = pi, BindControl = c });
}
}
}
}
}
public static void MapDataToControl(Yom.Extend.Entity.EntityBase entity, System.Windows.Forms.Control parent, Action<EntityDataBindEventArgs> afterDataSet)
{
System.Reflection.PropertyInfo[] pis = entity.GetType().GetProperties();
if (pis != null && pis.Length > )
{
System.Windows.Forms.Control c;
foreach (System.Reflection.PropertyInfo pi in pis)
{
c = null;
try
{
c = parent.Controls.Find(pi.Name, true)[];
}
catch
{
continue;
}
if (c != null)
{
if ((c is System.Windows.Forms.ComboBox) && !(c as System.Windows.Forms.ComboBox).Items.Contains(pi.GetValue(entity, null))) {
continue;
}
c.Text = pi.GetValue(entity, null).ToString();
if (afterDataSet != null)
{
afterDataSet(new EntityDataBindEventArgs() { CurrentEntity = entity, PropertyInfo = pi,BindControl=c });
}
}
}
}
}
}
此封装要结合之前发的那篇分页控件的文章
逻辑处理不是最终形态 仅仅是临时解决方案
【NET】Winform用户控件的初步封装之编辑控件的更多相关文章
- 【NET】Winform用户控件的初步封装之列表页控件
public abstract partial class TListPager<TEntity, TRepository, TSqlStrConstruct> : UserControl ...
- Qt 编程指南 4 单行编辑控件
从 Qt 设计师界面可以看到常用的 Qt 文本编辑和浏览控件,包括四个: 其中单行编辑控件 QLineEdit 和 普通文本编辑控件 QPlainTextEdit 都是针对最普通的 C++ 字符串编辑 ...
- DevExpress2011控件教程)编辑控件(comboBox,AspxCheckBox) 范例1
DevExpress2011控件教程)编辑控件(comboBox,AspxCheckBox) 范例1 AspxCheckBox 是一个检查编辑控件去展示特殊条件是否关闭或者打开.它一般会展示Yes/N ...
- 在Winform界面使用自定义用户控件及TabelPanel和StackPanel布局控件
在很多时候,我们做一些非常规化的界面的时候,往往需要创建一些用户控件,在其中绘制好一些基础的界面块,作为后续重复使用的一个单元,用户控件同时也可以封装处理一些简单的逻辑.在开发Winform各种类型项 ...
- winform 用户控件、 动态创建添加控件、timer控件、控件联动
用户控件: 相当于自定义的一个panel 里面可以放各种其他控件,并可以在后台一下调用整个此自定义控件. 使用方法:在项目上右键.添加.用户控件,之后用户控件的编辑与普通容器控件类似.如果要在后台往窗 ...
- WinForm用户控件、动态创建添加控件、timer控件--2016年12月12日
好文要顶 关注我 收藏该文 徐淳 关注 - 1 粉丝 - 3 0 0 用户控件: 通过布局将多个控件整合为一个控件,根据自己的需要进行修改,可对用户控件内的所有控件及控件属性进行修 ...
- 在Winform开发框架中使用DevExpress的TreeList和TreeListLookupEdit控件
DevExpress提供的树形列表控件TreeList和树形下拉列表控件TreeListLookupEdit都是非常强大的一个控件,它和我们传统Winform的TreeView控件使用上有所不同,我一 ...
- C# Winform 通过FlowLayoutPanel及自定义的编辑控件,实现快速构建C/S版的编辑表单页面
个人理解,开发应用程序的目的,不论是B/S或是C/S结构类型,无非就是实现可供用户进行查.增.改.删,其中查询用到最多,开发设计的场景也最为复杂,包括但不限于:表格记录查询.报表查询.导出文件查询等等 ...
- FileUpload控件使用初步
FileUpload控件使用初步 FileUpload控件使用初步: 1.实现文件上传 protected void btnSubmit_click(object sender, EventArg ...
随机推荐
- 兼容安卓的javaproject1.0
<pre class="java" name="code"> //兼容安卓的系统package cn.com.likeshow; import ja ...
- 2014年辛星jquery解读第二节
*************jquery的语法****************** 1.jquery是通过选取HTML元素,而且对选取的元素运行某些操作,从而完毕某些特效的. 2.因此,我们在使用jQu ...
- 内存排查 valgrind
内存问题排查工具 --- valgrind 1. 概述 2. Valgrind 3. 内存泄漏监测 3.1. 示例代码 3.2. 编译它 3.3. 用Valgrind监测进程的内存泄漏 4. 悬挂指针 ...
- 网页动态切换母版页(MasterPage)
原文:网页动态切换母版页(MasterPage) 是否可以变更网页的母版页(MasterPage)呢?某.aspx在创建时,已经附加入某一母版页(MasterPage)了,现需要.aspx动态变更母版 ...
- 系统预定义委托与Lambda表达式
NET中那些所谓的新语法之三:系统预定义委托与Lambda表达式 开篇:在上一篇中,我们了解了匿名类.匿名方法与扩展方法等所谓的新语法,这一篇我们继续征程,看看系统预定义委托(Action/Fun ...
- rpt水晶报表制作过程
原文:rpt水晶报表制作过程 最近公司安排一个以前的项目,里面需要用到水晶报表,由于原来做这个项目的同事离职,所在公司的同事报表做成了rdlc类型的,而这类报表在加载的时候很难动态的从数据库加载数据, ...
- 浅谈我对几个Web前端开发框架的比较
强调一下,这篇日志主要还是针对想学前端开发的新朋友写的,不是说我有什么独特见解,而是比较客观的状态,就各种框架的异同和应用场合,需要注意的地方做简单描述,不做具体深入分析,有的地方比较抽象,对于抽象之 ...
- jquery背景动画插件使用
在网页制作动画特效的时候,有时候想通过背景插入图片,然后通过控制背景显示的位置来实现一些动画效果,这样就不用使用绝对定位控制left和top来实现动画效果!但是jquery本身的动画函数是不支持背景动 ...
- 【MS SQL】数据库维护计划之数据库备份(二)
原文:[MS SQL]数据库维护计划之数据库备份(二) 上篇[MS SQL]数据库维护计划之数据库备份(一) 说了数据库备份的一些概念后,这篇以HRP_KQYY数据库备份为例,进行备份计划设置. 考虑 ...
- .Net中批量添加数据的几种实现方法比较
在.Net中经常会遇到批量添加数据,如将Excel中的数据导入数据库,直接在DataGridView控件中添加数据再保存到数据库等等. 方法一:一条一条循环添加 通常我们的第一反应是采用for或for ...