| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383 |
-
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Security.Permissions;
- using System.Windows.Forms;
- using Dongke.WinForm.Utilities;
- namespace Dongke.WinForm.Controls
- {
- /// <summary>
- /// 标准复选框列表控件
- /// </summary>
- [ToolboxBitmap(typeof(CheckedListBox))]
- [DefaultBindingProperty("SelectedValue"), DefaultProperty("Items"), DefaultEvent("ItemChecked")]
- [LookupBindingProperties("DataSource", "DisplayMember", "ValueMember", "SelectedValue")]
- public class CklCheckedListBox : CheckedListBox, IDKControl, IDataVerifiable, IAsyncControl
- {
- #region 事件声明
- #region ItemChecked
- /// <summary>
- /// 当某项的选中状态更改后发生
- /// </summary>
- private static readonly object EventItemChecked = new object();
- /// <summary>
- /// 当某项的选中状态更改后发生。
- /// </summary>
- [Description("当某项的选中状态更改后发生。"), Category("CustomerEx")]
- public event ItemCheckedEventHandler ItemChecked
- {
- add
- {
- base.Events.AddHandler(EventItemChecked, value);
- }
- remove
- {
- base.Events.RemoveHandler(EventItemChecked, value);
- }
- }
- #endregion
- #region HasErrorChanged
- /// <summary>
- /// 当 HasError 属性的值更改时发生。
- /// </summary>
- private static readonly object EventHasErrorChanged = new object();
- /// <summary>
- /// 当 HasError 属性的值更改时发生。
- /// </summary>
- [Description("当 HasError 属性的值更改时发生。"), Category("CustomerEx")]
- public event EventHandler HasErrorChanged
- {
- add
- {
- base.Events.AddHandler(EventHasErrorChanged, value);
- }
- remove
- {
- base.Events.RemoveHandler(EventHasErrorChanged, value);
- }
- }
- #endregion
- #endregion
- #region 成员变量
- /// <summary>
- /// 焦点是否进入控件
- /// </summary>
- private bool _entered = false;
- /// <summary>
- /// 鼠标是否进入控件
- /// </summary>
- private bool _mouseOver = false;
- /// <summary>
- /// 边框颜色
- /// </summary>
- private Color? _borderColor = null;
- /// <summary>
- /// 是否单选
- /// </summary>
- private bool _simpleChecked = false;
- /// <summary>
- /// 正在处理必须选中
- /// </summary>
- private bool _isMustChecking = false;
- /// <summary>
- /// 正在处理单项选中
- /// </summary>
- private bool _isSimpleChecking = false;
- /// <summary>
- /// 上次选中项个数
- /// </summary>
- private int _inputCount = -1;
- #endregion
- #region 构造函数
- /// <summary>
- /// 标准复选框列表控件
- /// </summary>
- public CklCheckedListBox()
- {
- base.CheckOnClick = true;
- base.FormattingEnabled = false;
- base.ThreeDCheckBoxes = false;
- CommonSetting.InitControls(this);
- }
- #endregion
- #region 属性
- /// <summary>
- /// 获取选中的文本
- /// </summary>
- [Description("获取选中的文本。"), Category("CustomerEx")]
- [DefaultValue(null)]
- public string[] CheckedTexts
- {
- get
- {
- List<string> checkedTexts = this.GetCheckedItemTexts();
- return checkedTexts.ToArray();
- }
- }
- /// <summary>
- /// 获取选中的值
- /// </summary>
- [Description("获取选中的值。"), Category("CustomerEx")]
- [DefaultValue(null)]
- public object[] CheckedValues
- {
- get
- {
- List<object> checkedValues = this.GetCheckedItemValues();
- return checkedValues.ToArray();
- }
- }
- /// <summary>
- /// 获取选中项的文本表示形式,用【,】隔开。
- /// </summary>
- [Description("获取选中项的文本表示形式,用【,】隔开。"), Category("CustomerEx")]
- [DefaultValue(null)]
- public string CheckedText
- {
- get
- {
- string text =
- string.Join(Constant.MULTIPLE_TEXT_SEPARATOR,
- this.GetCheckedItemTexts());
- return text;
- }
- }
- /// <summary>
- /// 获取选中项的值,用【,】隔开
- /// </summary>
- [Description("获取选中项的值,用【,】隔开。"), Category("CustomerEx")]
- [DefaultValue(null)]
- public string CheckedValue
- {
- get
- {
- string value =
- string.Join(Constant.MULTIPLE_VALUE_SEPARATOR,
- this.GetCheckedItemValues());
- return value;
- }
- }
- /// <summary>
- /// 获取或设置控件是否单项选中。
- /// </summary>
- [Description("获取或设置控件是否单项选中。"), Category("CustomerEx")]
- [DefaultValue(false)]
- public bool SimpleChecked
- {
- get
- {
- return this._simpleChecked;
- }
- set
- {
- if (this._simpleChecked != value)
- {
- this._simpleChecked = value;
- this.ResetMustOrSimpleChecked();
- }
- }
- }
- /// <summary>
- /// 获取一个值,该值指示控件是否有输入焦点。
- /// </summary>
- [DefaultValue(false)]
- [Browsable(false)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- [EditorBrowsable(EditorBrowsableState.Advanced)]
- public virtual bool Entered
- {
- get
- {
- return this._entered;
- }
- protected set
- {
- if (this._entered != value)
- {
- this._entered = value;
- this.InvalidateBorder();
- }
- }
- }
- /// <summary>
- /// 获取一个值,该值指示鼠标是否在控件上方。
- /// </summary>
- [DefaultValue(false)]
- [Browsable(false)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- [EditorBrowsable(EditorBrowsableState.Advanced)]
- public virtual bool MouseOver
- {
- get
- {
- return this._mouseOver;
- }
- protected set
- {
- if (this._mouseOver != value)
- {
- this._mouseOver = value;
- this.InvalidateBorder();
- }
- }
- }
- #endregion
- #region 重写属性
- /// <summary>
- /// 获取或设置一个值,该值指示当选定项时是否应切换复选框
- /// </summary>
- [DefaultValue(true)]
- //[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- //[EditorBrowsable(EditorBrowsableState.Never)]
- public new bool CheckOnClick
- {
- get
- {
- return base.CheckOnClick;
- }
- set
- {
- base.CheckOnClick = value;
- }
- }
- /// <summary>
- /// 获取或设置控件的数据源
- /// </summary>
- [Description("获取或设置控件的数据源。"), Category("CustomerEx")]
- [DefaultValue(null)]
- [Browsable(true)]
- public new object DataSource
- {
- get
- {
- return base.DataSource;
- }
- set
- {
- string displayMember = base.DisplayMember;
- base.DataSource = value;
- base.DisplayMember = displayMember;
- //if (!this._mustInput)
- {
- this.SelectedIndex = -1;
- }
- }
- }
- /// <summary>
- /// 获取或设置一个字符串,该字符串指定要显示其内容的列表框中所含对象的属性
- /// </summary>
- [Description("获取或设置一个字符串,该字符串指定要显示其内容的列表框中所含对象的属性。"), Category("CustomerEx")]
- [DefaultValue("")]
- [Browsable(true)]
- public new string DisplayMember
- {
- get
- {
- return base.DisplayMember;
- }
- set
- {
- base.DisplayMember = value;
- }
- }
- /// <summary>
- /// 获取或设置一个字符串,该字符串指定要从中取值的数据源的属性
- /// </summary>
- [Description("获取或设置一个字符串,该字符串指定要从中取值的数据源的属性。"), Category("CustomerEx")]
- [DefaultValue("")]
- [Browsable(true)]
- public new string ValueMember
- {
- get
- {
- return base.ValueMember;
- }
- set
- {
- base.ValueMember = value;
- }
- }
- #endregion
- #region 重写事件
- #region 属性改变
- /// <summary>
- /// 引发 DataSourceChanged 事件。
- /// </summary>
- /// <param name="e"></param>
- protected override void OnDataSourceChanged(EventArgs e)
- {
- try
- {
- base.OnDataSourceChanged(e);
- }
- catch
- {
- }
- this.ResetMustOrSimpleChecked();
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="e"></param>
- protected override void OnDisplayMemberChanged(EventArgs e)
- {
- base.OnDisplayMemberChanged(e);
- this.ResetMustOrSimpleChecked();
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="e"></param>
- protected override void OnValueMemberChanged(EventArgs e)
- {
- base.OnValueMemberChanged(e);
- this.ResetMustOrSimpleChecked();
- }
- /// <summary>
- /// 引发 HasErrorChanged 事件
- /// </summary>
- /// <param name="e">包含事件数据的 EventArgs</param>
- protected virtual void OnHasErrorChanged(EventArgs e)
- {
- EventHandler eventHandler = (EventHandler)base.Events[EventHasErrorChanged];
- if (eventHandler != null)
- {
- eventHandler(this, e);
- }
- }
- #endregion
- #region 行为事件
- /// <summary>
- /// 引发 ItemCheck 事件
- /// </summary>
- /// <param name="ice"></param>
- protected override void OnItemCheck(ItemCheckEventArgs ice)
- {
- if (this._isMustChecking)
- {
- return;
- }
- if (this._isSimpleChecking)
- {
- base.OnItemCheck(ice);
- if (ice.NewValue == CheckState.Indeterminate)
- {
- ice.NewValue = CheckState.Checked;
- }
- return;
- }
- int checkedCount = (ice.NewValue == CheckState.Unchecked ? -1 : 1) + this.CheckedIndices.Count;
- if (this._mustInput && checkedCount == 0)
- {
- this._isMustChecking = true;
- //base.SetItemChecked(ice.Index, true);
- ice.NewValue = CheckState.Checked;
- this._isMustChecking = false;
- return;
- }
- if (this._simpleChecked && checkedCount > 1)
- {
- this._isSimpleChecking = true;
- foreach (int index in this.CheckedIndices)
- {
- if (index != ice.Index)
- {
- base.SetItemChecked(index, false);
- //this.SetItemChecked(index, false);
- }
- }
- this._isSimpleChecking = false;
- }
- base.OnItemCheck(ice);
- if (ice.NewValue == CheckState.Indeterminate)
- {
- ice.NewValue = CheckState.Checked;
- }
- }
- /// <summary>
- /// 引发 ItemChecked 事件
- /// </summary>
- /// <param name="ice">包含事件数据的 ItemCheckedEventArgs</param>
- protected virtual void OnItemChecked(ItemCheckedEventArgs ice)
- {
- ItemCheckedEventHandler eventHandler = (ItemCheckedEventHandler)base.Events[EventItemChecked];
- if (eventHandler != null)
- {
- eventHandler(this, ice);
- }
- }
- #endregion
- #region 键盘事件
- /// <summary>
- /// 引发 System.Windows.Forms.Control.KeyPress 事件
- /// </summary>
- /// <param name="e">所引发的 System.Windows.Forms.KeyPressEventArgs</param>
- protected override void OnKeyPress(KeyPressEventArgs e)
- {
- if (e.KeyChar == Constant.C_SPACE && this.SelectionMode != SelectionMode.None)
- {
- int selectedIndex = this.SelectedIndex;
- if (selectedIndex < 0 || selectedIndex >= this.Items.Count)
- {
- base.OnKeyPress(e);
- return;
- }
- CheckState checkedState = this.GetItemCheckState(selectedIndex);
- base.OnKeyPress(e);
- CheckState newCheckValue = this.GetItemCheckState(selectedIndex);
- if (checkedState != newCheckValue)
- {
- this.CheckOnItemChecked();
- ItemCheckedEventArgs itemCheckedEventArgs =
- new ItemCheckedEventArgs(selectedIndex, newCheckValue, checkedState);
- this.OnItemChecked(itemCheckedEventArgs);
- }
- }
- else
- {
- base.OnKeyPress(e);
- }
- }
- /// <summary>
- /// 键盘点击
- /// </summary>
- /// <param name="e"></param>
- protected override void OnKeyDown(KeyEventArgs e)
- {
- base.OnKeyDown(e);
- if (this.SelectedIndex < 1 && (e.KeyCode == Keys.Up || e.KeyCode == Keys.Left))
- {
- this.SelectPrevItem();
- e.Handled = true;
- return;
- }
- if (this.SelectedIndex == this.Items.Count - 1 &&
- (e.KeyCode == Keys.Down || e.KeyCode == Keys.Right))
- {
- this.SelectNextItem();
- e.Handled = true;
- return;
- }
- }
- #endregion
- #region 焦点事件
- /// <summary>
- /// 输入焦点进入控件
- /// </summary>
- /// <param name="e"></param>
- protected override void OnEnter(EventArgs e)
- {
- this.Entered = true;
- base.OnEnter(e);
- }
- /// <summary>
- /// 获得焦点
- /// </summary>
- /// <param name="e"></param>
- protected override void OnGotFocus(EventArgs e)
- {
- base.OnGotFocus(e);
- }
- /// <summary>
- /// 失去焦点
- /// </summary>
- /// <param name="e"></param>
- protected override void OnLostFocus(EventArgs e)
- {
- base.OnLostFocus(e);
- }
- /// <summary>
- /// 输入焦点离开控件
- /// </summary>
- /// <param name="e"></param>
- protected override void OnLeave(EventArgs e)
- {
- base.OnLeave(e);
- //this.Entered = false;
- }
- /// <summary>
- /// 控件正在验证
- /// </summary>
- /// <param name="e"></param>
- protected override void OnValidating(CancelEventArgs e)
- {
- base.OnValidating(e);
- if (this.HasError && !this._canLostFocusOnError)
- {
- e.Cancel = true;
- }
- }
- /// <summary>
- /// 控件完成验证
- /// </summary>
- /// <param name="e"></param>
- protected override void OnValidated(EventArgs e)
- {
- this.Entered = false;
- base.OnValidated(e);
- }
- #endregion
- #region 鼠标事件
- /// <summary>
- /// 鼠标进入控件
- /// </summary>
- /// <param name="e"></param>
- protected override void OnMouseEnter(EventArgs e)
- {
- this.MouseOver = true;
- base.OnMouseEnter(e);
- }
- /// <summary>
- /// 鼠标离开控件
- /// </summary>
- /// <param name="e"></param>
- protected override void OnMouseLeave(EventArgs e)
- {
- base.OnMouseLeave(e);
- this.MouseOver = false;
- }
- /// <summary>
- /// 鼠标移动
- /// </summary>
- /// <param name="e"></param>
- protected override void OnMouseMove(MouseEventArgs e)
- {
- base.OnMouseMove(e);
- }
- /// <summary>
- /// 鼠标按下
- /// </summary>
- /// <param name="e"></param>
- protected override void OnMouseDown(MouseEventArgs e)
- {
- base.OnMouseDown(e);
- }
- /// <summary>
- /// 鼠标抬起
- /// </summary>
- /// <param name="e"></param>
- protected override void OnMouseUp(MouseEventArgs e)
- {
- base.OnMouseUp(e);
- }
- #endregion
- #endregion
- #region 重写方法
- /// <summary>
- /// 处理 System.Windows.Forms.CheckedListBox 控件从顶级窗口中接收的命令消息
- /// </summary>
- /// <param name="m">顶级窗口发送到此控件的 System.Windows.Forms.Message</param>
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
- protected override void WmReflectCommand(ref Message m)
- {
- int n = (int)((long)m.WParam);
- n = n >> 16 & 65535;
- switch (n)
- {
- case 1:
- case 2:
- int selectedIndex = this.SelectedIndex;
- if (selectedIndex < 0 || selectedIndex >= this.Items.Count)
- {
- base.WmReflectCommand(ref m);
- return;
- }
- CheckState checkedState = this.GetItemCheckState(selectedIndex);
- base.WmReflectCommand(ref m);
- CheckState newCheckValue = this.GetItemCheckState(selectedIndex);
- if (checkedState != newCheckValue)
- {
- this.CheckOnItemChecked();
- ItemCheckedEventArgs itemCheckEventArgs =
- new ItemCheckedEventArgs(selectedIndex, newCheckValue, checkedState);
- this.OnItemChecked(itemCheckEventArgs);
- }
- return;
- default:
- base.WmReflectCommand(ref m);
- return;
- }
- }
- /// <summary>
- /// 处理 Windows 消息
- /// </summary>
- /// <param name="m">要处理的 Windows System.Windows.Forms.Message</param>
- protected override void WndProc(ref Message m)
- {
- base.WndProc(ref m);
- if (m.Msg == (int)WindowsMessage.WM_PAINT ||
- m.Msg == (int)WindowsMessage.WM_NCPAINT ||
- m.Msg == (int)WindowsMessage.WM_CTLCOLOREDIT)
- {
- //this.WmBorderPaint(ref m);
- BorderColorPaint.WmBorderPaint(this._borderColor, this.Width, this.Height, ref m);
- }
- }
- #endregion
- #region 公有方法
- /// <summary>
- /// 获取指定项的成员属性的值
- /// </summary>
- /// <param name="item">项</param>
- /// <returns>值</returns>
- public virtual object GetItemValue(object item)
- {
- if (this.DataManager != null)
- {
- string dataMember = this.ValueMember;
- if (dataMember == null)
- {
- dataMember = string.Empty;
- }
- int num = dataMember.LastIndexOf(Constant.S_POINT);
- if (num != -1)
- {
- dataMember = dataMember.Substring(num + 1);
- }
- return this.FilterItemOnProperty(item, dataMember);
- }
- return null;
- }
- /// <summary>
- /// 获取选中项的文本表示形式
- /// </summary>
- /// <returns>选中项的文本</returns>
- public virtual List<string> GetCheckedItemTexts()
- {
- List<string> checkedTexts = new List<string>();
- foreach (object item in this.CheckedItems)
- {
- checkedTexts.Add(this.GetItemText(item));
- }
- return checkedTexts;
- }
- /// <summary>
- /// 获取选中项的成员属性的值
- /// </summary>
- /// <returns>选中项的值</returns>
- public virtual List<object> GetCheckedItemValues()
- {
- return this.GetCheckedItemValues<object>();
- }
- /// <summary>
- /// 获取选中项的成员属性的值
- /// </summary>
- /// <typeparam name="T">属性值的类型</typeparam>
- /// <returns>选中项的值</returns>
- public virtual List<T> GetCheckedItemValues<T>()
- {
- List<T> checkedValues = new List<T>();
- foreach (object item in this.CheckedItems)
- {
- checkedValues.Add((T)this.GetItemValue(item));
- }
- return checkedValues;
- }
- /// <summary>
- /// 设定全部项的复选状态
- /// </summary>
- /// <param name="value">若要将该项设置为选中,则为 true;否则为 false</param>
- public virtual void SetItemsChecked(bool value)
- {
- for (int i = 0; i < this.Items.Count; i++)
- {
- this.SetItemChecked(value, i);
- }
- }
- /// <summary>
- /// 获取全部项的复选状态
- /// </summary>
- /// <returns>选中状态</returns>
- public virtual CheckState GetItemsCheckState()
- {
- int checkedItemsCount = this.CheckedItems.Count;
- if (checkedItemsCount == 0)
- {
- return CheckState.Unchecked;
- }
- int itemsCount = this.Items.Count;
- if (checkedItemsCount != itemsCount)
- {
- return CheckState.Indeterminate;
- }
- if (!this.ThreeDCheckBoxes)
- {
- return CheckState.Checked;
- }
- for (int i = 0; i < this.CheckedItems.Count; i++)
- {
- CheckState cs = this.GetItemCheckState(i);
- if (cs == CheckState.Indeterminate)
- {
- return CheckState.Indeterminate;
- }
- }
- return CheckState.Checked;
- }
- /// <summary>
- /// 设定指定项的复选状态
- /// </summary>
- /// <param name="value">若要将该项设置为选中,则为 true;否则为 false</param>
- /// <param name="items">要为其设置复选状态的项</param>
- public virtual void SetItemCheckedByItem(bool value, params object[] items)
- {
- foreach (object item in items)
- {
- for (int i = 0; i < this.Items.Count; i++)
- {
- object itemInner = this.Items[i];
- if (itemInner == item)
- {
- this.SetItemChecked(value, i);
- return;
- }
- }
- }
- }
- /// <summary>
- /// 设定指定值的项的复选状态
- /// </summary>
- /// <param name="value">若要将该项设置为选中,则为 true;否则为 false</param>
- /// <param name="itemValues">要为其设置复选状态的项的值</param>
- public virtual void SetItemCheckedByValue(bool value, params object[] itemValues)
- {
- foreach (object itemValue in itemValues)
- {
- for (int i = 0; i < this.Items.Count; i++)
- {
- object itemValueInner = this.GetItemValue(this.Items[i]);
- if ((itemValueInner == null && itemValue == null) ||
- (itemValueInner != null && itemValueInner.Equals(itemValue)))
- {
- this.SetItemChecked(value, i);
- break;
- }
- }
- }
- }
- /// <summary>
- /// 设定指定值的项的复选状态
- /// </summary>
- /// <param name="value">若要将该项设置为选中,则为 true;否则为 false</param>
- /// <param name="itemValues">要为其设置复选状态的项的值</param>
- public virtual void SetItemCheckedByValueID(bool value, params string[] itemValues)
- {
- foreach (string itemValue in itemValues)
- {
- for (int i = 0; i < this.Items.Count; i++)
- {
- object itemValueInner = this.GetItemValue(this.Items[i]);
- if ((itemValueInner == null && itemValue == null) ||
- (itemValue != null && itemValue == itemValueInner + ""))
- {
- this.SetItemChecked(value, i);
- break;
- }
- }
- }
- }
- /// <summary>
- /// 设定指定索引处的项目选中或取消
- /// </summary>
- /// <param name="value">若要将该项设置为选中,则为 true;否则为 false</param>
- /// <param name="indexs">要为其设置复选状态的项的索引</param>
- public void SetItemChecked(bool value, params int[] indexs)
- {
- foreach (int index in indexs)
- {
- //this.SetItemCheckState(index, value ? CheckState.Checked : CheckState.Unchecked);
- // 响应OnItemChecked事件
- this.SetItemChecked(index, value);
- }
- }
- /// <summary>
- /// 设置指定索引处项的复选状态
- /// </summary>
- /// <param name="index">要为其设置状态的项的索引</param>
- /// <param name="value">若要将该项设置为选中,则为 true;否则为 false</param>
- public new void SetItemChecked(int index, bool value)
- {
- bool checkedValue = this.GetItemChecked(index);
- base.SetItemChecked(index, value);
- if (value != checkedValue)
- {
- this.CheckOnItemChecked();
- ItemCheckedEventArgs itemCheckEventArgs =
- new ItemCheckedEventArgs(index,
- value ? CheckState.Checked : CheckState.Unchecked,
- checkedValue ? CheckState.Checked : CheckState.Unchecked);
- this.OnItemChecked(itemCheckEventArgs);
- }
- }
- /// <summary>
- /// 设置指定索引处项的复选状态
- /// </summary>
- /// <param name="index">要为其设置状态的项的索引</param>
- /// <param name="value">System.Windows.Forms.CheckState 值之一</param>
- public new void SetItemCheckState(int index, CheckState value)
- {
- CheckState checkedState = this.GetItemCheckState(index);
- base.SetItemCheckState(index, value);
- if (value != checkedState)
- {
- this.CheckOnItemChecked();
- ItemCheckedEventArgs itemCheckEventArgs =
- new ItemCheckedEventArgs(index, value, checkedState);
- this.OnItemChecked(itemCheckEventArgs);
- }
- }
- /// <summary>
- /// 选择下一个Item
- /// </summary>
- /// <param name="clearSelect">是否能选择空</param>
- public void SelectNextItem(bool clearSelect = true)
- {
- int boxItemCount = this.Items.Count;
- if (boxItemCount < 1)
- {
- return;
- }
- int boxSelectedIndex = this.SelectedIndex;
- boxSelectedIndex++;
- if (boxSelectedIndex >= boxItemCount)
- {
- if (this._mustInput || !clearSelect)
- {
- boxSelectedIndex = 0;
- }
- else
- {
- boxSelectedIndex = -1;
- }
- }
- if (boxSelectedIndex < 0)
- {
- this.SelectedIndex = 0;
- this.ClearSelected();
- }
- else
- {
- this.SelectedIndex = boxSelectedIndex;
- }
- }
- /// <summary>
- /// 选择上一个Item
- /// </summary>
- /// <param name="clearSelect">是否能选择空</param>
- public void SelectPrevItem(bool clearSelect = true)
- {
- int boxItemCount = this.Items.Count;
- if (boxItemCount < 1)
- {
- return;
- }
- int boxSelectedIndex = this.SelectedIndex;
- boxSelectedIndex--;
- if (boxSelectedIndex < -1 ||
- ((this._mustInput || !clearSelect) && boxSelectedIndex < 0))
- {
- boxSelectedIndex = boxItemCount - 1;
- }
- if (boxSelectedIndex < 0)
- {
- this.ClearSelected();
- }
- else
- {
- this.SelectedIndex = boxSelectedIndex;
- }
- }
- #endregion
- #region 保护方法
- /// <summary>
- /// 重置全部项的复选状态(必选或单选)
- /// </summary>
- internal protected virtual void ResetMustOrSimpleChecked()
- {
- if (this._mustInput && this.CheckedIndices.Count == 0 && this.Items.Count > 0)
- {
- int index = this.SelectedIndex < 0 ? 0 : this.SelectedIndex;
- this.SetItemChecked(index, true);
- return;
- }
- if (this._simpleChecked && this.CheckedIndices.Count > 1)
- {
- this._isSimpleChecking = true;
- try
- {
- int index = this.CheckedIndices.Contains(this.SelectedIndex) ?
- this.SelectedIndex : this.CheckedIndices[0];
- foreach (int checkedIndex in this.CheckedIndices)
- {
- if (checkedIndex != index)
- {
- this.SetItemChecked(checkedIndex, false);
- }
- }
- }
- finally
- {
- this._isSimpleChecking = false;
- }
- return;
- }
- }
- #endregion
- #region 私有方法
- /// <summary>
- /// 当ItemChecked事件发生时,验证Error。
- /// </summary>
- private void CheckOnItemChecked()
- {
- if (this._mustInput)
- {
- if (this._inputCount != this.CheckedItems.Count)
- {
- this._inputCount = this.CheckedItems.Count;
- if (this._inputCount > 0)
- {
- this.ClearError();
- }
- else
- {
- if (this.SelectedIndex > 0)
- {
- this.SetItemChecked(this.SelectedIndex, true);
- return;
- }
- if (this.Items.Count > 0)
- {
- this.SetItemChecked(0, true);
- return;
- }
- this.SetError(true, ControlErrorCode.DKC_0001, this._itemName);
- }
- }
- }
- }
- /// <summary>
- /// 使父控件的指定区域无效(将其添加到控件的更新区域,下次绘制操作时将重新绘制更新区域),并向父控件发送绘制消息。
- /// </summary>
- 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
- #region IDataVerifiable 成员
- #region 成员变量
- /// <summary>
- /// 显示边框颜色
- /// </summary>
- private bool _showBorderColor = true;
- /// <summary>
- /// 控件的项目名
- /// </summary>
- private string _itemName = null;
- /// <summary>
- /// 控件是否是必须输入项目
- /// </summary>
- private bool _mustInput = false;
- /// <summary>
- /// 控件在验证输入错误时,如何提示
- /// </summary>
- private InputErrorAlert _errorAlert = InputErrorAlert.Validated;
- /// <summary>
- /// 是否显示必须输入项目的提示
- /// </summary>
- private bool _showMustInputAlert = true;
- /// <summary>
- /// 验证不通过时,焦点能否离开
- /// </summary>
- private bool _canLostFocusOnError = true;
- /// <summary>
- /// 验证是否有错误
- /// </summary>
- private bool _hasError = false;
- /// <summary>
- /// 是否自定义错误
- /// </summary>
- private bool _hasCustomerError = false;
- /// <summary>
- /// 错误编码
- /// </summary>
- private ControlErrorCode _errorCode = ControlErrorCode.DKC_0000;
- /// <summary>
- /// 错误消息
- /// </summary>
- private string _errorMessage = null;
- /// <summary>
- /// 自定义错误消息
- /// </summary>
- private string _customerErrorMessage = null;
- #endregion
- #region 属性
- /// <summary>
- /// 获取或设置控件是否显示边框颜色。
- /// </summary>
- [Description("获取或设置控件是否显示边框颜色。"), Category("IDataVerifiable")]
- [DefaultValue(true)]
- public bool ShowBorderColor
- {
- get
- {
- return this._showBorderColor;
- }
- set
- {
- if (this._showBorderColor != value)
- {
- this._showBorderColor = value;
- this.InvalidateBorder();
- }
- }
- }
- /// <summary>
- /// 获取或设置控件的项目名
- /// </summary>
- [Description("获取或设置控件的项目名。"), Category("IDataVerifiable")]
- [DefaultValue(null)]
- public string CDItemName
- {
- get
- {
- return this._itemName;
- }
- set
- {
- this._itemName = value;
- }
- }
- /// <summary>
- /// 获取或设置控件是否必须选中项目。
- /// </summary>
- [Description("获取或设置控件是否必须选中项目。"), Category("IDataVerifiable")]
- [DefaultValue(false)]
- public bool MustInput
- {
- get
- {
- return this._mustInput;
- }
- set
- {
- if (this._mustInput != value)
- {
- this._mustInput = value;
- this.ResetMustOrSimpleChecked();
- this.ValidateData();
- if (this._mustInput && this._showMustInputAlert)
- {
- this.InvalidateBorder();
- }
- }
- }
- }
- /// <summary>
- /// 获取或设置控件是否必须选中项目。
- /// </summary>
- [Description("获取或设置控件在验证输入错误时,如何提示。"), Category("IDataVerifiable")]
- [DefaultValue(typeof(InputErrorAlert), "Validated")]
- public InputErrorAlert InputErrorAlert
- {
- get
- {
- return this._errorAlert;
- }
- set
- {
- if (this._errorAlert != value)
- {
- this._errorAlert = value;
- this.InvalidateBorder();
- }
- }
- }
- /// <summary>
- /// 获取或设置控件是否显示必须输入项目提示
- /// </summary>
- [Description("获取或设置控件是否显示必须输入项目提示。"), Category("IDataVerifiable")]
- [DefaultValue(true)]
- public bool ShowMustInputAlert
- {
- get
- {
- return this._showMustInputAlert;
- }
- set
- {
- if (this._showMustInputAlert != value)
- {
- this._showMustInputAlert = value;
- this.InvalidateBorder();
- }
- }
- }
- /// <summary>
- /// 获取或设置当验证不通过时,控件是否可以失去焦点
- /// </summary>
- [Description("获取或设置当验证不通过时,控件是否可以失去焦点。"), Category("IDataVerifiable")]
- [DefaultValue(true)]
- public bool CanLostFocusOnError
- {
- get
- {
- return this._canLostFocusOnError;
- }
- set
- {
- this._canLostFocusOnError = value;
- }
- }
- /// <summary>
- /// 获取控件校验时是否有错误
- /// </summary>
- [Description("获取控件校验时是否有错误。"), Category("IDataVerifiable")]
- [DefaultValue(false)]
- public bool HasError
- {
- get
- {
- return this._hasCustomerError || this._hasError;
- }
- }
- /// <summary>
- /// 获取控件校验时的错误编码
- /// </summary>
- [Description("获取控件校验时的错误编码。"), Category("IDataVerifiable")]
- [DefaultValue(typeof(ControlErrorCode), "DKC_0000")]
- public ControlErrorCode ErrorCode
- {
- get
- {
- return this._hasCustomerError ? ControlErrorCode.DKC_C001 : this._errorCode;
- }
- }
- /// <summary>
- /// 获取控件校验时的错误消息
- /// </summary>
- [Description("获取控件校验时的错误编码。"), Category("IDataVerifiable")]
- [DefaultValue(null)]
- public string ErrorMessage
- {
- get
- {
- return this._hasCustomerError ? this._customerErrorMessage : this._errorMessage;
- }
- }
- #endregion
- #region 公有方法
- /// <summary>
- /// 设置自定义错误
- /// </summary>
- /// <param name="hasError">输入是否有错误</param>
- /// <param name="errorMessage">错误消息</param>
- 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();
- }
- }
- /// <summary>
- /// 清除自定义错误
- /// </summary>
- public virtual void ClearCustomerError()
- {
- this.SetCustomerError(false, null);
- }
- /// <summary>
- /// 验证输入内容
- /// </summary>
- /// <returns>验证结果</returns>
- public virtual bool ValidateData()
- {
- if (this._mustInput && this.CheckedItems.Count == 0)
- {
- this.SetError(true, ControlErrorCode.DKC_0001, this._itemName);
- return false;
- }
- this.ClearError();
- return true;
- }
- /// <summary>
- /// 清除输入项
- /// </summary>
- public virtual void ClearValue()
- {
- this.SetItemsChecked(false);
- }
- #endregion
- #region 保护方法
- /// <summary>
- /// 设置验证不通过错误
- /// </summary>
- /// <param name="hasError">是否有错误</param>
- /// <param name="code">错误编码</param>
- /// <param name="args">设置格式的对象</param>
- 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();
- }
- }
- /// <summary>
- /// 清除验证不通过错误
- /// </summary>
- protected void ClearError()
- {
- this.SetError(false, ControlErrorCode.DKC_0000);
- }
- #endregion
- #endregion
- #region IAsyncControl 成员
- #region 成员变量
- /// <summary>
- /// 异步处理开始时,控件状态
- /// </summary>
- private bool _asyncBeginStatus = false;
- private bool _asyncBeginFocused = false;
- #endregion
- #region 公有方法
- /// <summary>
- /// 开始异步处理
- /// </summary>
- /// <param name="doFocus">是否处理焦点</param>
- public virtual void BeginAsync(ref bool doFocus)
- {
- this._asyncBeginFocused = false;
- if (doFocus && this.Focused)
- {
- this._asyncBeginFocused = true;
- doFocus = false;
- }
- this._asyncBeginStatus = this.Enabled;
- this.Enabled = false;
- }
- /// <summary>
- /// 结束异步处理
- /// </summary>
- public virtual void EndAsync()
- {
- this.Enabled = this._asyncBeginStatus;
- if (this._asyncBeginFocused)
- {
- this.Focus();
- }
- }
- #endregion
- #endregion
- }
- }
|