DatePickerCustom.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace CombBoxXX
  10. {
  11. public partial class DatePickerCustom : UserControl
  12. {
  13. private Button btnNull;
  14. private Button btnOK;
  15. private MonthCalendar Calendar;
  16. private Form FrmCalendar;
  17. private string _dttext;
  18. private Panel panel;
  19. private NumericUpDown num1;
  20. private NumericUpDown num2;
  21. /// <summary>
  22. /// 供外部调用获取值
  23. /// </summary>
  24. public string DtText
  25. { get { return _dttext; } set { _dttext = value; } }
  26. private bool _isshowtime = false;
  27. /// <summary>
  28. /// 是否要提供时间
  29. /// </summary>
  30. public bool IsShowTime
  31. { get { return _isshowtime; } set { _isshowtime = value; } }
  32. public DatePickerCustom()
  33. {
  34. InitializeComponent();
  35. #region 添加个层
  36. panel = new Panel();
  37. panel.Size = new Size(120, 25);
  38. Label lbl1 = new Label();
  39. lbl1.Name = "";
  40. lbl1.Text = "时";
  41. lbl1.Width = 15;
  42. lbl1.Location = new Point(0, 5);
  43. num1 = new NumericUpDown();
  44. num1.Name = "numHour";
  45. num1.Size = new Size(35, num1.Height);
  46. num1.Maximum = 23;
  47. num1.Minimum = 0;
  48. num1.Location = new Point(lbl1.Width + 2, 2);
  49. Label lbl2 = new Label();
  50. lbl2.Name = "";
  51. lbl2.Text = "分";
  52. lbl2.Width = 15;
  53. lbl2.Location = new Point(num1.Location.X + num1.Width + 2, lbl1.Location.Y);
  54. num2 = new NumericUpDown();
  55. num2.Name = "numMinute";
  56. num2.Size = new Size(35, num2.Height);
  57. num2.Maximum = 59;
  58. num2.Minimum = 0;
  59. num2.Location = new Point(lbl2.Location.X + lbl2.Width + 2, num1.Location.Y);
  60. panel.Controls.Add(lbl1);
  61. panel.Controls.Add(num1);
  62. panel.Controls.Add(lbl2);
  63. panel.Controls.Add(num2);
  64. #endregion
  65. this.btnOK = new Button();
  66. this.btnOK.Text = "确定";
  67. this.Calendar = new MonthCalendar();
  68. this.Calendar.Visible = true;
  69. this.Calendar.ShowToday = false;
  70. this.Calendar.ShowTodayCircle = true;
  71. this.Calendar.MaxSelectionCount = 1;
  72. this.Calendar.DateSelected += Calendar_DateSelected;
  73. this.textBoxEx1.Leave += textBoxEx1_Leave;
  74. this.textBoxEx1.EventTextBoxEx_TextChanged += textBoxEx1_EventTextBoxEx_TextChanged;
  75. this.Resize += DatePickerCustom_Resize;
  76. this.btnOK.Click += btnOK_Click;
  77. }
  78. /// <summary>
  79. /// 确定
  80. /// </summary>
  81. /// <param name="sender"></param>
  82. /// <param name="e"></param>
  83. void btnOK_Click(object sender, EventArgs e)
  84. {
  85. Calendar_DateSelected(null, null);
  86. }
  87. /// <summary>
  88. /// 窗体大小发生变化
  89. /// </summary>
  90. /// <param name="sender"></param>
  91. /// <param name="e"></param>
  92. void DatePickerCustom_Resize(object sender, EventArgs e)
  93. {
  94. this.textBoxEx1.Width = this.Width;
  95. this.Height = this.textBoxEx1.Height;
  96. this.buttonExpand1.Size = new Size(this.textBoxEx1.Height - 2, this.textBoxEx1.Height - 2);
  97. this.buttonExpand1.Location = new Point(this.textBoxEx1.Width - this.buttonExpand1.Size.Width-1,1);
  98. }
  99. public new Font Font
  100. {
  101. get
  102. {
  103. return _Font;
  104. }
  105. set
  106. {
  107. _Font = value;
  108. this.NewFont = value;
  109. }
  110. }
  111. Font _Font = new Font("宋体", 10.5f);
  112. [Category("控件扩展属性"), Description("设置控件中文本字体")]
  113. public Font NewFont
  114. {
  115. get
  116. {
  117. return _Font;
  118. }
  119. set
  120. {
  121. _Font = value;
  122. this.textBoxEx1.Font = _Font;
  123. this.Height = this.textBoxEx1.Height;
  124. this.buttonExpand1.Size = new Size(this.textBoxEx1.Height - 2, this.textBoxEx1.Height - 2);
  125. this.buttonExpand1.Location = new Point(this.textBoxEx1.Width - this.buttonExpand1.Size.Width-1, 1);
  126. Invalidate();
  127. }
  128. }
  129. /// <summary>
  130. ///
  131. /// </summary>
  132. /// <param name="sender"></param>
  133. /// <param name="e"></param>
  134. void textBoxEx1_EventTextBoxEx_TextChanged(object sender, EventArgs e)
  135. {
  136. this.DtText = this.textBoxEx1.Text.Trim();
  137. if (this.txt_TextChanged != null)
  138. txt_TextChanged(sender, e);
  139. }
  140. #region 定义文本框值更改后触发事件
  141. //定义delegate
  142. public delegate void TextBox_TextChanged(object sender, EventArgs e);
  143. //用event 关键字声明事件对象
  144. /// <summary>
  145. ///
  146. /// </summary>
  147. [Category("控件扩展事件"), Description("文本框值修改时")]
  148. public event TextBox_TextChanged txt_TextChanged;
  149. #endregion
  150. /// <summary>
  151. /// 窗体加载事件
  152. /// </summary>
  153. /// <param name="sender"></param>
  154. /// <param name="e"></param>
  155. private void DatePickerCustom_Load(object sender, EventArgs e)
  156. {
  157. this.buttonExpand1.BackgroundImage = LYFZ.ComponentLibrary.GetUIResources.DateTime;
  158. }
  159. /// <summary>
  160. /// 点击弹出日期框
  161. /// </summary>
  162. /// <param name="sender"></param>
  163. /// <param name="e"></param>
  164. private void buttonExpand1_Click(object sender, EventArgs e)
  165. {
  166. if (this.FrmCalendar != null) {
  167. if (!this.FrmCalendar.Visible) {
  168. this.HideWindos();
  169. this.FrmCalendar.Visible = true;
  170. return;
  171. }
  172. this.FrmCalendar.Visible = false;
  173. }
  174. this.btnNull = new Button();
  175. this.btnNull.Text = "空值";
  176. this.FrmCalendar = new Form();
  177. this.FrmCalendar.FormBorderStyle = FormBorderStyle.None;
  178. this.FrmCalendar.TopMost = true;
  179. this.FrmCalendar.Size = new Size(220, 205);
  180. this.FrmCalendar.Controls.Add(this.Calendar);
  181. this.FrmCalendar.Controls.Add(this.btnNull);
  182. this.FrmCalendar.BackColor = this.Calendar.BackColor;
  183. this.Calendar.Dock = DockStyle.Top;
  184. this.FrmCalendar.StartPosition = FormStartPosition.Manual;
  185. this.FrmCalendar.ShowInTaskbar = false;
  186. this.btnNull.Size = new Size(this.FrmCalendar.Width, 25);
  187. this.btnNull.Location = new Point(0, this.Calendar.Location.Y + this.Calendar.Height + 5);
  188. this.btnNull.Click += btnNull_Click;
  189. if (IsShowTime)
  190. {
  191. this.FrmCalendar.Controls.Add(this.btnOK);
  192. this.btnOK.Size = new Size(50, 25);
  193. this.btnOK.Location = new Point(120, this.Calendar.Location.Y + this.Calendar.Height + 5);
  194. this.btnNull.Size = new Size(50, 25);
  195. this.btnNull.Location = new Point(170, this.Calendar.Location.Y + this.Calendar.Height + 5);
  196. panel.Location = new Point(0, this.Calendar.Location.Y + this.Calendar.Height + 5);
  197. this.FrmCalendar.Controls.Add(panel);
  198. }
  199. else
  200. {
  201. this.btnNull.Size = new Size(this.FrmCalendar.Width, 25);
  202. this.btnNull.Location = new Point(0, this.Calendar.Location.Y + this.Calendar.Height + 5);
  203. }
  204. Point pos = this.Location;
  205. pos.Y = pos.Y + this.Height;
  206. pos = this.Parent.PointToScreen(pos);
  207. pos = GetPoint(pos, this.FrmCalendar);
  208. this.FrmCalendar.SetDesktopLocation(pos.X, pos.Y);
  209. this.FrmCalendar.Show();
  210. if (textBoxEx1.Text.ToString().Trim() != "")
  211. {
  212. Calendar.SelectionStart = Convert.ToDateTime(textBoxEx1.Text);
  213. if (IsShowTime)
  214. {
  215. int hh = Convert.ToDateTime(textBoxEx1.Text).Hour;
  216. int mm = Convert.ToDateTime(textBoxEx1.Text).Minute;
  217. num1.Value = hh;
  218. num2.Value = mm;
  219. }
  220. }
  221. this.textBoxEx1.Focus();
  222. }
  223. /// <summary>
  224. /// 文本框推动焦点时隐藏选择面板
  225. /// </summary>
  226. /// <param name="sender"></param>
  227. /// <param name="e"></param>
  228. void textBoxEx1_Leave(object sender, EventArgs e)
  229. {
  230. this.DtText = this.textBoxEx1.Text.Trim();
  231. if (this.txt_TextChanged != null)
  232. txt_TextChanged(sender, e);
  233. this.HideWindos();
  234. }
  235. /// <summary>
  236. /// 选择日期值隐藏当前控件
  237. /// </summary>
  238. /// <param name="sender"></param>
  239. /// <param name="e"></param>
  240. void Calendar_DateSelected(object sender, DateRangeEventArgs e)
  241. {
  242. string StrDate = this.Calendar.SelectionRange.Start.ToString("yyyy-MM-dd");
  243. if (IsShowTime)
  244. {
  245. string StrHour = "";
  246. string StrMinute = "";
  247. if (this.num1.Value.ToString().Trim().Length > 1)
  248. { StrHour = this.num1.Value.ToString().Trim(); }
  249. else { StrHour = "0" + this.num1.Value.ToString().Trim(); }
  250. if (this.num2.Value.ToString().Trim().Length > 1)
  251. { StrMinute = this.num2.Value.ToString().Trim(); }
  252. else { StrMinute = "0" + this.num2.Value.ToString().Trim(); }
  253. this.textBoxEx1.Text = StrDate + " " + StrHour + ":" + StrMinute;
  254. }
  255. this.textBoxEx1.Text = StrDate;
  256. DtText = textBoxEx1.Text;
  257. this.HideWindos();
  258. }
  259. /// <summary>
  260. /// 空值
  261. /// </summary>
  262. /// <param name="sender"></param>
  263. /// <param name="e"></param>
  264. void btnNull_Click(object sender, EventArgs e)
  265. {
  266. textBoxEx1.Text = "";
  267. this.Calendar.SelectionStart = this.Calendar.TodayDate;
  268. DtText = textBoxEx1.Text;
  269. this.HideWindos();
  270. }
  271. /// <summary>
  272. /// 取得指定控件全部显示时的顶点坐标
  273. /// </summary>
  274. /// <param name="sPos"></param>
  275. /// <param name="ConToShow"></param>
  276. /// <returns></returns>
  277. private Point GetPoint(Point sPos, Control ConToShow)
  278. {
  279. Point NewPos = new Point(sPos.X, sPos.Y);
  280. int iConWidth = ConToShow.Width;
  281. int iConHeight = ConToShow.Height;
  282. int iScrWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
  283. int iScrHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
  284. if (sPos.Y + iConHeight > iScrHeight)
  285. { NewPos.Y = iScrHeight - iConHeight; }
  286. if (sPos.X + iConWidth > iScrWidth)
  287. { NewPos.X = iScrWidth - iConWidth; }
  288. return NewPos;
  289. }
  290. /// <summary>
  291. /// 供外部调用赋值
  292. /// </summary>
  293. /// <param name="strText"></param>
  294. public void TextBoxGiveText(string strText)
  295. {
  296. DtText = strText;
  297. if (DtText != "")
  298. {
  299. if (IsShowTime)
  300. { DtText = Convert.ToDateTime(DtText).ToString("yyyy-MM-dd HH:mm").Trim(); }
  301. else { DtText = Convert.ToDateTime(DtText).ToString("yyyy-MM-dd").Trim(); }
  302. textBoxEx1.Text = DtText;
  303. }
  304. }
  305. /// <summary>
  306. /// 供外部调用,隐藏选择面板
  307. /// </summary>
  308. public void HideWindos()
  309. {
  310. this.FrmCalendar.Hide();
  311. }
  312. }
  313. }