【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 ...
随机推荐
- Unity插件之NGUI学习(8)—— Table和NGUI尺寸转换为世界坐标系尺寸
依据 Unity插件之NGUI学习(2),创建一个UI Root,在UI Root下创建一个Texture作为背景图,并设置图片,在Wiget下调整大小:然后在UI Root下再创建一个Panel. ...
- uva 12003 Array Transformer (大规模阵列)
白皮书393页面. 乱搞了原始数组中.其实用另一种阵列块记录. 你不能改变原始数组. 请注意,与原来的阵列和阵列块的良好关系,稍微细心处理边境.这是不难. #include <cstdio> ...
- [使用]Git--命令行
如何利用终端命令将文件上传到github远程服务器 (1) git status 命令查看下状态. (2) git pull 更新代码,确保代码是库上最新代码,防止覆盖其他人的提交. (3) git ...
- 【.NET特供-第三季】ASP.NET MVC系列:MVC与三层图形对照
近期在开发小组在研究:BS项目中是利用'MVC框架'还是继续沿用'三层'的问题. 由于曾经的.NET项目大多数都是利用三层开发的,所以大多数人都可以对三层进行熟练地运用.而项目的開始我们也曾听说过MV ...
- Middleware开发入门
Middleware开发入门 上篇我们谈了Host和Server的建立,但Host和Server无法产出任何有实际意义的内容,真正的内容来自于加载于Server的Middleware,本篇我们就着重介 ...
- 基于webrtc技术session border controler (SBC)
由于原来的文章 http://blog.csdn.net/voipmaker 转载注明出处. 我建了一个通信学习 交流群. 45211986, 欢迎增加. WebRTC技术致力于在浏览器端实现实时音 ...
- SOA面向服务架构
SOA面向服务架构 风尘浪子 只要肯努力,梦想总有一天会实现 随笔分类 - SOA面向服务架构 结合领域驱动设计的SOA分布式软件架构 摘要: 领域驱动设计DDD的总体结构,Repository层使用 ...
- Linux C语言操作MySQL
原文:Linux C语言操作MySQL 1.MySQL数据库简介 MySQL是一个开源码的小型关系数据库管理系统,体积小,速度快,总体成本低,开源.MySQL有以下特性: (1) 使用C和C++编写, ...
- selenium2入门 用selenium安装、加载、启用插件(一)
一:下载 下载地址是:http://docs.seleniumhq.org/download/
- Java 异常归纳总结
1.异常的分类 1) Checked exception: 这类异常都是Exception的子类 .异常的向上抛出机制进行处理,如果子类可能产生A异常,那么在父类中也必须throws A异常.可能导致 ...