| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
-
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.Windows.Forms;
- namespace Dongke.WinForm.Controls
- {
- /// <summary>
- /// 标识单选按钮
- /// </summary>
- [ToolboxBitmap(typeof(CheckBox))]
- public partial class ChkFlagCheckBox : UserControl, IDKControl, IMustInput, IAsyncControl
- {
- #region 成员变量
- /// <summary>
- /// 自动调整大小
- /// </summary>
- private bool _autoSize = true;
- /// <summary>
- /// 自动调整大小时的固定大小
- /// </summary>
- private Size _fixedSize = Size.Empty;
- #endregion
- #region 构造函数
- /// <summary>
- /// 标识单选按钮
- /// </summary>
- public ChkFlagCheckBox()
- {
- this.InitializeComponent();
- this.SetStyle(ControlStyles.FixedHeight, true);
- this.SetStyle(ControlStyles.FixedWidth, true);
- base.AutoSize = false;
- base.BackColor = Color.Transparent;
- }
- #endregion
- #region 属性
- /// <summary>
- /// 获取或设置标识CheckBox选中状态。
- /// </summary>
- [Description("获取或设置标识CheckBox选中状态。"), Category("CustomerEx")]
- [DefaultValue(typeof(FlagCheckBoxChecked), "None")]
- public FlagCheckBoxChecked FlagBoxChecked
- {
- get
- {
- int flag = -1;
- if (this.chkNo.Checked)
- {
- flag += 1;
- }
- if (this.chkYes.Checked)
- {
- flag += 2;
- }
- return (FlagCheckBoxChecked)flag;
- }
- set
- {
- switch (value)
- {
- case FlagCheckBoxChecked.All:
- this.chkYes.Checked = true;
- this.chkNo.Checked = true;
- break;
- case FlagCheckBoxChecked.Yes:
- this.chkYes.Checked = true;
- this.chkNo.Checked = false;
- break;
- case FlagCheckBoxChecked.No:
- this.chkYes.Checked = false;
- this.chkNo.Checked = true;
- break;
- default:
- if (this._mustInput)
- {
- this.chkYes.Checked = true;
- this.chkNo.Checked = true;
- }
- else
- {
- this.chkYes.Checked = false;
- this.chkNo.Checked = false;
- }
- break;
- }
- }
- }
- /// <summary>
- /// 获取或设置 Yes CheckBox 的文本。
- /// </summary>
- [Description("获取或设置 Yes CheckBox 的文本。"), Category("CustomerEx")]
- [DefaultValue("是")]
- public string TextYes
- {
- get
- {
- return this.chkYes.Text;
- }
- set
- {
- this.chkYes.Text = value;
- }
- }
- /// <summary>
- /// 获取或设置 No CheckBox 的文本。
- /// </summary>
- [Description("获取或设置 No CheckBox 的文本。"), Category("CustomerEx")]
- [DefaultValue("否")]
- public string TextNo
- {
- get
- {
- return this.chkNo.Text;
- }
- set
- {
- this.chkNo.Text = value;
- }
- }
- #endregion
- #region 重写属性
- /// <summary>
- /// 获取或设置一个值,该值指示是否自动调整控件的大小以完整显示其内容
- /// </summary>
- [DefaultValue(true)]
- public override bool AutoSize
- {
- get
- {
- return this._autoSize;
- //return base.AutoSize;
- }
- set
- {
- if (this._autoSize != value)
- {
- this._autoSize = value;
- if (this._autoSize)
- {
- this.SetStyle(ControlStyles.FixedHeight, true);
- this.SetStyle(ControlStyles.FixedWidth, true);
- }
- else
- {
- this.SetStyle(ControlStyles.FixedHeight, false);
- this.SetStyle(ControlStyles.FixedWidth, false);
- }
- //base.AutoSize = value;
- this.AdjustSize();
- }
- }
- }
- /// <summary>
- /// 获取或设置控件的背景色
- /// </summary>
- [DefaultValue(typeof(Color), "Transparent")]
- public override Color BackColor
- {
- get
- {
- return base.BackColor;
- }
- set
- {
- base.BackColor = value;
- }
- }
- #endregion
- #region 重写事件
- /// <summary>
- ///
- /// </summary>
- /// <param name="e"></param>
- protected override void OnResize(System.EventArgs e)
- {
- base.OnResize(e);
- this.AdjustSize();
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="e"></param>
- protected override void OnSizeChanged(System.EventArgs e)
- {
- base.OnSizeChanged(e);
- this.AdjustSize();
- }
- #endregion
- #region 事件处理
- /// <summary>
- /// 显示文本改变,导致控件大小改变
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void chkYes_SizeChanged(object sender, EventArgs e)
- {
- this.AdjustSize();
- }
- /// <summary>
- /// 显示文本改变,导致控件大小改变
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void chkNo_SizeChanged(object sender, EventArgs e)
- {
- this.AdjustSize();
- }
- /// <summary>
- /// 选中状态改变
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void chkYes_CheckedChanged(object sender, EventArgs e)
- {
- if (this._mustInput &&
- !this.chkYes.Checked &&
- !this.chkNo.Checked)
- {
- this.chkYes.Checked = true;
- }
- }
- /// <summary>
- /// 选中状态改变
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void chkNo_CheckedChanged(object sender, EventArgs e)
- {
- if (this._mustInput &&
- !this.chkYes.Checked &&
- !this.chkNo.Checked)
- {
- this.chkNo.Checked = true;
- }
- }
- #endregion
- #region 私有方法
- /// <summary>
- /// 调整控件尺寸大小
- /// </summary>
- private void AdjustSize()
- {
- this.chkYes.Location = new Point(0, 0);
- this.chkNo.Location = new Point(this.chkYes.Right, 0);
- if (!this._autoSize)
- {
- return;
- }
- this._fixedSize = new Size(this.chkNo.Right, this.chkNo.Bottom);
- this.Size = this._fixedSize;
- }
- #endregion
- #region IMustInput 成员
- #region 成员变量
- /// <summary>
- /// 获取或设置控件是否是必须输入项目
- /// </summary>
- private bool _mustInput = false;
- /// <summary>
- /// 是否显示必须输入项目的提示
- /// </summary>
- private bool _showMustInputAlert = true;
- #endregion
- #region 属性
- /// <summary>
- /// 获取或设置控件是否必须选中项目。
- /// </summary>
- [Description("获取或设置控件是否必须选中项目。"), Category("IMustInput")]
- [DefaultValue(false)]
- public bool MustInput
- {
- get
- {
- return this._mustInput;
- }
- set
- {
- if (this._mustInput != value)
- {
- this._mustInput = value;
- if (value && this.FlagBoxChecked == FlagCheckBoxChecked.None)
- {
- this.FlagBoxChecked = FlagCheckBoxChecked.All;
- }
- }
- }
- }
- /// <summary>
- /// 获取或设置控件是否显示必须输入项目提示
- /// </summary>
- [Description("获取或设置控件是否显示必须输入项目提示。"), Category("IMustInput")]
- [DefaultValue(true)]
- [Bindable(false)]
- [Browsable(false)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- [EditorBrowsable(EditorBrowsableState.Advanced)]
- public bool ShowMustInputAlert
- {
- get
- {
- return this._showMustInputAlert;
- }
- set
- {
- if (this._showMustInputAlert != value)
- {
- this._showMustInputAlert = value;
- }
- }
- }
- #endregion
- #region 公有方法
- /// <summary>
- /// 清除输入项
- /// </summary>
- public virtual void ClearValue()
- {
- this.FlagBoxChecked = FlagCheckBoxChecked.None;
- }
- #endregion
- #endregion
- #region IAsyncControl 成员
- #region 成员变量
- /// <summary>
- /// 异步处理开始时,控件状态
- /// </summary>
- private bool _asyncBeginStatus = false;
- private bool _asyncBeginFocused = false;
- private Control _activeControl = null;
- #endregion
- #region 公有方法
- /// <summary>
- /// 开始异步处理
- /// </summary>
- /// <param name="doFocus">是否处理焦点</param>
- 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.Enabled;
- this.Enabled = false;
- }
- /// <summary>
- /// 结束异步处理
- /// </summary>
- public virtual void EndAsync()
- {
- this.Enabled = this._asyncBeginStatus;
- if (this._asyncBeginFocused)
- {
- //this.Focus();
- this.ActiveControl = this._activeControl;
- this._activeControl = null;
- }
- }
- #endregion
- #endregion
- }
- }
|