关键字:Grove 、Reflection、UI
在编写应用程序时,我们都需要收集一些必备的数据。比如我们举一个常见的例子,员工和客户在许多应用程序中我们都会遇到(如图1)。每次遇上我们不外是先画一个窗体,然后在窗体上放置不同的控件用于收集信息。
图1
对于上面的这些信息收集,其操作过程完全是一样的。保存时,先对界面控件的值进行判断是否有效,后一一收集,然后保存。新增时,先判断单据是否修改过,如修改,则提醒用户是否保存。最后将界面控件的值设置为初始值。显示一条记录时,我们做的是从数据库中取出一条记录(有可能是实体对象),然后将记录中的数据一一设置到对应的控件上。
这就是我们平常的自以为的”高新技术”,经过N+1次机械的“编程”后,我已经厌恶了这种到处充斥着缰化和腐朽思想的代码。我思考着一种能解决所有界面录入问题的方式(复杂界面暂不考察)。我们已知道这些不同信息收集界面的处理方式都是相同的,唯一不同的只是存放在数据库不同的表中,以及表的字段及数据类型等不同。OK,既然处理方式相同,那么对于实现一种通用处理方式是可能的。下面请花点时间来整理一下我们的思路。
所有的处理方式相同,意味着我们可以定义一个公共的数据接口,所有需要收集信息的表,通常我们都会做ORM。如果这一步做了,那么请等一下,请让数据实体类实现我们定义的接口吧。
using System;
namespace JadeSoft.Common
{
/// <summary>
/// 定义实体类接口,系统中所有的实体类都应符合此标准
/// </summary>
public interface IEntity
{
System.Guid OID
{
get;
set;
}
/// <summary>
/// 该属性标识该数据实体对象当前状态。
/// </summary>
EntityStatus IsPersist{get; set;}
/// <summary>
/// 该方法提供一个验证该实体对象内的数据是否有效的途径。每一个数据实体类都需要实现该方法。但是在该方法中只进行对自身数据的有效性检查。
/// 涉及到多个实例的有效性验证建议不归在此方法中,应放在相应的控制类中进行。
/// </summary>
/// <returns>校验结果是否有效</returns>
bool Validate();
}
/// <summary>
/// 标示一个数据实体的当前状态
/// </summary>
public enum EntityStatus
{
/// <summary>
/// 表明不存在于数据库表中
/// </summary>
New=0,
/// <summary>
/// 表明是来自于数据库表中
/// </summary>
FromDB=1,
/// <summary>
/// 被标记为需要UPdate到数据库中
/// </summary>
Update=2,
/// <summary>
/// 被标记为需要从数据库中Delete
/// </summary>
Delete=3,
/// <summary>
/// 表明和数据库中相比是没有变化的
/// </summary>
Persisted=4,
/// <summary>
/// 从数据库中删除后,只存在内存中
/// </summary>
Null=5
}
}
在IEntity此接口中我们定义了OID此字段,毫无疑问,这意味着所有的数据库表中的主键都设置为OID,还是uniqueidentifier型的.下面来看一下我定义的实体类(//此处使用了Grove组件。它可以帮我动态解决数据持久问题.如有不明请http://grove.91link.com下载并运行,这里不多作解释)
namespace JadeSoft.Logistics.SystemInfo
{
using System;
using Grove.ORM; using JadeSoft.Common;
[DataTable("sDepartment")]
public class Department:IEntity
{
Guid _OID;
String _DeptID="";
String _DeptName="";
String _Phone="";
Guid _ParentID=Guid.Empty;
Guid _Creator=Permisser.CurrentUserGuid;
DateTime _CreatedTime=DateTime.Now;
Guid _Modifier=Permisser.CurrentUserGuid;
DateTime _ModifiedTime=DateTime.Now;
Boolean _Deleted=false;
Guid _DeptPerson=Guid.Empty;
[KeyField("OID",KeyType=UniqueIDType.OtherDefinition)]
public Guid OID
{
get{return this._OID;}
set
{
this._OID=value;
//数据更改,需要保存到数据库中
}
}
[EntityUIControl("txtDeptCode")]
[DataField("DeptID",Size=30)]
public String DeptID
{
get{
return this._DeptID;
}
set{
this._DeptID=value;
//数据更改,需要保存到数据库中
}
}
[EntityUIControl("txtDeptName")]
[DataField("DeptName",Size=50)]
public String DeptName
{
get{return this._DeptName;}
set{this._DeptName=value;
//数据更改,需要保存到数据库中
}
}
[EntityUIControl("txttel")]
[DataField("Phone",Size=50)]
public String Phone
{
get{return this._Phone;}
set{
this._Phone=value;
//数据更改,需要保存到数据库中
}
}
[EntityUIControl("cboParentDept","Value")]
[DataField("ParentID")]
public Guid ParentID
{
get{return this._ParentID;}
set
{
this._ParentID=value;
//数据更改,需要保存到数据库中
}
}
[EntityUIControl("cboDeptPerson","Value")]
[DataField("DeptPerson")]
public Guid DeptPerson
{
get{return this._DeptPerson;}
set
{
this._DeptPerson=value;
//数据更改,需要保存到数据库中
}
}
[EntityUIControl("txtCreator","Tag")]
[DataField("Creator")]
public Guid Creator
{
get{return this._Creator;}
set
{
this._Creator=value;
//数据更改,需要保存到数据库中
}
}
[EntityUIControl("dtCreateTime","DateTime")]
[DataField("CreatedTime")]
public DateTime CreatedTime
{
get{return this._CreatedTime;}
set
{
this._CreatedTime=value;
//数据更改,需要保存到数据库中
}
}
[EntityUIControl("txtEditor","Tag")]
[DataField("Modifier")]
public Guid Modifier
{
get{return this._Modifier;}
set
{
this._Modifier=value;
//数据更改,需要保存到数据库中
}
}
[EntityUIControl("dtEditTime","DateTime")]
[DataField("ModifiedTime")]
public DateTime ModifiedTime
{
get{return this._ModifiedTime;}
set
{
this._ModifiedTime=value;
//数据更改,需要保存到数据库中
}
}
[EntityUIControl("txtEmpCode")]
[DataField("Deleted")]
public Boolean Deleted
{
get{return this._Deleted;}
set
{
this._Deleted=value;
//数据更改,需要保存到数据库中
}
}
#region IEntity 成员
public bool Validate()
{
if(this._DeptID.Length==0)
{
return false;
}
if(this._DeptName.Length==0)
{
return false;
}
return true;
}
private EntityStatus _EntityStatus=EntityStatus.New;
public JadeSoft.Common.EntityStatus IsPersist
{
get
{
// TODO: 添加 Department.IsPersist getter 实现
return _EntityStatus;
}
set
{
_EntityStatus=value;
}
}
#endregion
}
}
几乎在数据实体类的每个属性 (Property) 上,我都加入了[EntityUIControl("txtDeptCode")],这样一句属性(Attribute)修饰。其实正是Grove的使用给我了一定的启发,利用Attribute来传递一些运行时数据。 EntityUIControl类派生自System.Attribute.主要功能是用来描述某个Property 在界面上对应哪个控件的哪个属性(显示或收集数据时用)。我在定义EntityUIControl允许用户设置对应界面控件的ID及数据绑定属性,默认为Text属性,如用户特殊要求,可自行再设置([EntityUIControl("dtEditTime","DateTime")])
好,接下来我们看一下EntityUIControl是如何实现的。还是来用代码来说话吧。
namespace JadeSoft.Common
{
/// <summary>
/// 用于标记实体对象的属性对应于界面哪个控件
/// </summary>
[AttributeUsageAttribute(AttributeTargets.Property)]
public class EntityUIControl:System.Attribute
{
string cID;//控件ID
public string ControlID
{
get{return cID;}
set{cID=value;}
}
//默认为TEXT属性
string _ControlPropery="Text";
/// <summary>
/// 要将实体的值显示到控件的哪个属性
/// </summary>
public string DisplayPropery
{
get
{
return _ControlPropery;
}
set
{
_ControlPropery=value;
}
}
/// <summary>
/// 界面控件的类型及实体对象的属性的对应关系处理
/// </summary>
/// <param name="ControlID">控件ID</param>
public EntityUIControl(string ControlID)
{
cID=ControlID;
}
public EntityUIControl(string ControlID,string ControlPropery)
{
cID=ControlID;
_ControlPropery=ControlPropery;
}
}
}
既然前提都准备好了,那么我考虑这样一种方案。我定义一个基窗体类(FrmAddBase)。此类将负责所有信息的数据保存,及数据显示等业务,因为我们前面讲过这些业务规则的处理都是一样的。那么数据验证呢,好象每个数据收集窗体的验证规则都不一样,那么好,只能让派生类自己处理了。基类如下图,只有一个工具栏,上面有保存,新增这二个功能(示例用),我们上面看到的图1,其实都是派生自此类
有很多个数据收集窗体的业务处理都在FrmAddBase里实现。这可能吗?请不要怀疑,很早以前我也是怀疑这个问题,因此我没有去尝试去解决。其实只要你愿去尝试一些看起来不可能解决的问题,那么问题的解决其实只差一步之遥了。在看我们剩下来的一步时我要申明一点是对于运行效率非常讲究的程序员我不建议阅读! 因为下面的思路是建立在灵活换取效率的基础上的。
如何解决不同的自定义实体类的保存及界面显示,看似一个很复杂的问题。那么多不同的数据实体的属性,如何区分,如何映射到数据库,及如何动态生成SQL语句。为什么我们不能使用反射来解决这个问题,利用System.Reflection我们可以实现对元数据的访问,同样我们也可以使用它来动态的创建对象。我们完全可以在FrmAddBase中对所引用的数据实体进行反射,查看数据实体对象的元数据,从而解决将数据实体的属性值显示在界面上或将界面的值保存到数据实体对象中去,那样的话,以前那种 Emp.EmpName=txtEmp.Text;或txtEmp.Text=Emp.EmpName这种腐朽的代码可以从我们的程序中一扫而光了。
using System;
using System.Windows.Forms;
using System.Reflection;
namespace JadeSoft.Common
{
/// <summary>
/// EntityController 的摘要说明。
/// </summary>
public class EntityController
{
IEntityController _IEntityController;
IEntity _entity;
Form _form;
Type _EntityType; //要控件的实体的类型描述
/// <summary>
/// 对符合IEntity的实体进行通用控制,如新增,保存。修改等功能
/// </summary>
/// <param name="entity">要控制的实体</param>
/// <param name="Container">窗体容器</param>
public EntityController(ref IEntity Entity,Form Container,IEntityController IEntityCtl )
{
_entity=Entity;
_form=Container;
_IEntityController=IEntityCtl;
}
/// <summary>
/// 对符合IEntity的实体进行通用控制,如新增,保存。修改等功能,使用默认数据访问器CommonDataAccess来更新数据
/// </summary>
/// <param name="entity">要控制的实体</param>
/// <param name="Container">窗体容器</param>
public EntityController(Form Container )
{
_form=Container;
_IEntityController=new CommonDataAccess();
}
/// <summary>
/// 将实体显示在界面上
/// </summary>
public void BindUI(IEntity Entity)
{
if(Entity==null)
{
throw new NullReferenceException("实体对象不能为空");
}
_entity=Entity;
_EntityType=_entity.GetType();
//获取实体对象的所有属性
System.Reflection.PropertyInfo[] ps=_EntityType.GetProperties();
foreach(System.Reflection.PropertyInfo p in ps)
{
EntityUIControl[] atts=(EntityUIControl[])p.GetCustomAttributes(typeof(EntityUIControl),true);
if(atts.Length==0)
continue;
//获取窗体对应的控件
Control c=GetFormControl(atts[0].ControlID);
if(c==null)
continue;//如果在窗体上没有找到对应的控件,暂时先忽略
//获取实体对象的某个属性值
object v=GetEntityValue(p.Name);
this.SetControlValue(c,atts[0].DisplayPropery,v);
}
}
/// <summary>
/// 判断实体对象状态是否发生改变
/// </summary>
/// <returns></returns>
public bool EntityStatusChanged()
{
if(_entity==null)
{
throw new NullReferenceException("实体对象不能为空");
}
//比较控件的值与对象的属性的值有没有改变
_EntityType=_entity.GetType();
//获取实体对象的所有属性
System.Reflection.PropertyInfo[] ps=_EntityType.GetProperties();
foreach(System.Reflection.PropertyInfo p in ps)
{
EntityUIControl[] atts=(EntityUIControl[])p.GetCustomAttributes(typeof(EntityUIControl),true);
if(atts.Length==0)
continue;
//获取窗体对应的控件
Control c=GetFormControl(atts[0].ControlID);
if(c==null)
continue;//如果在窗体上没有找到对应的控件,暂时先忽略
//获取实体对象的某个属性值
object EntityValue=GetEntityValue(p.Name);
object ControlValue=GetControlValue(c,atts[0].DisplayPropery);
//比较二个对象中的数据是否一致。
if(EntityValue.ToString()!=ControlValue.ToString())
{
return true;
}
}
return false;
}
/// <summary>
/// 对实体进行保存处理,有新增保存,更新保存,及删除三种操作,但接口一致
/// </summary>
public void UpdateEntity()
{
if(_entity==null)
{
throw new NullReferenceException("实体对象不能为空");
}
//收集实体信息
System.Reflection.PropertyInfo[] ps=_EntityType.GetProperties();
foreach(System.Reflection.PropertyInfo p in ps)
{
EntityUIControl[] atts=(EntityUIControl[])p.GetCustomAttributes(typeof(EntityUIControl),true);
if(atts.Length==0)
continue;
//获取窗体对应的控件
Control c=GetFormControl(atts[0].ControlID);
if(c==null)
continue;//如果在窗体上没有找到对应的控件,暂时先忽略
//获取实体对象的属性所对应的控件的属性的值
object v=new object();
v=GetControlValue(c,atts[0].DisplayPropery);
this.SetEntityValue(p.Name,v);
}
_IEntityController.UpdateObject(_entity);
}
/// <summary>
/// 获取窗体中的某个控件的引用
/// </summary>
/// <param name="ControlID">控件ID</param>
/// <returns>控件</returns>
private Control GetFormControl(string ControlID)
{
try
{
Type type=_form.GetType();
FieldInfo fi= type.GetField(ControlID,BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.Public);
return (Control)fi.GetValue(_form);
}
catch
{
return null;
}
}
/// <summary>
/// 获取实体对象的属性值
/// </summary>
/// <param name="propertyName">属性名</param>
/// <returns>属性值</returns>
private object GetEntityValue( string propertyName)
{
PropertyInfo pi=_EntityType.GetProperty(propertyName);
return pi.GetValue(this._entity,null);
}
/// <summary>
/// 使用反射来设置实体的属性值=控件的值
/// </summary>
private void SetEntityValue(string PropertyName ,object EntityValue)
{
PropertyInfo pi=_EntityType.GetProperty(PropertyName);
pi.SetValue(_entity,EntityValue,null);
}
/// <summary>
/// 获取控件的某个属性值
/// </summary>
/// <param name="UIControl"></param>
/// <param name="PropertyName"></param>
private object GetControlValue(Control UIControl,String PropertyName)
{
PropertyInfo pi=UIControl.GetType().GetProperty(PropertyName);
return pi.GetValue(UIControl,null);
}
/// <summary>
/// 使用反射来实现对控件的属性赋值
/// </summary>
/// <param name="UIControl">要处理的控件</param>
/// <param name="PropertyName">控件要赋值的属性</param>
/// <param name="ControlValue">值</param>
private void SetControlValue(Control UIControl,string PropertyName,object ControlValue)
{
try
{
//MessageBox.Show(UIControl.Name);
PropertyInfo pi=UIControl.GetType().GetProperty(PropertyName);
pi.SetValue(UIControl,ControlValue,null);
}
catch
{
}
}
}
}
EntityController 类,此类主要功能是在运行时,根据传入的对象,利用反射功能来将对象的值设置到界面元素,或将界面元素的值设置到实体对象。同时可以判断对象的值与界面元素是否一致等。从而统一解决了不同实体对象的录入设置问题。
大致问题都差不多,那么我们可以来看一下,界面FrmAddBase及其子类(不同数据收集窗体)是如何工作的。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using JadeSoft.Common;
using System.Reflection;
namespace JadeSoft.Logistics.SystemInfo.UI
{
/// <summary>
/// FrmDeptAdd 的摘要说明。
/// </summary>
public class FrmAddBase : System.Windows.Forms.Form
{
/// <summary>
/// 当用户点击工具栏成功处理为基类里的方法后,需要通知子类执行的事件
/// </summary>
public event AfterToolClick OnAfterToolClick;
internal Infragistics.Win.UltraWinToolbars.UltraToolbarsManager ultraToolbarsManager1;
private Infragistics.Win.UltraWinToolbars.UltraToolbarsDockArea _FrmDeptAdd_Toolbars_Dock_Area_Left;
private Infragistics.Win.UltraWinToolbars.UltraToolbarsDockArea _FrmDeptAdd_Toolbars_Dock_Area_Right;
private Infragistics.Win.UltraWinToolbars.UltraToolbarsDockArea _FrmDeptAdd_Toolbars_Dock_Area_Top;
private Infragistics.Win.UltraWinToolbars.UltraToolbarsDockArea _FrmDeptAdd_Toolbars_Dock_Area_Bottom;
private System.Windows.Forms.ImageList toolImgs;
internal Infragistics.Win.UltraWinStatusBar.UltraStatusBar ultraStatusBar1;
internal System.Windows.Forms.ErrorProvider errProvider;
private System.ComponentModel.IContainer components;
protected Type _EntityType;//要控件的实体类型描述
protected IEntity _Entity; //实体对象
protected EntityController _EntityController; //实体控制器
public FrmAddBase()
{
InitializeComponent();
}
/// <summary>
/// 更新一个对象时使用的构造器
/// </summary>
/// <param name="commonEntity"></param>
public FrmAddBase(IEntity commonEntity):this()
{
_Entity=commonEntity;
_EntityType=commonEntity.GetType();
}
/// <summary>
/// 新建一个对象时使用的构造器
/// </summary>
/// <param name="EntityType"></param>
public FrmAddBase(Type EntityType):this()
{
_EntityType=EntityType;
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
Infragistics.Win.UltraWinToolbars.UltraToolbar ultraToolbar1 = new Infragistics.Win.UltraWinToolbars.UltraToolbar("MainMenu");
Infragistics.Win.UltraWinToolbars.PopupMenuTool popupMenuTool1 = new Infragistics.Win.UltraWinToolbars.PopupMenuTool("mnuFile");
Infragistics.Win.UltraWinToolbars.UltraToolbar ultraToolbar2 = new Infragistics.Win.UltraWinToolbars.UltraToolbar("MainTool");
Infragistics.Win.UltraWinToolbars.ButtonTool buttonTool1 = new Infragistics.Win.UltraWinToolbars.ButtonTool("tblNew");
Infragistics.Win.UltraWinToolbars.ButtonTool buttonTool2 = new Infragistics.Win.UltraWinToolbars.ButtonTool("tblSave");
Infragistics.Win.UltraWinToolbars.ButtonTool buttonTool3 = new Infragistics.Win.UltraWinToolbars.ButtonTool("tlbExit");
Infragistics.Win.UltraWinToolbars.ButtonTool buttonTool4 = new Infragistics.Win.UltraWinToolbars.ButtonTool("tblNew");
Infragistics.Win.Appearance appearance1 = new Infragistics.Win.Appearance();
Infragistics.Win.UltraWinToolbars.ButtonTool buttonTool5 = new Infragistics.Win.UltraWinToolbars.ButtonTool("tlbExit");
Infragistics.Win.Appearance appearance2 = new Infragistics.Win.Appearance();
Infragistics.Win.UltraWinToolbars.PopupMenuTool popupMenuTool2 = new Infragistics.Win.UltraWinToolbars.PopupMenuTool("mnuFile");
Infragistics.Win.UltraWinToolbars.ButtonTool buttonTool6 = new Infragistics.Win.UltraWinToolbars.ButtonTool("tblNew");
Infragistics.Win.UltraWinToolbars.ButtonTool buttonTool7 = new Infragistics.Win.UltraWinToolbars.ButtonTool("tblSave");
Infragistics.Win.UltraWinToolbars.ButtonTool buttonTool8 = new Infragistics.Win.UltraWinToolbars.ButtonTool("tlbExit");
Infragistics.Win.UltraWinToolbars.ButtonTool buttonTool9 = new Infragistics.Win.UltraWinToolbars.ButtonTool("tblSave");
Infragistics.Win.Appearance appearance3 = new Infragistics.Win.Appearance();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FrmAddBase));
Infragistics.Win.UltraWinStatusBar.UltraStatusPanel ultraStatusPanel1 = new Infragistics.Win.UltraWinStatusBar.UltraStatusPanel();
Infragistics.Win.UltraWinStatusBar.UltraStatusPanel ultraStatusPanel2 = new Infragistics.Win.UltraWinStatusBar.UltraStatusPanel();
Infragistics.Win.UltraWinStatusBar.UltraStatusPanel ultraStatusPanel3 = new Infragistics.Win.UltraWinStatusBar.UltraStatusPanel();
this.ultraToolbarsManager1 = new Infragistics.Win.UltraWinToolbars.UltraToolbarsManager(this.components);
this.toolImgs = new System.Windows.Forms.ImageList(this.components);
this._FrmDeptAdd_Toolbars_Dock_Area_Left = new Infragistics.Win.UltraWinToolbars.UltraToolbarsDockArea();
this._FrmDeptAdd_Toolbars_Dock_Area_Right = new Infragistics.Win.UltraWinToolbars.UltraToolbarsDockArea();
this._FrmDeptAdd_Toolbars_Dock_Area_Top = new Infragistics.Win.UltraWinToolbars.UltraToolbarsDockArea();
this._FrmDeptAdd_Toolbars_Dock_Area_Bottom = new Infragistics.Win.UltraWinToolbars.UltraToolbarsDockArea();
this.ultraStatusBar1 = new Infragistics.Win.UltraWinStatusBar.UltraStatusBar();
this.errProvider = new System.Windows.Forms.ErrorProvider();
((System.ComponentModel.ISupportInitialize)(this.ultraToolbarsManager1)).BeginInit();
this.SuspendLayout();
//
// ultraToolbarsManager1
//
this.ultraToolbarsManager1.DesignerFlags = 0;
this.ultraToolbarsManager1.DockWithinContainer = this;
this.ultraToolbarsManager1.ImageListSmall = this.toolImgs;
this.ultraToolbarsManager1.ShowFullMenusDelay = 500;
this.ultraToolbarsManager1.Style = Infragistics.Win.UltraWinToolbars.ToolbarStyle.VisualStudio2005;
ultraToolbar1.DockedColumn = 0;
ultraToolbar1.DockedRow = 0;
ultraToolbar1.IsMainMenuBar = true;
ultraToolbar1.Text = "MainMenu";
ultraToolbar1.Tools.AddRange(new Infragistics.Win.UltraWinToolbars.ToolBase[] {
popupMenuTool1});
ultraToolbar2.DockedColumn = 0;
ultraToolbar2.DockedRow = 1;
ultraToolbar2.Settings.AllowCustomize = Infragistics.Win.DefaultableBoolean.False;
ultraToolbar2.Settings.FillEntireRow = Infragistics.Win.DefaultableBoolean.True;
ultraToolbar2.Text = "MainTool";
buttonTool3.InstanceProps.IsFirstInGroup = true;
ultraToolbar2.Tools.AddRange(new Infragistics.Win.UltraWinToolbars.ToolBase[] {
buttonTool1,
buttonTool2,
buttonTool3});
this.ultraToolbarsManager1.Toolbars.AddRange(new Infragistics.Win.UltraWinToolbars.UltraToolbar[] {
ultraToolbar1,
ultraToolbar2});
appearance1.Image = 0;
buttonTool4.SharedProps.AppearancesSmall.Appearance = appearance1;
buttonTool4.SharedProps.Caption = "新增";
appearance2.Image = 6;
buttonTool5.SharedProps.AppearancesSmall.Appearance = appearance2;
buttonTool5.SharedProps.Caption = "退出";
popupMenuTool2.SharedProps.Caption = "文件";
buttonTool8.InstanceProps.IsFirstInGroup = true;
popupMenuTool2.Tools.AddRange(new Infragistics.Win.UltraWinToolbars.ToolBase[] {
buttonTool6,
buttonTool7,
buttonTool8});
appearance3.Image = 12;
buttonTool9.SharedProps.AppearancesSmall.Appearance = appearance3;
buttonTool9.SharedProps.Caption = "保存";
this.ultraToolbarsManager1.Tools.AddRange(new Infragistics.Win.UltraWinToolbars.ToolBase[] {
buttonTool4,
buttonTool5,
popupMenuTool2,
buttonTool9});
this.ultraToolbarsManager1.ToolClick += new Infragistics.Win.UltraWinToolbars.ToolClickEventHandler(this.ultraToolbarsManager1_ToolClick);
//
// toolImgs
//
this.toolImgs.ImageSize = new System.Drawing.Size(16, 16);
this.toolImgs.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("toolImgs.ImageStream")));
this.toolImgs.TransparentColor = System.Drawing.Color.Transparent;
//
// _FrmDeptAdd_Toolbars_Dock_Area_Left
//
this._FrmDeptAdd_Toolbars_Dock_Area_Left.AccessibleRole = System.Windows.Forms.AccessibleRole.Grouping;
this._FrmDeptAdd_Toolbars_Dock_Area_Left.BackColor = System.Drawing.SystemColors.Control;
this._FrmDeptAdd_Toolbars_Dock_Area_Left.DockedPosition = Infragistics.Win.UltraWinToolbars.DockedPosition.Left;
this._FrmDeptAdd_Toolbars_Dock_Area_Left.ForeColor = System.Drawing.SystemColors.ControlText;
this._FrmDeptAdd_Toolbars_Dock_Area_Left.Location = new System.Drawing.Point(0, 50);
this._FrmDeptAdd_Toolbars_Dock_Area_Left.Name = "_FrmDeptAdd_Toolbars_Dock_Area_Left";
this._FrmDeptAdd_Toolbars_Dock_Area_Left.Size = new System.Drawing.Size(0, 284);
this._FrmDeptAdd_Toolbars_Dock_Area_Left.ToolbarsManager = this.ultraToolbarsManager1;
//
// _FrmDeptAdd_Toolbars_Dock_Area_Right
//
this._FrmDeptAdd_Toolbars_Dock_Area_Right.AccessibleRole = System.Windows.Forms.AccessibleRole.Grouping;
this._FrmDeptAdd_Toolbars_Dock_Area_Right.BackColor = System.Drawing.SystemColors.Control;
this._FrmDeptAdd_Toolbars_Dock_Area_Right.DockedPosition = Infragistics.Win.UltraWinToolbars.DockedPosition.Right;
this._FrmDeptAdd_Toolbars_Dock_Area_Right.ForeColor = System.Drawing.SystemColors.ControlText;
this._FrmDeptAdd_Toolbars_Dock_Area_Right.Location = new System.Drawing.Point(504, 50);
this._FrmDeptAdd_Toolbars_Dock_Area_Right.Name = "_FrmDeptAdd_Toolbars_Dock_Area_Right";
this._FrmDeptAdd_Toolbars_Dock_Area_Right.Size = new System.Drawing.Size(0, 284);
this._FrmDeptAdd_Toolbars_Dock_Area_Right.ToolbarsManager = this.ultraToolbarsManager1;
//
// _FrmDeptAdd_Toolbars_Dock_Area_Top
//
this._FrmDeptAdd_Toolbars_Dock_Area_Top.AccessibleRole = System.Windows.Forms.AccessibleRole.Grouping;
this._FrmDeptAdd_Toolbars_Dock_Area_Top.BackColor = System.Drawing.SystemColors.Control;
this._FrmDeptAdd_Toolbars_Dock_Area_Top.DockedPosition = Infragistics.Win.UltraWinToolbars.DockedPosition.Top;
this._FrmDeptAdd_Toolbars_Dock_Area_Top.ForeColor = System.Drawing.SystemColors.ControlText;
this._FrmDeptAdd_Toolbars_Dock_Area_Top.Location = new System.Drawing.Point(0, 0);
this._FrmDeptAdd_Toolbars_Dock_Area_Top.Name = "_FrmDeptAdd_Toolbars_Dock_Area_Top";
this._FrmDeptAdd_Toolbars_Dock_Area_Top.Size = new System.Drawing.Size(504, 50);
this._FrmDeptAdd_Toolbars_Dock_Area_Top.ToolbarsManager = this.ultraToolbarsManager1;
//
// _FrmDeptAdd_Toolbars_Dock_Area_Bottom
//
this._FrmDeptAdd_Toolbars_Dock_Area_Bottom.AccessibleRole = System.Windows.Forms.AccessibleRole.Grouping;
this._FrmDeptAdd_Toolbars_Dock_Area_Bottom.BackColor = System.Drawing.SystemColors.Control;
this._FrmDeptAdd_Toolbars_Dock_Area_Bottom.DockedPosition = Infragistics.Win.UltraWinToolbars.DockedPosition.Bottom;
this._FrmDeptAdd_Toolbars_Dock_Area_Bottom.ForeColor = System.Drawing.SystemColors.ControlText;
this._FrmDeptAdd_Toolbars_Dock_Area_Bottom.Location = new System.Drawing.Point(0, 334);
this._FrmDeptAdd_Toolbars_Dock_Area_Bottom.Name = "_FrmDeptAdd_Toolbars_Dock_Area_Bottom";
this._FrmDeptAdd_Toolbars_Dock_Area_Bottom.Size = new System.Drawing.Size(504, 0);
this._FrmDeptAdd_Toolbars_Dock_Area_Bottom.ToolbarsManager = this.ultraToolbarsManager1;
//
// ultraStatusBar1
//
this.ultraStatusBar1.Location = new System.Drawing.Point(0, 334);
this.ultraStatusBar1.Name = "ultraStatusBar1";
ultraStatusPanel1.Key = "BillState";
ultraStatusPanel1.Text = "单据状态:新增";
ultraStatusPanel1.Width = 120;
ultraStatusPanel2.Key = "SaveState";
ultraStatusPanel2.SizingMode = Infragistics.Win.UltraWinStatusBar.PanelSizingMode.Spring;
ultraStatusPanel3.DateTimeFormat = "hh:mm:ss";
ultraStatusPanel3.Style = Infragistics.Win.UltraWinStatusBar.PanelStyle.Date;
ultraStatusPanel3.Width = 70;
this.ultraStatusBar1.Panels.AddRange(new Infragistics.Win.UltraWinStatusBar.UltraStatusPanel[] {
ultraStatusPanel1,
ultraStatusPanel2,
ultraStatusPanel3});
this.ultraStatusBar1.Size = new System.Drawing.Size(504, 23);
this.ultraStatusBar1.TabIndex = 4;
this.ultraStatusBar1.ViewStyle = Infragistics.Win.UltraWinStatusBar.ViewStyle.VisualStudio2005;
//
// errProvider
//
this.errProvider.ContainerControl = this;
//
// FrmAddBase
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.ControlLightLight;
this.ClientSize = new System.Drawing.Size(504, 357);
this.Controls.Add(this._FrmDeptAdd_Toolbars_Dock_Area_Left);
this.Controls.Add(this._FrmDeptAdd_Toolbars_Dock_Area_Right);
this.Controls.Add(this._FrmDeptAdd_Toolbars_Dock_Area_Top);
this.Controls.Add(this._FrmDeptAdd_Toolbars_Dock_Area_Bottom);
this.Controls.Add(this.ultraStatusBar1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Name = "FrmAddBase";
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "新增资料模板窗体";
this.Load += new System.EventHandler(this.FrmAddBase_Load);
((System.ComponentModel.ISupportInitialize)(this.ultraToolbarsManager1)).EndInit();
this.ResumeLayout(false);
}
#endregion
#region 虚函数,派生类自己可以实现特定规则
/// <summary>
/// 检查界面控件数据是否符合要求,由派生类自己实现
/// </summary>
protected virtual bool CheckData()
{
return true;
}
/// <summary>
/// 在派生类中设置界面元素的值,因为有时在派生类界面保存实体对象时,还想修改一下界面上的值的可能性,同派生类自己实现
/// </summary>
protected virtual void SetUI()
{
}
#endregion
/// <summary>
/// 设错误提示
/// </summary>
/// <param name="control">要提示的控件</param>
/// <param name="ErrMsg">提示信息</param>
protected void SetErrInfo(Control control,string ErrMsg)
{
errProvider.SetError(control,ErrMsg);
this.ultraStatusBar1.Panels["SaveState"].Text=ErrMsg;
if(ErrMsg.Length!=0)
{
control.Focus();
}
}
private void CancelErrorIcon()
{
foreach(Control c in this.Controls)
{
errProvider.SetError(c,"");
}
}
/// <summary>
/// 设置当前单据操作状态
/// </summary>
/// <param name="pBillOpt"></param>
protected void SetBillStatus(BillOperator pBillOpt)
{
switch(pBillOpt)
{
case BillOperator.New:
this.ultraStatusBar1.Panels["BillState"].Text="单据状态:新增";
break;
case BillOperator.Update:
this.ultraStatusBar1.Panels["BillState"].Text="单据状态:修改";
break;
case BillOperator.Saved:
this.ultraStatusBar1.Panels["BillState"].Text="单据状态:已保存";
break;
case BillOperator.Edit:
this.ultraStatusBar1.Panels["BillState"].Text="单据状态:编辑中";
break;
}
}
private void FrmAddBase_Load(object sender, System.EventArgs e)
{
this.Closing+=new CancelEventHandler(FrmAddBase_Closing);
}
//此方法在子类需要手工调用一下。如数据实体类的属性值是某个集合中的一员(下拉列表框等选择而来)则子类窗体需先填充好数据,再调用此方法
public void DisplayUI()
{
//窗体加载时,先实例化实体控制器
if(_Entity==null) //如果实体为空,则使用反射动态新建一个实体对象
{
CreateIEntity();
SetBillStatus(BillOperator.New);
}
else
{
SetBillStatus(BillOperator.Update);
}
_EntityController=new EntityController(this);
_EntityController.BindUI( _Entity);
}
//将实体对象保存到数据库中
private bool SaveEntity()
{
if(CheckData())//如数据正确
{
SetUI();
if(_Entity.IsPersist!=JadeSoft.Common.EntityStatus.New)
{
_Entity.IsPersist=EntityStatus.Update;
}
_EntityController.UpdateEntity();
SetBillStatus(BillOperator.Saved);
MessageBox.Show("当前单据信息已成功保存",this.Text,MessageBoxButtons.OK,MessageBoxIcon.Information);
return true;
}
return false;
}
/// <summary>
/// 概据元数据动态创建一个对象
/// </summary>
private void CreateIEntity()
{
_Entity =(IEntity)Activator.CreateInstance(_EntityType);
_Entity.OID=System.Guid.NewGuid();//给对象分配一个新的ID
}
/// <summary>
/// 工具栏事件处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
{
try
{
switch (e.Tool.Key)
{
case "tblNew":
//如果用户界面控件的值发生改变
if(_EntityController.EntityStatusChanged())
{
DialogResult dr=MessageBox.Show("当前单据信息已改变!/r/n/r/n是否保存对当前单据的更改",this.Text,MessageBoxButtons.YesNoCancel,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button3);
if(dr==DialogResult.Cancel)
{//取消,单据不更改
return;
}
else if(dr==DialogResult.Yes)
{
//如成功保存数据
if(SaveEntity())
{
CreateIEntity();
_EntityController.BindUI(_Entity);
CancelErrorIcon();
}
}
else
{
CreateIEntity();
_EntityController.BindUI(_Entity);
CancelErrorIcon();
}
}
else
{
CreateIEntity();
_EntityController.BindUI(_Entity);
}
SetBillStatus(BillOperator.New);
if(OnAfterToolClick!=null)
OnAfterToolClick(sender,e);
break;
case "tlbExit": // ButtonTool
this.Close();
break;
case "tblSave": // 如果实体状态发生改变
if(_EntityController.EntityStatusChanged())
{
if(this.SaveEntity())
{
if(OnAfterToolClick!=null)
OnAfterToolClick(_Entity,e);
}
}
break;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,this.Text,MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
private void FrmAddBase_Closing(object sender, CancelEventArgs e)
{
if(_EntityController.EntityStatusChanged())
{
DialogResult dr=MessageBox.Show("当前单据信息已改变!/r/n/r/n是否保存对当前单据的更改",this.Text,MessageBoxButtons.YesNoCancel,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button3);
if(dr==DialogResult.Cancel)
{//取消,单据不更改
e.Cancel=true;
return;
}
else if(dr==DialogResult.Yes)
{
//如成功保存数据
if(!SaveEntity())
{
e.Cancel=true;
}
}
}
}
}
public delegate void AfterToolClick(object sender ,Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e);
/// <summary>
/// 单据的操作状态
/// </summary>
public enum BillOperator
{
New=0,//新增
Update=1,//更新
Saved=2, //已保存
Edit=3 //处于编辑状态
}
}
子类窗体的调用方式如下。
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using JadeSoft.Logistics.SystemInfo.BusinessFacades;
using JadeSoft.Common;
namespace JadeSoft.Logistics.SystemInfo.UI
{
public class FrmDeptAdd : JadeSoft.Logistics.SystemInfo.UI.FrmAddBase
{
#region 窗体变量
private Infragistics.Win.Misc.UltraLabel ultraLabel1;
private Infragistics.Win.Misc.UltraLabel ultraLabel2;
private Infragistics.Win.Misc.UltraLabel ultraLabel3;
private Infragistics.Win.Misc.UltraLabel ultraLabel4;
private Infragistics.Win.Misc.UltraLabel ultraLabel5;
private Infragistics.Win.Misc.UltraLabel ultraLabel6;
private Infragistics.Win.Misc.UltraLabel ultraLabel7;
private Infragistics.Win.Misc.UltraLabel ultraLabel8;
private Infragistics.Win.Misc.UltraLabel ultraLabel9;
private Infragistics.Win.UltraWinEditors.UltraTextEditor txtDeptCode;
private Infragistics.Win.UltraWinEditors.UltraTextEditor txtDeptName;
private Infragistics.Win.UltraWinEditors.UltraTextEditor txttel;
private Infragistics.Win.UltraWinEditors.UltraTextEditor txtCreator;
private Infragistics.Win.UltraWinEditors.UltraTextEditor txtEditor;
private Infragistics.Win.UltraWinGrid.UltraCombo cboParentDept;
private Infragistics.Win.UltraWinGrid.UltraCombo cboDeptPerson;
private System.ComponentModel.IContainer components = null;
private Infragistics.Win.UltraWinEditors.UltraDateTimeEditor dtCreateTime;
private Infragistics.Win.UltraWinEditors.UltraDateTimeEditor dtEditTime;
FrmDeptDts frmOwner=null; //列表窗体指针,当此窗体新增时,要刷新列表窗体
#endregion
#region 构造器
public FrmDeptAdd(Department deptEntity ,FrmDeptDts ownerForm):base(deptEntity)
{
// 该调用是 Windows 窗体设计器所必需的。
InitializeComponent();
frmOwner=ownerForm;
}
public FrmDeptAdd(FrmDeptDts ownerForm):base(typeof(Department))
{
// 该调用是 Windows 窗体设计器所必需的。
InitializeComponent();
frmOwner=ownerForm;
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#endregion
#region 设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
Infragistics.Win.Appearance appearance1 = new Infragistics.Win.Appearance();
Infragistics.Win.Appearance appearance2 = new Infragistics.Win.Appearance();
Infragistics.Win.Appearance appearance3 = new Infragistics.Win.Appearance();
Infragistics.Win.Appearance appearance4 = new Infragistics.Win.Appearance();
Infragistics.Win.Appearance appearance5 = new Infragistics.Win.Appearance();
Infragistics.Win.Appearance appearance6 = new Infragistics.Win.Appearance();
Infragistics.Win.Appearance appearance7 = new Infragistics.Win.Appearance();
Infragistics.Win.Appearance appearance8 = new Infragistics.Win.Appearance();
this.ultraLabel1 = new Infragistics.Win.Misc.UltraLabel();
this.ultraLabel2 = new Infragistics.Win.Misc.UltraLabel();
this.ultraLabel3 = new Infragistics.Win.Misc.UltraLabel();
this.ultraLabel4 = new Infragistics.Win.Misc.UltraLabel();
this.ultraLabel5 = new Infragistics.Win.Misc.UltraLabel();
this.ultraLabel6 = new Infragistics.Win.Misc.UltraLabel();
this.ultraLabel7 = new Infragistics.Win.Misc.UltraLabel();
this.ultraLabel8 = new Infragistics.Win.Misc.UltraLabel();
this.ultraLabel9 = new Infragistics.Win.Misc.UltraLabel();
this.txtDeptCode = new Infragistics.Win.UltraWinEditors.UltraTextEditor();
this.txtDeptName = new Infragistics.Win.UltraWinEditors.UltraTextEditor();
this.txttel = new Infragistics.Win.UltraWinEditors.UltraTextEditor();
this.txtCreator = new Infragistics.Win.UltraWinEditors.UltraTextEditor();
this.txtEditor = new Infragistics.Win.UltraWinEditors.UltraTextEditor();
this.cboParentDept = new Infragistics.Win.UltraWinGrid.UltraCombo();
this.cboDeptPerson = new Infragistics.Win.UltraWinGrid.UltraCombo();
this.dtCreateTime = new Infragistics.Win.UltraWinEditors.UltraDateTimeEditor();
this.dtEditTime = new Infragistics.Win.UltraWinEditors.UltraDateTimeEditor();
((System.ComponentModel.ISupportInitialize)(this.txtDeptCode)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.txtDeptName)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.txttel)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.txtCreator)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.txtEditor)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.cboParentDept)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.cboDeptPerson)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dtCreateTime)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dtEditTime)).BeginInit();
this.SuspendLayout();
//
// ultraLabel1
//
this.ultraLabel1.Location = new System.Drawing.Point(10, 64);
this.ultraLabel1.Name = "ultraLabel1";
this.ultraLabel1.Size = new System.Drawing.Size(72, 16);
this.ultraLabel1.TabIndex = 0;
this.ultraLabel1.Text = "部门编号: ";
//
// ultraLabel2
//
this.ultraLabel2.Location = new System.Drawing.Point(272, 64);
this.ultraLabel2.Name = "ultraLabel2";
this.ultraLabel2.Size = new System.Drawing.Size(72, 16);
this.ultraLabel2.TabIndex = 1;
this.ultraLabel2.Text = "部门名称:";
//
// ultraLabel3
//
this.ultraLabel3.Location = new System.Drawing.Point(272, 96);
this.ultraLabel3.Name = "ultraLabel3";
this.ultraLabel3.Size = new System.Drawing.Size(72, 16);
this.ultraLabel3.TabIndex = 2;
this.ultraLabel3.Text = "上级部门:";
//
// ultraLabel4
//
this.ultraLabel4.Location = new System.Drawing.Point(10, 96);
this.ultraLabel4.Name = "ultraLabel4";
this.ultraLabel4.Size = new System.Drawing.Size(72, 16);
this.ultraLabel4.TabIndex = 3;
this.ultraLabel4.Text = "电话:";
//
// ultraLabel5
//
this.ultraLabel5.Location = new System.Drawing.Point(10, 136);
this.ultraLabel5.Name = "ultraLabel5";
this.ultraLabel5.Size = new System.Drawing.Size(80, 16);
this.ultraLabel5.TabIndex = 4;
this.ultraLabel5.Text = "部门负责人:";
//
// ultraLabel6
//
this.ultraLabel6.Location = new System.Drawing.Point(10, 168);
this.ultraLabel6.Name = "ultraLabel6";
this.ultraLabel6.Size = new System.Drawing.Size(72, 16);
this.ultraLabel6.TabIndex = 5;
this.ultraLabel6.Text = "创建者:";
//
// ultraLabel7
//
this.ultraLabel7.Location = new System.Drawing.Point(272, 168);
this.ultraLabel7.Name = "ultraLabel7";
this.ultraLabel7.Size = new System.Drawing.Size(72, 16);
this.ultraLabel7.TabIndex = 6;
this.ultraLabel7.Text = "创建时间:";
//
// ultraLabel8
//
this.ultraLabel8.Location = new System.Drawing.Point(10, 200);
this.ultraLabel8.Name = "ultraLabel8";
this.ultraLabel8.Size = new System.Drawing.Size(72, 16);
this.ultraLabel8.TabIndex = 7;
this.ultraLabel8.Text = "修改者:";
//
// ultraLabel9
//
this.ultraLabel9.Location = new System.Drawing.Point(272, 200);
this.ultraLabel9.Name = "ultraLabel9";
this.ultraLabel9.Size = new System.Drawing.Size(72, 16);
this.ultraLabel9.TabIndex = 8;
this.ultraLabel9.Text = "修改时间:";
//
// txtDeptCode
//
appearance1.BorderColor = System.Drawing.Color.Black;
this.txtDeptCode.Appearance = appearance1;
this.txtDeptCode.BorderStyle = Infragistics.Win.UIElementBorderStyle.Solid;
this.txtDeptCode.Location = new System.Drawing.Point(83, 64);
this.txtDeptCode.Name = "txtDeptCode";
this.txtDeptCode.Size = new System.Drawing.Size(168, 19);
this.txtDeptCode.TabIndex = 1;
//
// txtDeptName
//
appearance2.BorderColor = System.Drawing.Color.Black;
this.txtDeptName.Appearance = appearance2;
this.txtDeptName.BorderStyle = Infragistics.Win.UIElementBorderStyle.Solid;
this.txtDeptName.Location = new System.Drawing.Point(336, 64);
this.txtDeptName.Name = "txtDeptName";
this.txtDeptName.Size = new System.Drawing.Size(168, 19);
this.txtDeptName.TabIndex = 2;
//
// txttel
//
appearance3.BorderColor = System.Drawing.Color.Black;
this.txttel.Appearance = appearance3;
this.txttel.BorderStyle = Infragistics.Win.UIElementBorderStyle.Solid;
this.txttel.Location = new System.Drawing.Point(83, 96);
this.txttel.Name = "txttel";
this.txttel.Size = new System.Drawing.Size(168, 19);
this.txttel.TabIndex = 3;
//
// txtCreator
//
appearance4.BackColor = System.Drawing.Color.White;
appearance4.BorderColor = System.Drawing.Color.Black;
this.txtCreator.Appearance = appearance4;
this.txtCreator.BorderStyle = Infragistics.Win.UIElementBorderStyle.Solid;
this.txtCreator.Location = new System.Drawing.Point(83, 160);
this.txtCreator.Name = "txtCreator";
this.txtCreator.ReadOnly = true;
this.txtCreator.Size = new System.Drawing.Size(168, 19);
this.txtCreator.TabIndex = 6;
//
// txtEditor
//
appearance5.BackColor = System.Drawing.Color.White;
appearance5.BorderColor = System.Drawing.Color.Black;
this.txtEditor.Appearance = appearance5;
this.txtEditor.BorderStyle = Infragistics.Win.UIElementBorderStyle.Solid;
this.txtEditor.Location = new System.Drawing.Point(83, 192);
this.txtEditor.Name = "txtEditor";
this.txtEditor.ReadOnly = true;
this.txtEditor.Size = new System.Drawing.Size(168, 19);
this.txtEditor.TabIndex = 8;
//
// cboParentDept
//
this.cboParentDept.BorderStyle = Infragistics.Win.UIElementBorderStyle.Solid;
this.cboParentDept.CharacterCasing = System.Windows.Forms.CharacterCasing.Normal;
this.cboParentDept.DisplayMember = "";
this.cboParentDept.DisplayStyle = Infragistics.Win.EmbeddableElementDisplayStyle.VisualStudio2005;
this.cboParentDept.DropDownStyle = Infragistics.Win.UltraWinGrid.UltraComboStyle.DropDownList;
this.cboParentDept.Location = new System.Drawing.Point(336, 96);
this.cboParentDept.Name = "cboParentDept";
this.cboParentDept.Size = new System.Drawing.Size(168, 19);
this.cboParentDept.TabIndex = 4;
this.cboParentDept.ValueMember = "";
this.cboParentDept.InitializeLayout += new Infragistics.Win.UltraWinGrid.InitializeLayoutEventHandler(this.cboParentDept_InitializeLayout);
//
// cboDeptPerson
//
appearance6.BorderColor = System.Drawing.Color.Black;
appearance6.BorderColor3DBase = System.Drawing.Color.Black;
this.cboDeptPerson.Appearance = appearance6;
this.cboDeptPerson.BorderStyle = Infragistics.Win.UIElementBorderStyle.Solid;
this.cboDeptPerson.CharacterCasing = System.Windows.Forms.CharacterCasing.Normal;
this.cboDeptPerson.DisplayMember = "";
this.cboDeptPerson.DisplayStyle = Infragistics.Win.EmbeddableElementDisplayStyle.VisualStudio2005;
this.cboDeptPerson.DropDownStyle = Infragistics.Win.UltraWinGrid.UltraComboStyle.DropDownList;
this.cboDeptPerson.Location = new System.Drawing.Point(83, 128);
this.cboDeptPerson.Name = "cboDeptPerson";
this.cboDeptPerson.Size = new System.Drawing.Size(168, 19);
this.cboDeptPerson.TabIndex = 5;
this.cboDeptPerson.ValueMember = "";
this.cboDeptPerson.InitializeLayout += new Infragistics.Win.UltraWinGrid.InitializeLayoutEventHandler(this.cboDeptPerson_InitializeLayout);
//
// dtCreateTime
//
appearance7.BackColor = System.Drawing.Color.White;
this.dtCreateTime.Appearance = appearance7;
this.dtCreateTime.BorderStyle = Infragistics.Win.UIElementBorderStyle.Solid;
this.dtCreateTime.DropDownButtonDisplayStyle = Infragistics.Win.ButtonDisplayStyle.Never;
this.dtCreateTime.FormatProvider = new System.Globalization.CultureInfo("zh-CN");
this.dtCreateTime.FormatString = "";
this.dtCreateTime.Location = new System.Drawing.Point(336, 161);
this.dtCreateTime.MaskInput = "yyyy-mm-dd hh:mm:ss";
this.dtCreateTime.Name = "dtCreateTime";
this.dtCreateTime.ReadOnly = true;
this.dtCreateTime.Size = new System.Drawing.Size(167, 19);
this.dtCreateTime.TabIndex = 50;
//
// dtEditTime
//
appearance8.BackColor = System.Drawing.Color.White;
this.dtEditTime.Appearance = appearance8;
this.dtEditTime.BorderStyle = Infragistics.Win.UIElementBorderStyle.Solid;
this.dtEditTime.DropDownButtonDisplayStyle = Infragistics.Win.ButtonDisplayStyle.Never;
this.dtEditTime.FormatString = "";
this.dtEditTime.Location = new System.Drawing.Point(336, 193);
this.dtEditTime.MaskInput = "yyyy-mm-dd hh:mm:ss";
this.dtEditTime.Name = "dtEditTime";
this.dtEditTime.ReadOnly = true;
this.dtEditTime.Size = new System.Drawing.Size(167, 19);
this.dtEditTime.TabIndex = 49;
//
// FrmDeptAdd
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(528, 261);
this.Controls.Add(this.dtCreateTime);
this.Controls.Add(this.dtEditTime);
this.Controls.Add(this.txtDeptCode);
this.Controls.Add(this.txtDeptName);
this.Controls.Add(this.txttel);
this.Controls.Add(this.txtCreator);
this.Controls.Add(this.txtEditor);
this.Controls.Add(this.cboDeptPerson);
this.Controls.Add(this.cboParentDept);
this.Controls.Add(this.ultraLabel1);
this.Controls.Add(this.ultraLabel2);
this.Controls.Add(this.ultraLabel3);
this.Controls.Add(this.ultraLabel4);
this.Controls.Add(this.ultraLabel5);
this.Controls.Add(this.ultraLabel6);
this.Controls.Add(this.ultraLabel7);
this.Controls.Add(this.ultraLabel8);
this.Controls.Add(this.ultraLabel9);
this.Name = "FrmDeptAdd";
this.Text = "部门资料管理体";
this.Load += new System.EventHandler(this.FrmDeptAdd_Load);
this.Controls.SetChildIndex(this.ultraLabel9, 0);
this.Controls.SetChildIndex(this.ultraLabel8, 0);
this.Controls.SetChildIndex(this.ultraLabel7, 0);
this.Controls.SetChildIndex(this.ultraLabel6, 0);
this.Controls.SetChildIndex(this.ultraLabel5, 0);
this.Controls.SetChildIndex(this.ultraLabel4, 0);
this.Controls.SetChildIndex(this.ultraLabel3, 0);
this.Controls.SetChildIndex(this.ultraLabel2, 0);
this.Controls.SetChildIndex(this.ultraLabel1, 0);
this.Controls.SetChildIndex(this.cboParentDept, 0);
this.Controls.SetChildIndex(this.cboDeptPerson, 0);
this.Controls.SetChildIndex(this.txtEditor, 0);
this.Controls.SetChildIndex(this.txtCreator, 0);
this.Controls.SetChildIndex(this.txttel, 0);
this.Controls.SetChildIndex(this.txtDeptName, 0);
this.Controls.SetChildIndex(this.txtDeptCode, 0);
this.Controls.SetChildIndex(this.dtEditTime, 0);
this.Controls.SetChildIndex(this.dtCreateTime, 0);
((System.ComponentModel.ISupportInitialize)(this.txtDeptCode)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.txtDeptName)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.txttel)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.txtCreator)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.txtEditor)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.cboParentDept)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.cboDeptPerson)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dtCreateTime)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dtEditTime)).EndInit();
this.ResumeLayout(false);
}
#endregion
#region 窗体加载事件
private void FrmDeptAdd_Load(object sender, System.EventArgs e)
{
InitDeptEmp();
InitDeptList();
DisplayUI();
txtCreator.Text=EmployeeManager.GetEmpNameByGuid((Guid)txtCreator.Tag);
txtEditor.Text=EmployeeManager.GetEmpNameByGuid((Guid)txtEditor.Tag);
base.OnAfterToolClick+=new AfterToolClick(FrmDeptAdd_OnAfterToolClick);
}
//覆写父类的检查数据有效动作。如果返回假,父类的操作将中断
protected override bool CheckData()
{
if(txtDeptCode.Text.Trim()=="")
{
SetErrInfo(txtDeptCode,"部门编号必填");
txtDeptCode.Focus();
return false;
}
else
{
SetErrInfo(txtDeptCode,"");
}
if(txtDeptName.Text.Trim()=="")
{
SetErrInfo(txtDeptCode,"部门名称必填");
txtDeptName.Focus();
return false;
}
else
{
SetErrInfo(txtDeptName,"");
}
if(cboParentDept.SelectedRow==null)
{
SetErrInfo(cboParentDept,"上级部门必填");
cboParentDept.Focus();
return false;
}
else
{
SetErrInfo(cboParentDept,"");
}
if(cboDeptPerson.SelectedRow==null)
{
SetErrInfo(cboDeptPerson,"部门负责人必填");
cboDeptPerson.Focus();
return false;
}
else
{
SetErrInfo(cboDeptPerson,"");
}
return true;
}
protected override void SetUI()
{
//设置单据修改人员为当前线程负责人的GUID,及当前时间.以便保存到数据库中
txtEditor.Tag=Permisser.CurrentUserGuid;
dtEditTime.DateTime=System.DateTime.Now;
}
#endregion
#region 初始化窗体界面中的下拉列表
private void InitDeptEmp()
{
EmployeeManager empManager=new EmployeeManager();
this.cboDeptPerson.DataSource=empManager.GetEmpsForDept();
this.cboDeptPerson.DisplayMember="EmpName";
this.cboDeptPerson.ValueMember="OID";
}
private void InitDeptList()
{
DeptManager dptMng=new DeptManager();
this.cboParentDept.DataSource=dptMng.GetAllDeptByRelation();
this.cboParentDept.DisplayMember="DeptName";
this.cboParentDept.ValueMember="OID";
}
private void cboParentDept_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
{
try
{
e.Layout.Grid.DisplayLayout.Load("CommonGrid.lyt");
e.Layout.Bands[0].Columns["OID"].Hidden=true;
e.Layout.Bands[0].Columns["DeptID"].Header.Caption="部门编号";
e.Layout.Bands[0].Columns["DeptName"].Header.Caption="部门名称";
e.Layout.Bands[0].Columns["Phone"].Header.Caption="电话";
e.Layout.Bands[0].Columns["ParentDeptName"].Header.Caption="上级部门";
e.Layout.Bands[0].Columns["EmpName"].Header.Caption="负责人";
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,this.Text,MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
private void cboDeptPerson_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
{
try
{
e.Layout.Grid.DisplayLayout.Load("CommonGrid.lyt");
e.Layout.Bands[0].Columns["OID"].Hidden=true;
e.Layout.Bands[0].Columns["EmpID"].Header.Caption="员工编号";
e.Layout.Bands[0].Columns["EmpName"].Header.Caption="员工姓名";
e.Layout.Bands[0].Columns["DeptName"].Header.Caption="部门名称";
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,this.Text,MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
#endregion
private void FrmDeptAdd_OnAfterToolClick(object sender,Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
{
Department dpt=(Department)sender;
frmOwner.UpdateDeptNode(dpt.OID.ToString(),dpt.DeptName,dpt.ParentID.ToString());
}
}
}
对于信息收集子窗体来说,做的事情就很简单了,只是做一些数据验证,然后提供了父类对数据保存前及保存后对子类函数的调用。以方便子类进行一些自定义操作。
此程序经过实际测试,运行良好,对于性能的损失也不是很大。同时解决了所有数据收集的重复操作过程。 以上代码只是初次实验代码。未经过优化。此次演示时,派生窗体的控件是事先构建好的,下次将注重解决界面控件自动生成问题。
如有意,请联系greystar@e172.com 索取原代码。