ChkFlagCheckBox.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. 
  2. using System;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. namespace Dongke.WinForm.Controls
  7. {
  8. /// <summary>
  9. /// 标识单选按钮
  10. /// </summary>
  11. [ToolboxBitmap(typeof(CheckBox))]
  12. public partial class ChkFlagCheckBox : UserControl, IDKControl, IMustInput, IAsyncControl
  13. {
  14. #region 成员变量
  15. /// <summary>
  16. /// 自动调整大小
  17. /// </summary>
  18. private bool _autoSize = true;
  19. /// <summary>
  20. /// 自动调整大小时的固定大小
  21. /// </summary>
  22. private Size _fixedSize = Size.Empty;
  23. #endregion
  24. #region 构造函数
  25. /// <summary>
  26. /// 标识单选按钮
  27. /// </summary>
  28. public ChkFlagCheckBox()
  29. {
  30. this.InitializeComponent();
  31. this.SetStyle(ControlStyles.FixedHeight, true);
  32. this.SetStyle(ControlStyles.FixedWidth, true);
  33. base.AutoSize = false;
  34. base.BackColor = Color.Transparent;
  35. }
  36. #endregion
  37. #region 属性
  38. /// <summary>
  39. /// 获取或设置标识CheckBox选中状态。
  40. /// </summary>
  41. [Description("获取或设置标识CheckBox选中状态。"), Category("CustomerEx")]
  42. [DefaultValue(typeof(FlagCheckBoxChecked), "None")]
  43. public FlagCheckBoxChecked FlagBoxChecked
  44. {
  45. get
  46. {
  47. int flag = -1;
  48. if (this.chkNo.Checked)
  49. {
  50. flag += 1;
  51. }
  52. if (this.chkYes.Checked)
  53. {
  54. flag += 2;
  55. }
  56. return (FlagCheckBoxChecked)flag;
  57. }
  58. set
  59. {
  60. switch (value)
  61. {
  62. case FlagCheckBoxChecked.All:
  63. this.chkYes.Checked = true;
  64. this.chkNo.Checked = true;
  65. break;
  66. case FlagCheckBoxChecked.Yes:
  67. this.chkYes.Checked = true;
  68. this.chkNo.Checked = false;
  69. break;
  70. case FlagCheckBoxChecked.No:
  71. this.chkYes.Checked = false;
  72. this.chkNo.Checked = true;
  73. break;
  74. default:
  75. if (this._mustInput)
  76. {
  77. this.chkYes.Checked = true;
  78. this.chkNo.Checked = true;
  79. }
  80. else
  81. {
  82. this.chkYes.Checked = false;
  83. this.chkNo.Checked = false;
  84. }
  85. break;
  86. }
  87. }
  88. }
  89. /// <summary>
  90. /// 获取或设置 Yes CheckBox 的文本。
  91. /// </summary>
  92. [Description("获取或设置 Yes CheckBox 的文本。"), Category("CustomerEx")]
  93. [DefaultValue("是")]
  94. public string TextYes
  95. {
  96. get
  97. {
  98. return this.chkYes.Text;
  99. }
  100. set
  101. {
  102. this.chkYes.Text = value;
  103. }
  104. }
  105. /// <summary>
  106. /// 获取或设置 No CheckBox 的文本。
  107. /// </summary>
  108. [Description("获取或设置 No CheckBox 的文本。"), Category("CustomerEx")]
  109. [DefaultValue("否")]
  110. public string TextNo
  111. {
  112. get
  113. {
  114. return this.chkNo.Text;
  115. }
  116. set
  117. {
  118. this.chkNo.Text = value;
  119. }
  120. }
  121. #endregion
  122. #region 重写属性
  123. /// <summary>
  124. /// 获取或设置一个值,该值指示是否自动调整控件的大小以完整显示其内容
  125. /// </summary>
  126. [DefaultValue(true)]
  127. public override bool AutoSize
  128. {
  129. get
  130. {
  131. return this._autoSize;
  132. //return base.AutoSize;
  133. }
  134. set
  135. {
  136. if (this._autoSize != value)
  137. {
  138. this._autoSize = value;
  139. if (this._autoSize)
  140. {
  141. this.SetStyle(ControlStyles.FixedHeight, true);
  142. this.SetStyle(ControlStyles.FixedWidth, true);
  143. }
  144. else
  145. {
  146. this.SetStyle(ControlStyles.FixedHeight, false);
  147. this.SetStyle(ControlStyles.FixedWidth, false);
  148. }
  149. //base.AutoSize = value;
  150. this.AdjustSize();
  151. }
  152. }
  153. }
  154. /// <summary>
  155. /// 获取或设置控件的背景色
  156. /// </summary>
  157. [DefaultValue(typeof(Color), "Transparent")]
  158. public override Color BackColor
  159. {
  160. get
  161. {
  162. return base.BackColor;
  163. }
  164. set
  165. {
  166. base.BackColor = value;
  167. }
  168. }
  169. #endregion
  170. #region 重写事件
  171. /// <summary>
  172. ///
  173. /// </summary>
  174. /// <param name="e"></param>
  175. protected override void OnResize(System.EventArgs e)
  176. {
  177. base.OnResize(e);
  178. this.AdjustSize();
  179. }
  180. /// <summary>
  181. ///
  182. /// </summary>
  183. /// <param name="e"></param>
  184. protected override void OnSizeChanged(System.EventArgs e)
  185. {
  186. base.OnSizeChanged(e);
  187. this.AdjustSize();
  188. }
  189. #endregion
  190. #region 事件处理
  191. /// <summary>
  192. /// 显示文本改变,导致控件大小改变
  193. /// </summary>
  194. /// <param name="sender"></param>
  195. /// <param name="e"></param>
  196. private void chkYes_SizeChanged(object sender, EventArgs e)
  197. {
  198. this.AdjustSize();
  199. }
  200. /// <summary>
  201. /// 显示文本改变,导致控件大小改变
  202. /// </summary>
  203. /// <param name="sender"></param>
  204. /// <param name="e"></param>
  205. private void chkNo_SizeChanged(object sender, EventArgs e)
  206. {
  207. this.AdjustSize();
  208. }
  209. /// <summary>
  210. /// 选中状态改变
  211. /// </summary>
  212. /// <param name="sender"></param>
  213. /// <param name="e"></param>
  214. private void chkYes_CheckedChanged(object sender, EventArgs e)
  215. {
  216. if (this._mustInput &&
  217. !this.chkYes.Checked &&
  218. !this.chkNo.Checked)
  219. {
  220. this.chkYes.Checked = true;
  221. }
  222. }
  223. /// <summary>
  224. /// 选中状态改变
  225. /// </summary>
  226. /// <param name="sender"></param>
  227. /// <param name="e"></param>
  228. private void chkNo_CheckedChanged(object sender, EventArgs e)
  229. {
  230. if (this._mustInput &&
  231. !this.chkYes.Checked &&
  232. !this.chkNo.Checked)
  233. {
  234. this.chkNo.Checked = true;
  235. }
  236. }
  237. #endregion
  238. #region 私有方法
  239. /// <summary>
  240. /// 调整控件尺寸大小
  241. /// </summary>
  242. private void AdjustSize()
  243. {
  244. this.chkYes.Location = new Point(0, 0);
  245. this.chkNo.Location = new Point(this.chkYes.Right, 0);
  246. if (!this._autoSize)
  247. {
  248. return;
  249. }
  250. this._fixedSize = new Size(this.chkNo.Right, this.chkNo.Bottom);
  251. this.Size = this._fixedSize;
  252. }
  253. #endregion
  254. #region IMustInput 成员
  255. #region 成员变量
  256. /// <summary>
  257. /// 获取或设置控件是否是必须输入项目
  258. /// </summary>
  259. private bool _mustInput = false;
  260. /// <summary>
  261. /// 是否显示必须输入项目的提示
  262. /// </summary>
  263. private bool _showMustInputAlert = true;
  264. #endregion
  265. #region 属性
  266. /// <summary>
  267. /// 获取或设置控件是否必须选中项目。
  268. /// </summary>
  269. [Description("获取或设置控件是否必须选中项目。"), Category("IMustInput")]
  270. [DefaultValue(false)]
  271. public bool MustInput
  272. {
  273. get
  274. {
  275. return this._mustInput;
  276. }
  277. set
  278. {
  279. if (this._mustInput != value)
  280. {
  281. this._mustInput = value;
  282. if (value && this.FlagBoxChecked == FlagCheckBoxChecked.None)
  283. {
  284. this.FlagBoxChecked = FlagCheckBoxChecked.All;
  285. }
  286. }
  287. }
  288. }
  289. /// <summary>
  290. /// 获取或设置控件是否显示必须输入项目提示
  291. /// </summary>
  292. [Description("获取或设置控件是否显示必须输入项目提示。"), Category("IMustInput")]
  293. [DefaultValue(true)]
  294. [Bindable(false)]
  295. [Browsable(false)]
  296. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  297. [EditorBrowsable(EditorBrowsableState.Advanced)]
  298. public bool ShowMustInputAlert
  299. {
  300. get
  301. {
  302. return this._showMustInputAlert;
  303. }
  304. set
  305. {
  306. if (this._showMustInputAlert != value)
  307. {
  308. this._showMustInputAlert = value;
  309. }
  310. }
  311. }
  312. #endregion
  313. #region 公有方法
  314. /// <summary>
  315. /// 清除输入项
  316. /// </summary>
  317. public virtual void ClearValue()
  318. {
  319. this.FlagBoxChecked = FlagCheckBoxChecked.None;
  320. }
  321. #endregion
  322. #endregion
  323. #region IAsyncControl 成员
  324. #region 成员变量
  325. /// <summary>
  326. /// 异步处理开始时,控件状态
  327. /// </summary>
  328. private bool _asyncBeginStatus = false;
  329. private bool _asyncBeginFocused = false;
  330. private Control _activeControl = null;
  331. #endregion
  332. #region 公有方法
  333. /// <summary>
  334. /// 开始异步处理
  335. /// </summary>
  336. /// <param name="doFocus">是否处理焦点</param>
  337. public virtual void BeginAsync(ref bool doFocus)
  338. {
  339. this._asyncBeginFocused = false;
  340. //if (doFocus && this.Focused)
  341. if (doFocus && this.ActiveControl != null)
  342. {
  343. this._asyncBeginFocused = true;
  344. this._activeControl = this.ActiveControl;
  345. doFocus = false;
  346. }
  347. this._asyncBeginStatus = this.Enabled;
  348. this.Enabled = false;
  349. }
  350. /// <summary>
  351. /// 结束异步处理
  352. /// </summary>
  353. public virtual void EndAsync()
  354. {
  355. this.Enabled = this._asyncBeginStatus;
  356. if (this._asyncBeginFocused)
  357. {
  358. //this.Focus();
  359. this.ActiveControl = this._activeControl;
  360. this._activeControl = null;
  361. }
  362. }
  363. #endregion
  364. #endregion
  365. }
  366. }