RdoFlagRadioBox.cs 10 KB

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