【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 ...
随机推荐
- 数据结构与算法之递推算法 C++与PHP实现
数据结构是算法实现的基础,算法总是要依赖于某种数据结构来实现的.往往是在发展一种算法的时候,构建了适合于这样的算法的数据结构.一种数据结构假设脱离了算法,也就没有存在的价值了. 算法的作用----解决 ...
- github basic usage in windows
1. create a new accout, create orginazation, create repo 2. install git in your local pc Note: you c ...
- angularJS之使用过滤器转化输出 (angularJS系列最后一篇)
在视图模板中使用过滤器 过滤器也是一种服务,负责对输入的内容进行处理转换,以便更好地向用户显示. 过滤器可以在模板中的{{}}标记中使用: {{ expression | filter:arg1:ar ...
- PHP时间戳与时间相互转换(精确到毫秒)
原文:PHP时间戳与时间相互转换(精确到毫秒) /** 获取当前时间戳,精确到毫秒 */ function microtime_float(){ list($usec, $sec) = explo ...
- Linux常用命令汇总-速查
对Linux新手有用的20个命令 对中级Linux用户有用的20个命令 对Linux专家非常有用的20个命令 20个最受欢迎的Linux命令 20个有趣的Linux命令
- SQL_修改表结构
***********************************************声明*************************************************** ...
- (12) MVC5 EF6 Bootstrap3
MVC5 + EF6 + Bootstrap3 (12) 新建数据 系列教程:MVC5 + EF6 + Bootstrap3 上一节:MVC5 + EF6 + Bootstrap3 (11) 排序.搜 ...
- Object-C中Category类体验
Object-C中Category类体验 Object-C开发的时候有的时候会用到Category类,类似于Java和C#中扩展类,就是如果你觉得如果你觉得常用的方法在String中没有,可以根据业务 ...
- changePage() 页面跳转
jQuery.mobile.changePage( to [, options ] ) 从一个页面跳转到另一个页面,使用$.mobile对象的changePage方法来实现.但要使用此方式的时候,要以 ...
- Oracle中注意用户的访问权限
新增表.序列.存储过程等,要注意用户(例如System)的权限.如果在增删改查过程中出现数据库读写权限的报错,则在建表(或者序列.存储过程等)时,在脚本前面加 GRANT CREATE TABLE T ...