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