编辑控件

 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用户控件的初步封装之编辑控件的更多相关文章

  1. 【NET】Winform用户控件的初步封装之列表页控件

    public abstract partial class TListPager<TEntity, TRepository, TSqlStrConstruct> : UserControl ...

  2. Qt 编程指南 4 单行编辑控件

    从 Qt 设计师界面可以看到常用的 Qt 文本编辑和浏览控件,包括四个: 其中单行编辑控件 QLineEdit 和 普通文本编辑控件 QPlainTextEdit 都是针对最普通的 C++ 字符串编辑 ...

  3. DevExpress2011控件教程)编辑控件(comboBox,AspxCheckBox) 范例1

    DevExpress2011控件教程)编辑控件(comboBox,AspxCheckBox) 范例1 AspxCheckBox 是一个检查编辑控件去展示特殊条件是否关闭或者打开.它一般会展示Yes/N ...

  4. 在Winform界面使用自定义用户控件及TabelPanel和StackPanel布局控件

    在很多时候,我们做一些非常规化的界面的时候,往往需要创建一些用户控件,在其中绘制好一些基础的界面块,作为后续重复使用的一个单元,用户控件同时也可以封装处理一些简单的逻辑.在开发Winform各种类型项 ...

  5. winform 用户控件、 动态创建添加控件、timer控件、控件联动

    用户控件: 相当于自定义的一个panel 里面可以放各种其他控件,并可以在后台一下调用整个此自定义控件. 使用方法:在项目上右键.添加.用户控件,之后用户控件的编辑与普通容器控件类似.如果要在后台往窗 ...

  6. WinForm用户控件、动态创建添加控件、timer控件--2016年12月12日

    好文要顶 关注我 收藏该文 徐淳 关注 - 1 粉丝 - 3       0 0     用户控件: 通过布局将多个控件整合为一个控件,根据自己的需要进行修改,可对用户控件内的所有控件及控件属性进行修 ...

  7. 在Winform开发框架中使用DevExpress的TreeList和TreeListLookupEdit控件

    DevExpress提供的树形列表控件TreeList和树形下拉列表控件TreeListLookupEdit都是非常强大的一个控件,它和我们传统Winform的TreeView控件使用上有所不同,我一 ...

  8. C# Winform 通过FlowLayoutPanel及自定义的编辑控件,实现快速构建C/S版的编辑表单页面

    个人理解,开发应用程序的目的,不论是B/S或是C/S结构类型,无非就是实现可供用户进行查.增.改.删,其中查询用到最多,开发设计的场景也最为复杂,包括但不限于:表格记录查询.报表查询.导出文件查询等等 ...

  9. FileUpload控件使用初步

    FileUpload控件使用初步   FileUpload控件使用初步: 1.实现文件上传 protected void btnSubmit_click(object sender, EventArg ...

随机推荐

  1. ASP.NET MVC学习之控制器篇扩展性

    原文:ASP.NET MVC学习之控制器篇扩展性 一.前言 在之前的一篇随笔中已经讲述过控制器,而今天的随笔是作为之前的扩展. 二.正文 1.自定义动作方法 相信大家在开发过程一定会遇到动作方法的重名 ...

  2. Linq实现对XML的简单增删查改

    一.传统DOM创建XML方法 private static void CreateXmlDocWithDom() { XmlDocument doc =new XmlDocument(); XmlEl ...

  3. 使用javascript实现的一些功能

    原文:使用javascript实现的一些功能 今天学习了javascript中的事件,已经接近尾声,可以说明天跨入jquery的学习啦,学习了一周的javascript,感觉还没有掌握其中学习的微妙之 ...

  4. windows7股票的,win8残疾人,安装Han澳大利亚sinoxn个时间,sinox它支持大多数windows软体

    腾讯科技: 正如先前所宣布,微软,10一个月31迄今,Windows 7家庭基础版.家庭高级版和旗舰版盒装版本将不再销售.而微软是不会再OEM制造商授予许可数量的三个版本. windows7股票的.由 ...

  5. 採用Hexo 搭建Team Blog

    採用Hexo 搭建Team Blog 首先你要会使用Git 已经GitHub,然后让我们了解下什么是静态Blog 静态博客是指 不须要数据库驱动,拥有丰富模板,通过模板标记语言生成简单html css ...

  6. curl 简单使用

    cURL -- Command Line URL viewer -u username:password 以 Basic 方式发送用户名和密码 -d 以 POST 方式发送数据 -X 支持其它动词, ...

  7. iOS 开发者必不可少的 75 个工具

    如果你去到一位熟练的木匠的工作室,你总是能发现他/她有一堆工具来完成不同的任务. 软件开发同样如此.你可以从软件开发者如何使用工具中看出他水准如何.有经验的开发者精于使用工具.对你目前所使用的工具不断 ...

  8. CSS下背景属性background的使用方法

    背景颜色(background-color) CSS可以用纯色来作为背景,也可以将背景设置为透明,background相当于xhtml中的bgcolor. 它的两个值: transparent(默认值 ...

  9. [C#][ASP.net] 透过WebBrowser 取得AJAX 后的网页

    原文[C#][ASP.net] 透过WebBrowser 取得AJAX 后的网页 今天 Shih-Min 问我说,假设网页一开始是AJAX 会载入一些资料,但是透过WebClient 去抓 抓到都是J ...

  10. Kafka集群在空载情况下Cpu消耗比较高的问题

    线上kafka与storm的空载情况下负载都比较高, kafka达到122%, storm平均负载达到, 20%,  当前是通过Ambari下管理kafka的, a. 先停止s5的kafka进程.b. ...