using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Design; using System.Reflection; using System.Windows.Forms; using Dongke.WinForm.Utilities; namespace Dongke.WinForm.Controls { /// /// 查询文本框控件 /// [ToolboxBitmap(typeof(ScbSearchBox), "ToolboxBitmap.SearchBox_00.bmp")] [DefaultEvent("SelectedItemChanged"), DefaultProperty("SearchText")] public partial class ScbSearchBox : UserControl, IDKControl, IDataVerifiable, IAsyncControl { #region 事件声明 #region HasErrorChanged /// /// 当 HasError 属性的值更改时发生。 /// private static readonly object EventHasErrorChanged = new object(); /// /// 当 HasError 属性的值更改时发生。 /// [Description("当 HasError 属性的值更改时发生。"), Category("CustomerEx")] public event EventHandler HasErrorChanged { add { base.Events.AddHandler(EventHasErrorChanged, value); } remove { base.Events.RemoveHandler(EventHasErrorChanged, value); } } /// /// 引发 HasErrorChanged 事件 /// /// 包含事件数据的 EventArgs protected virtual void OnHasErrorChanged(EventArgs e) { EventHandler eventHandler = (EventHandler)base.Events[EventHasErrorChanged]; if (eventHandler != null) { eventHandler(this, e); } } #endregion #region ReadOnlyChanged /// /// 当 ReadOnly 属性的值更改时发生。 /// private static readonly object EventReadOnlyChanged = new object(); /// /// 当 ReadOnly 属性的值更改时发生 /// [Description("当 ReadOnly 属性的值更改时发生。"), Category("CustomerEx")] public event EventHandler ReadOnlyChanged { add { base.Events.AddHandler(EventReadOnlyChanged, value); } remove { base.Events.RemoveHandler(EventReadOnlyChanged, value); } } /// /// 引发 ReadOnlyChanged 事件 /// /// protected virtual void OnReadOnlyChanged(EventArgs e) { EventHandler eventHandler = base.Events[EventReadOnlyChanged] as EventHandler; if (eventHandler != null) { eventHandler(this, e); } } #endregion #region EditReadOnlyChanged /// /// 当 EditReadOnly 属性的值更改时发生。 /// private static readonly object EventEditReadOnlyChanged = new object(); /// /// 当 EditReadOnly 属性的值更改时发生 /// [Description("当 EditReadOnly 属性的值更改时发生。"), Category("CustomerEx")] public event EventHandler EditReadOnlyChanged { add { base.Events.AddHandler(EventEditReadOnlyChanged, value); } remove { base.Events.RemoveHandler(EventEditReadOnlyChanged, value); } } /// /// 引发 EditReadOnlyChanged 事件 /// /// protected virtual void OnEditReadOnlyChanged(EventArgs e) { EventHandler eventHandler = base.Events[EventEditReadOnlyChanged] as EventHandler; if (eventHandler != null) { eventHandler(this, e); } } #endregion #region SearchedItemChanged /// /// 当 SelectedItem 属性的值更改时发生。 /// private static readonly object EventSearchedItemChanged = new object(); /// /// 当 SelectedItem 属性的值更改时发生 /// [Description("当 SearchedItem 属性的值更改时发生。"), Category("CustomerEx")] public event EventHandler SearchedItemChanged { add { base.Events.AddHandler(EventSearchedItemChanged, value); } remove { base.Events.RemoveHandler(EventSearchedItemChanged, value); } } /// /// 引发 SelectedItem 事件 /// /// protected virtual void OnSearchedItemChanged(EventArgs e) { EventHandler eventHandler = base.Events[EventSearchedItemChanged] as EventHandler; if (eventHandler != null) { eventHandler(this, e); } } #endregion #region TextValueChanged public delegate void TextBoxChangedHandle(object sender, TextChangeEventArgs e); public event TextBoxChangedHandle TextValueChanged; public class TextChangeEventArgs : EventArgs { public TextChangeEventArgs(string message) { } } private void txtCondition1_TextChanged(object sender, EventArgs e) { if (TextValueChanged != null) { TextValueChanged(this, new TextChangeEventArgs(this.txtCondition1.Text)); } } #endregion #endregion #region 成员变量 /// /// 查询窗体 /// protected ISearchBoxForm _searchForm = null; /// /// 指示是否能够多项选择。 /// private bool _multiSelect = false; /// /// 指示控件中的文本是否为只读 /// private bool _editReadOnly = true; /// /// 指示控件中是否为只读 /// private bool _readOnly = false; ///// ///// 为此控件显示的属性集合 ///// //List _displayMembers = new List(); ///// ///// 查询条件控件集合 ///// //List _queryConditions = new List(); ///// ///// 为此控件显示的属性 ///// private string _displayMember = string.Empty; /// /// 用作此控件中的项的实际值 /// private string _valueMember = string.Empty; /// /// 0:手动输入,1:SearchForm,2:初始化 /// private SearchBoxValueFrom _valueFrom = SearchBoxValueFrom.UserInput; /// /// 查询的范围权限类型 /// private PurviewType _purviewType = PurviewType.None; /// /// 初始化的文本 /// private string _initText = null; /// /// 初始化的值 /// private object _initValue = null; /// /// 选定的数据 /// protected DataTable _checkedData = null; /// /// 选定后给文本框赋值时 /// protected bool _resetText = false; /// /// 查询条件文本框的集合 /// protected List _queryControls = new List(); protected int _searchedPKMember; #endregion #region 构造函数 /// /// 查询文本框控件 /// public ScbSearchBox() :this(true) { } /// /// 查询文本框控件 /// public ScbSearchBox(bool isInitControls = true) { this.InitializeComponent(); CommonSetting.InitControls(this); if (isInitControls) { this.InitQueryControls(); } this._searchForm = this.CreatSearchForm(); if (this._searchForm != null) { //throw new NullReferenceException("SearchBoxForm"); this._searchForm.FormClosed += this.SearchBoxForm_FormClosed; } this.txtCondition1.TextChanged += this.Condition_TextChanged; this.txtCondition1.KeyDown += this.Condition_KeyDown; } #endregion #region 属性 /// /// 设置或获取选定的数据 /// [DefaultValue("设置或获取选定的数据")] public virtual DataTable CheckedData { get { return _checkedData; } set { this._checkedData = value; } } /// /// 窗体标题 /// [DefaultValue("")] public virtual string FormText { get { if (this._searchForm == null) { return string.Empty; } return this._searchForm.Text; } set { if (this._searchForm == null) { return; } this._searchForm.Text = value; } } /// /// 获取或设置查询条件(多选时,不能设置)。 /// [Description("获取或设置查询条件(多选时,不能设置)。"), Category("CustomerEx")] [DefaultValue("")] [Browsable(true)] //[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] //[EditorBrowsable(EditorBrowsableState.Advanced)] public override string Text { get { return this.txtCondition1.Text; } set { if (!this.MultiSelect /*&& !this.EditReadOnly && !this.ReadOnly*/) { this.txtCondition1.Text = value; } //base.Text = value; } } /// /// /// public TextBox Condition { get { return this.txtCondition1; } } /// /// 获取或设置一个值,该值指示文本框中的文本是否为只读。 /// [Description("获取或设置一个值,该值指示文本框中的文本是否为只读。"), Category("CustomerEx")] [DefaultValue(false)] public virtual bool ReadOnly { get { return this._readOnly; } set { if (this._readOnly != value) { this._readOnly = value; this.SetReadOnly(); this.OnReadOnlyChanged(EventArgs.Empty); } } } /// /// 获取或设置一个值,该值指示文本框中的文本是否为只读。 /// [Description("获取或设置一个值,该值指示文本框中的文本是否为只读。"), Category("CustomerEx")] [DefaultValue(true)] public virtual bool EditReadOnly { get { if (this.MultiSelect) { this._editReadOnly = true; } return this._editReadOnly; } set { if (this.MultiSelect) { value = true; } if (this._editReadOnly != value) { this._editReadOnly = value; this.SetEditReadOnly(); this.OnEditReadOnlyChanged(EventArgs.Empty); } } } /// /// 获取或设置控件的显示的属性 /// [Description("获取或设置控件的显示的属性。"), Category("CustomerEx")] [DefaultValue("")] public virtual string DisplayMember { get { //if (this._displayMembers.Count == 0) //{ // return string.Empty; //} //return this._displayMembers[0]; return this._displayMember; } set { if (value == null) { value = string.Empty; } if (this._displayMember != value) { this._displayMember = value; this.ResetDisplayText(); } //if (this.DisplayMember != value) //{ // if (this._displayMembers.Count == 0) // { // this._displayMembers.Add(value); // } // else // { // this._displayMembers[0] = value; // } // this.ResetDisplayText(); //} } } /// /// 获取或设置一个属性,该属性将用作控件中的项的实际值。 /// [Description("获取或设置一个属性,该属性将用作控件中的项的实际值。"), Category("CustomerEx")] [DefaultValue("")] public virtual string ValueMember { get { return this._valueMember; } set { this._valueMember = value; } } /// /// 获取或设置一个值,该值指示查询结果数据集中的主键。 /// [Description("获取或设置一个值,该值指示查询结果数据集中的主键。"), Category("CustomerEx")] [DefaultValue("")] public virtual string PKMember { get { if (this._searchForm != null) { return this._searchForm.PKMember; } return null; } protected set { if (this._searchForm != null) { this._searchForm.PKMember = value; } } } /// /// 获取或设置一个值,该值指示是否能够多项选择。 /// [Description("获取或设置一个值,该值指示是否能够多项选择。"), Category("CustomerEx")] [DefaultValue(false)] public virtual bool MultiSelect { get { if (this._searchForm != null) { this._multiSelect = this._searchForm.MultiSelect; } return this._multiSelect; } set { if (this._multiSelect != value) { this._multiSelect = value; if (this._searchForm != null) { this._searchForm.MultiSelect = value; } if (value) { this.EditReadOnly = value; } } } } /// /// 获取或设置限制查询的范围权限类型。 /// [Description("获取或设置限制查询的范围权限类型。"), Category("CustomerEx")] [DefaultValue(typeof(PurviewType), "None")] public virtual PurviewType PurviewType { get { return this._purviewType; } set { if (!Enum.IsDefined(typeof(PurviewType), value)) { return; } if (this._purviewType != value) { this._purviewType = value; if (this._searchForm != null) { this._searchForm.PurviewType = value; } } } } /// /// 获取查询窗体 /// [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Advanced)] public ISearchBoxForm SearchForm { get { return this._searchForm; } } /// /// 获取数据来源。 /// [Description("获取数据来源。"), Category("CustomerEx")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Advanced)] public SearchBoxValueFrom ValueFrom { get { return this._valueFrom; } } /// /// 获取选择的项目 /// [Description("获取选择的项目。"), Category("CustomerEx")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Advanced)] public DataRow SearchedItem { get { //if (this._valueFrom != SearchBoxValueFrom.SearchForm) //{ // return null; //} if (this._checkedData == null || this._checkedData.Rows.Count == 0) { return null; } return this._checkedData.Rows[0]; } } public object[] InitItems { get; private set; } /// /// 获取选择的项目的值。 /// [Description("获取选择的项目的ID值。"), Category("CustomerEx")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Advanced)] public int SearchedPKMember { set { _searchedPKMember = value; } get { //if (this._valueFrom == SearchBoxValueFrom.InitValue) //{ // return this._initValue; //} //if (this._valueFrom != SearchBoxValueFrom.SearchForm) //{ // return null; //} if (this._checkedData == null || this._checkedData.Rows.Count == 0 || !this._checkedData.Columns.Contains(this.PKMember)) { return _searchedPKMember; } int.TryParse(this._checkedData.Rows[0][this.PKMember] + "", out _searchedPKMember); return _searchedPKMember; } } /// /// 获取选择的项目的值。 /// [Description("获取选择的项目的值。"), Category("CustomerEx")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Advanced)] public object SearchedValue { get { //if (this._valueFrom == SearchBoxValueFrom.InitValue) //{ // return this._initValue; //} //if (this._valueFrom != SearchBoxValueFrom.SearchForm) //{ // return null; //} if (this._checkedData == null || this._checkedData.Rows.Count == 0 || !this._checkedData.Columns.Contains(this._valueMember)) { return this._initValue; } return this._checkedData.Rows[0][this._valueMember]; } } /// /// 获取选择的项目的文本表示形式。 /// [Description("获取选择的项目的文本表示形式。"), Category("CustomerEx")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Advanced)] public string SearchedText { get { if (this._valueFrom == SearchBoxValueFrom.InitValue) { return this._initText; } if (this._valueFrom != SearchBoxValueFrom.SearchForm) { return null; } if (this._checkedData == null || this._checkedData.Rows.Count == 0 || !this._checkedData.Columns.Contains(this._displayMember)) { return null; } return this.FormatDisplay(this._checkedData.Rows[0][this._displayMember]); } } /// /// 获取选定的项目 /// [Description("获取选择的项目。"), Category("CustomerEx")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Advanced)] public DataTable CheckedItems { get { if (this._valueFrom != SearchBoxValueFrom.SearchForm) { return null; } return this._checkedData; } } /// /// 获取选定的项目的值。 /// [Description("获取选定的项目的值。"), Category("CustomerEx")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Advanced)] public object[] CheckedValues { get { if (this._valueFrom != SearchBoxValueFrom.SearchForm) { return null; } if (this._checkedData == null || this._checkedData.Rows.Count == 0 || !this._checkedData.Columns.Contains(this._valueMember)) { return null; } object[] values = new object[this._checkedData.Rows.Count]; for (int i = 0; i < this._checkedData.Rows.Count; i++) { values[i] = this._checkedData.Rows[i][this._valueMember]; } return values; } } /// /// 获取选定的项目的值,用【,】隔开。 /// [Description("获取选定的项目的值,用【,】隔开。"), Category("CustomerEx")] //[Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Advanced)] public string CheckedValue { get { if (this._valueFrom != SearchBoxValueFrom.SearchForm) { return null; } object[] values = this.CheckedValues; if (values == null) { return string.Empty; } string value = string.Join(Constant.MULTIPLE_VALUE_SEPARATOR, values); return value; } } /// /// 获取选定的项目的ID。 /// [Description("获取选定的项目的ID。"), Category("CustomerEx")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Advanced)] public object[] CheckedPKMembers { get { if (this._valueFrom != SearchBoxValueFrom.SearchForm) { return null; } if (this._checkedData == null || this._checkedData.Rows.Count == 0 || !this._checkedData.Columns.Contains(this._valueMember)) { return null; } object[] values = new object[this._checkedData.Rows.Count]; for (int i = 0; i < this._checkedData.Rows.Count; i++) { values[i] = this._checkedData.Rows[i][PKMember]; } return values; } } /// /// 获取选定的项目的ID,用【,】隔开。 /// [Description("获取选定的项目的ID,用【,】隔开。"), Category("CustomerEx")] //[Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Advanced)] public string CheckedPKMember { get { if (this._valueFrom != SearchBoxValueFrom.SearchForm) { return null; } object[] values = this.CheckedPKMembers; if (values == null) { return string.Empty; } string value = string.Join(Constant.MULTIPLE_VALUE_SEPARATOR, values); return value; } } /// /// 获取选定的项目的文本表示形式。 /// [Description("获取选定的项目的文本表示形式。"), Category("CustomerEx")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Advanced)] public string[] CheckedTexts { get { if (this._valueFrom != SearchBoxValueFrom.SearchForm) { return null; } if (this._checkedData == null || this._checkedData.Rows.Count == 0 || !this._checkedData.Columns.Contains(this._displayMember)) { return null; } string[] values = new string[this._checkedData.Rows.Count]; for (int i = 0; i < this._checkedData.Rows.Count; i++) { values[i] = this.FormatDisplay(this._checkedData.Rows[i][this._displayMember]); } return values; } } /// /// 获取选定的项目的文本表示形式,用【,】隔开。 /// [Description("获取选定的项目的文本表示形式,用【,】隔开。"), Category("CustomerEx")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Advanced)] public string CheckedText { get { if (this._valueFrom != SearchBoxValueFrom.SearchForm) { return null; } string[] values = this.CheckedTexts; if (values == null) { return string.Empty; } string value = string.Join(Constant.MULTIPLE_TEXT_SEPARATOR, values); return value; } } /// /// 获取或设置其他自定义属性。 /// [Description("获取或设置其他自定义属性。"), Category("CustomerEx")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Advanced)] public ExtendedProperties Properties { get { if (this._searchForm == null) { return null; } return this._searchForm.Properties; } } #endregion #region 重写属性 /// /// 获取或设置控件四周绘制的边框的类型 /// [Description("获取或设置控件四周绘制的边框的类型。"), Category("CustomerEx")] [DefaultValue(typeof(BorderStyle), "None")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Advanced)] public new BorderStyle BorderStyle { get { return base.BorderStyle; } set { base.BorderStyle = value; } } /// /// 获取或设置控件的背景色 /// [DefaultValue(typeof(Color), "Transparent")] public override Color BackColor { get { return base.BackColor; } set { base.BackColor = value; } } #endregion #region 事件处理 /// /// 键盘按下 /// /// /// protected void Condition_KeyDown(object sender, KeyEventArgs e) { if (this.EditReadOnly && !this._readOnly) { if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete) { this.ClearValue(); } } } /// /// 文本改变 /// /// /// protected void Condition_TextChanged(object sender, EventArgs e) { if (this._resetText) { return; } this._valueFrom = SearchBoxValueFrom.UserInput; } /// /// 关闭查询窗体 /// /// /// protected void SearchBoxForm_FormClosed(object sender, FormClosedEventArgs e) { this.OnSearchFormClosed(e); } /// /// 点击查询按钮 /// /// /// private void btnSearch_Click(object sender, EventArgs e) { this.ShowSearchForm(); } #endregion #region 公有方法 /// /// 打开查询窗体 /// public virtual void ShowSearchForm() { if (this._searchForm == null) { return; } //if (!this.MultiSelect) //{ // this._searchForm.SetConditions(this.txtCondition1.Text); //} if (this.ValueFrom == SearchBoxValueFrom.UserInput) { string display = this.DisplayMember.ToUpper(); if (display.EndsWith("CODE")) { this.SearchForm.SetConditions(this.txtCondition1.Text, ""); } else { this.SearchForm.SetConditions(this.txtCondition1.Text); } } this._searchForm.Show(); } /// /// 初始化显示的值 /// /// /// public virtual void InitValue(string text, object value, params object[] items) { this._initText = text; this._initValue = value; this.txtCondition1.Text = text; this._valueFrom = SearchBoxValueFrom.InitValue; InitItems = items; } ///// ///// 关闭查询窗体 ///// //public virtual void CloseSearchForm() //{ // this._searchForm.CloseSearchForm(); //} ///// ///// 清除查询条件 ///// //public virtual void ClearConditions() //{ // this._searchForm.ClearConditions(); //} ///// ///// 清除查询结果 ///// //public virtual void ClearQueryResults() //{ // this._searchForm.ClearQueryResults(); //} #endregion #region 保护方法 /// /// 初始化查询文本框集合 /// protected virtual void InitQueryControls() { this._queryControls.Add(this.txtCondition1); } /// /// 创建查询窗体。 /// protected virtual ISearchBoxForm CreatSearchForm() { //SearchBoxForm form = FormFactory.CreatForm(); //return form; return null; } /// /// 当查询窗体关闭时。 /// /// protected virtual void OnSearchFormClosed(FormClosedEventArgs e) { if (this._searchForm == null) { return; } this._searchForm.RefreshData(); DialogResult dialogResult = this._searchForm.DialogResult; if (dialogResult == DialogResult.OK) { this._checkedData = this._searchForm.CheckedData; if (this._checkedData == null || this._checkedData.Rows.Count == 0) { this.ClearValue(); } else { this._valueFrom = SearchBoxValueFrom.SearchForm; this.ResetDisplayText(); this._valueFrom = SearchBoxValueFrom.SearchForm; this.OnSearchedItemChanged(EventArgs.Empty); } } } /// /// 选择窗体关闭时,设置显示文本。 /// protected virtual void ResetDisplayText() { this._resetText = true; this.txtCondition1.Text = this.CheckedText; this._resetText = false; } /// /// 格式化显示文本。 /// /// 选定的值 /// 格式化显示的文本 protected virtual string FormatDisplay(object value) { // TODO return value.ToString(); } /// /// 设置文本框只读 /// protected virtual void SetEditReadOnly() { //foreach (TxtTextBox item in this._queryConditions) //{ // item.ReadOnly = this._editReadOnly; //} this.txtCondition1.ReadOnly = this.EditReadOnly || this._readOnly; } #endregion #region 私有方法 /// /// 设置控件只读 /// private void SetReadOnly() { this.SetEditReadOnly(); this.btnSearch.Enabled = !this._readOnly; } #endregion #region IDataVerifiable 成员 #region 成员变量 /// /// 显示边框颜色 /// private bool _showBorderColor = true; /// /// 控件的项目名 /// private string _itemName = null; /// /// 控件是否是必须输入项目 /// private bool _mustInput = false; /// /// 控件在验证输入错误时,如何提示 /// private InputErrorAlert _errorAlert = InputErrorAlert.Validated; /// /// 是否显示必须输入项目的提示 /// private bool _showMustInputAlert = true; /// /// 验证不通过时,焦点能否离开 /// private bool _canLostFocusOnError = true; /// /// 验证是否有错误 /// private bool _hasError = false; /// /// 是否自定义错误 /// private bool _hasCustomerError = false; /// /// 错误编码 /// private ControlErrorCode _errorCode = ControlErrorCode.DKC_0000; /// /// 错误消息 /// private string _errorMessage = null; /// /// 自定义错误消息 /// private string _customerErrorMessage = null; #endregion #region 属性 /// /// 获取或设置控件是否显示边框颜色。 /// [Description("获取或设置控件是否显示边框颜色。"), Category("IDataVerifiable")] [DefaultValue(true)] public bool ShowBorderColor { get { return this._showBorderColor; } set { if (this._showBorderColor != value) { this._showBorderColor = value; foreach (TextBox item in this._queryControls) { if (item is IDataVerifiable) { IDataVerifiable dvItem = item as IDataVerifiable; dvItem.ShowBorderColor = value; } } } } } /// /// 获取或设置控件的项目名 /// [Description("获取或设置控件的项目名。"), Category("IDataVerifiable")] [DefaultValue(null)] public string CDItemName { get { return this._itemName; } set { this._itemName = value; foreach (TextBox item in this._queryControls) { if (item is IDataVerifiable) { IDataVerifiable dvItem = item as IDataVerifiable; dvItem.CDItemName = value; } } } } /// /// 获取或设置控件是否必须选中项目。 /// [Description("获取或设置控件是否必须选中项目。"), Category("IDataVerifiable")] [DefaultValue(false)] public bool MustInput { get { return this._mustInput; } set { if (this._mustInput != value) { this._mustInput = value; foreach (TextBox item in this._queryControls) { if (item is IDataVerifiable) { IDataVerifiable dvItem = item as IDataVerifiable; dvItem.MustInput = value; } } } } } /// /// 获取或设置控件是否必须选中项目。 /// [Description("获取或设置控件在验证输入错误时,如何提示。"), Category("IDataVerifiable")] [DefaultValue(typeof(InputErrorAlert), "Validated")] public InputErrorAlert InputErrorAlert { get { return this._errorAlert; } set { if (this._errorAlert != value) { this._errorAlert = value; foreach (TextBox item in this._queryControls) { if (item is IDataVerifiable) { IDataVerifiable dvItem = item as IDataVerifiable; dvItem.InputErrorAlert = value; } } } } } /// /// 获取或设置控件是否显示必须输入项目提示 /// [Description("获取或设置控件是否显示必须输入项目提示。"), Category("IDataVerifiable")] [DefaultValue(true)] public bool ShowMustInputAlert { get { return this._showMustInputAlert; } set { if (this._showMustInputAlert != value) { this._showMustInputAlert = value; foreach (TextBox item in this._queryControls) { if (item is IDataVerifiable) { IDataVerifiable dvItem = item as IDataVerifiable; dvItem.ShowMustInputAlert = value; } } } } } /// /// 获取或设置当验证不通过时,控件是否可以失去焦点 /// [Description("获取或设置当验证不通过时,控件是否可以失去焦点。"), Category("IDataVerifiable")] [DefaultValue(true)] public bool CanLostFocusOnError { get { return this._canLostFocusOnError; } set { if (this._canLostFocusOnError != value) { this._canLostFocusOnError = value; foreach (TextBox item in this._queryControls) { if (item is IDataVerifiable) { IDataVerifiable dvItem = item as IDataVerifiable; dvItem.CanLostFocusOnError = value; } } } } } /// /// 获取控件校验时是否有错误 /// [Description("获取控件校验时是否有错误。"), Category("IDataVerifiable")] [DefaultValue(false)] public bool HasError { get { //return this._hasCustomerError || this._hasError; if (this._hasCustomerError) { return this._hasCustomerError; } foreach (TextBox item in this._queryControls) { if (item is IDataVerifiable) { IDataVerifiable dvItem = item as IDataVerifiable; if (dvItem.HasError) { return true; } } } return false; } } /// /// 获取控件校验时的错误编码 /// [Description("获取控件校验时的错误编码。"), Category("IDataVerifiable")] [DefaultValue(typeof(ControlErrorCode), "DKC_0000")] public ControlErrorCode ErrorCode { get { //return this._hasCustomerError ? ControlErrorCode.DKC_C001 : this._errorCode; if (this._hasCustomerError) { return ControlErrorCode.DKC_C001; } foreach (TextBox item in this._queryControls) { if (item is IDataVerifiable) { IDataVerifiable dvItem = item as IDataVerifiable; if (dvItem.HasError) { return dvItem.ErrorCode; } } } return this._errorCode; } } /// /// 获取控件校验时的错误消息 /// [Description("获取控件校验时的错误编码。"), Category("IDataVerifiable")] [DefaultValue(null)] public string ErrorMessage { get { //return this._hasCustomerError ? this._customerErrorMessage : this._errorMessage; if (this._hasCustomerError) { return this._customerErrorMessage; } foreach (TextBox item in this._queryControls) { if (item is IDataVerifiable) { IDataVerifiable dvItem = item as IDataVerifiable; if (dvItem.HasError) { return dvItem.ErrorMessage; } } } return this._errorMessage; } } #endregion #region 公有方法 /// /// 设置自定义错误 /// /// 输入是否有错误 /// 错误消息 public virtual void SetCustomerError(bool hasError, string errorMessage) { if (this._hasCustomerError != hasError || this._customerErrorMessage != errorMessage) { this._hasCustomerError = hasError; this._customerErrorMessage = errorMessage; this.OnHasErrorChanged(EventArgs.Empty); this.InvalidateBorder(); } } /// /// 清除自定义错误 /// public virtual void ClearCustomerError() { this.SetCustomerError(false, null); } /// /// 验证输入内容 /// /// 验证结果 public virtual bool ValidateData() { //string text = base.Text; //if (this._mustInput && text.Length == 0) //{ // this.SetError(true, ControlErrorCode.DKC_0001, this._itemName); // return false; //} //this.ClearError(); //return true; bool result = true; foreach (TextBox item in this._queryControls) { if (item is IDataVerifiable) { IDataVerifiable dvItem = item as IDataVerifiable; result = result && dvItem.ValidateData(); } } return result; } /// /// 清除输入项 /// public virtual void ClearValue() { foreach (TextBox item in this._queryControls) { if (item is IDataVerifiable) { IDataVerifiable dvItem = item as IDataVerifiable; dvItem.ClearValue(); } } if (this._checkedData != null && this._checkedData.Rows.Count > 0) { this._checkedData.Clear(); this.OnSearchedItemChanged(EventArgs.Empty); } SearchedPKMember = 0; } #endregion #region 保护方法 /// /// 设置验证不通过错误 /// /// 是否有错误 /// 错误编码 /// 设置格式的对象 protected void SetError(bool hasError, ControlErrorCode code, params object[] args) { if (this._hasError != hasError || this._errorCode != code) { this._hasError = hasError; this._errorCode = code; if (args != null && args.Length > 0) { this._errorMessage = string.Format(code.GetDescription(), args); } else { this._errorMessage = code.GetDescription(); } this.OnHasErrorChanged(EventArgs.Empty); this.InvalidateBorder(); } } /// /// 清除验证不通过错误 /// protected void ClearError() { this.SetError(false, ControlErrorCode.DKC_0000); } #endregion #region 私有方法 /// /// 使父控件的指定区域无效(将其添加到控件的更新区域,下次绘制操作时将重新绘制更新区域),并向父控件发送绘制消息。 /// private void InvalidateBorder() { //Color? borderColor = BorderColorPaint.GetBorderColor(this as IDataVerifiable, this._entered, this._mouseOver); //if (borderColor != this._borderColor) //{ // this._borderColor = borderColor; // if (this.Parent == null) // { // this.Invalidate(); // } // else // { // this.Parent.Invalidate(this.Bounds, true); // } //} } #endregion #endregion #region IAsyncControl 成员 #region 成员变量 /// /// 异步处理开始时,控件状态 /// private bool _asyncBeginStatus = false; private bool _asyncBeginFocused = false; private Control _activeControl = null; #endregion #region 公有方法 /// /// 开始异步处理 /// /// 是否处理焦点 public virtual void BeginAsync(ref bool doFocus) { this._asyncBeginFocused = false; //if (doFocus && this.Focused) if (doFocus && this.ActiveControl != null) { this._asyncBeginFocused = true; this._activeControl = this.ActiveControl; doFocus = false; } this._asyncBeginStatus = this.ReadOnly; this.ReadOnly = true; } /// /// 结束异步处理 /// public virtual void EndAsync() { this.ReadOnly = this._asyncBeginStatus; if (this._asyncBeginFocused) { //this.Focus(); this.ActiveControl = this._activeControl; this._activeControl = null; } } #endregion #endregion } }