winform自定义日期控件,要求可以手动输入日期DatePicker
要求:文本框中能手动输入数字,向上箭头根据鼠标位置给年月日递增,向下箭头递减
一:页面加载时:
- private void FlatDatePicker_Load(object sender, EventArgs e)
- {
- txtMain.Text = DateTime.Now.ToString("MM/dd/yyyy");
- txtMain.Location = new Point(,);
- txtMain.Width = ;
- btnUp.Width = ;
- btnUp.Height = txtMain.Height / ;
- btnDown.Width = ;
- btnDown.Height = txtMain.Height / ;
- this.Width = txtMain.Width + ;
- this.Height = txtMain.Height;
- btnUp.Location = new Point(txtMain.Width, );
- btnDown.Location = new Point(txtMain.Width, btnUp.Height);
- }
二、按下Delete键时不允许删除"/"
- private void txtMain_KeyDown(object sender, KeyEventArgs e)
- {
- switch (e.KeyCode)
- {
- //不允许Delete键删除'/'
- case Keys.Delete:
- int selectIndex = txtMain.SelectionStart;
- int selectLength = txtMain.SelectionLength;
- int xiegangIndex1 = txtMain.Text.IndexOf('/');
- int xiegangIndex2 = txtMain.Text.LastIndexOf('/');
- bool condition1 = selectLength == 0 && (selectIndex == xiegangIndex1 || selectIndex == xiegangIndex2);
- bool condition2 = selectLength > 0 && txtMain.Text.Substring(selectIndex, selectLength).Contains("/");
- if (condition1 || condition2)
- {
- e.Handled = true;
- }
- break;
- default:
- break;
- }
- }
三、按下键时排除不合适字符
- private void txtMain_KeyPress(object sender, KeyPressEventArgs e)
- {
- #region 不合适字符
- if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar) && !Char.IsControl(e.KeyChar))
- {
- e.Handled = true;//消除不合适字符
- }
- else if (Char.IsPunctuation(e.KeyChar))
- {
- if (e.KeyChar != '.' || this.Text.Length == )//小数点
- {
- e.Handled = true;
- }
- if (this.Text.LastIndexOf('.') != -)
- {
- e.Handled = true;
- }
- }
- else if (txtMain.Text == "")
- {
- e.Handled = true;
- }
- #endregion
- #region 按下数字键 数字超过指定的年月日则无效
- if (Char.IsNumber(e.KeyChar))//按下数字键
- {
- int selectedIndex = txtMain.SelectionStart;
- int selectedLength = txtMain.SelectionLength;
- int monthLength = txtMain.Text.Split('/')[].Length;
- int dayLength = txtMain.Text.Split('/')[].Length;
- int yearLength = txtMain.Text.Split('/')[].Length;
- int monthIndex = txtMain.Text.IndexOf('/', , );
- int dayIndex = txtMain.Text.IndexOf('/', );
- int month = DateTime.Now.Month;
- int day = DateTime.Now.Day;
- int year = DateTime.Now.Year;
- if (txtMain.SelectedText.Contains("/"))
- {
- e.Handled = true;
- }
- else if (selectedIndex <= monthIndex)
- {//修改月份
- #region 修改月份
- if (selectedIndex == )
- {
- if (selectedLength == )
- {
- if (monthLength == )
- {
- int.TryParse(e.KeyChar.ToString() + txtMain.Text.Substring(, ), out month);
- }
- else if (monthLength == )
- {
- e.Handled = true;
- }
- }
- else if (selectedLength == )
- {
- if (monthLength == )
- {
- int.TryParse(e.KeyChar.ToString(), out month);
- }
- else if (monthLength == )
- {
- int.TryParse(e.KeyChar.ToString() + txtMain.Text.Substring(, ), out month);
- }
- }
- else if (selectedLength == )
- {
- int.TryParse(e.KeyChar.ToString(), out month);
- }
- }
- else if (selectedIndex == )
- {
- if (selectedLength == )
- {
- if (monthLength == )
- {
- int.TryParse(txtMain.Text.Substring(, ) + e.KeyChar.ToString(), out month);
- }
- else if (monthLength == )
- {
- e.Handled = true;
- }
- }
- else if (selectedLength == )
- {
- int.TryParse(txtMain.Text.Substring(, ) + e.KeyChar.ToString(), out month);
- }
- }
- else if (selectedIndex == )
- {
- e.Handled = true;
- }
- if (month <= || month >= )
- {
- e.Handled = true;
- }
- #endregion
- }
- else if (selectedIndex <= dayIndex)
- {//修改日期
- #region 修改日期
- if (selectedIndex == )
- {
- if (selectedLength == )
- {
- if (dayLength == )
- {
- int.TryParse(e.KeyChar.ToString() + txtMain.Text.Substring(, ), out day);
- }
- else if (dayLength == )
- {
- e.Handled = true;
- }
- }
- else if (selectedLength == )
- {
- if (dayLength == )
- {
- int.TryParse(e.KeyChar.ToString(), out day);
- }
- else if (dayLength == )
- {
- int.TryParse(e.KeyChar.ToString() + txtMain.Text.Substring(, ), out day);
- }
- }
- else if (selectedLength == )
- {
- int.TryParse(e.KeyChar.ToString(), out day);
- }
- }
- else if (selectedIndex == )
- {
- if (selectedLength == )
- {
- if (dayLength == )
- {
- int.TryParse(txtMain.Text.Substring(, ) + e.KeyChar.ToString(), out day);
- }
- else if (dayLength == )
- {
- e.Handled = true;
- }
- }
- else if (selectedLength == )
- {
- int.TryParse(txtMain.Text.Substring(, ) + e.KeyChar.ToString(), out day);
- }
- }
- else if (selectedIndex == )
- {
- e.Handled = true;
- }
- int.TryParse(txtMain.Text.Split('/')[], out month);
- int.TryParse(txtMain.Text.Split('/')[], out year);
- if ((month == || month == || month == || month == || month == || month == || month == ) && day > )
- {
- e.Handled = true;
- }
- else if ((month == || month == || month == || month == ) && day > )
- {
- e.Handled = true;
- }
- if (DateTime.IsLeapYear(year) && month == && day > )
- {
- e.Handled = true;
- }
- else if (!DateTime.IsLeapYear(year) && month == && day > )
- {
- e.Handled = true;
- }
- #endregion
- }
- else
- {//修改年份
- #region 修改年份
- if (yearLength == )
- {
- int.TryParse(txtMain.Text.Substring(, ) + e.KeyChar.ToString(), out year);
- if (year < )
- {
- e.Handled = true;
- //txtMain.Text = txtMain.Text.Split('/')[0] + "/" + txtMain.Text.Split('/')[1] + "/" + DateTime.Now.Year;
- }
- }
- else if (yearLength > && selectedLength <= )
- {
- e.Handled = true;
- }
- #endregion
- }
- }
- #endregion
- #region 按下BackSpcae键 不允许删除"/"
- if (e.KeyChar == '\b')
- {
- int selectIndex = txtMain.SelectionStart;
- int selectlength = txtMain.SelectionLength;
- if (selectlength == && selectIndex > )
- {
- string delStr = txtMain.Text.Substring(selectIndex - , );
- if (delStr == "/")
- {
- e.Handled = true;
- }
- }
- if (selectlength > )
- {
- string delStr = txtMain.Text.Substring(selectIndex, selectlength);
- if (delStr.Contains("/"))
- {
- e.Handled = true;
- }
- }
- }
- #endregion
- }
四、
- private void txtMain_KeyUp(object sender, KeyEventArgs e)
- {
- int selectIndex = txtMain.SelectionStart;
- int month = ;
- int.TryParse(txtMain.Text.Split('/')[], out month);
- int day = ;
- int.TryParse(txtMain.Text.Split('/')[], out day);
- int year = ;
- int.TryParse(txtMain.Text.Split('/')[], out year);
- int xiegangIndex1 = txtMain.Text.IndexOf('/');//第一个'/'的位置
- int xiegangIndex2 = txtMain.Text.LastIndexOf('/');//第二个'/'的位置
- switch (e.KeyCode)
- {
- #region 松开左右键
- case Keys.Left:
- if (selectIndex <= xiegangIndex1)
- {
- FillDay();
- Fillyear();
- }
- else if (selectIndex <= xiegangIndex2)
- {
- FillMonth();
- Fillyear();
- }
- else
- {
- FillMonth();
- FillDay();
- }
- txtMain.SelectionStart = selectIndex;
- break;
- case Keys.Right:
- if (selectIndex <= xiegangIndex1)
- {
- FillDay();
- Fillyear();
- }
- else if (selectIndex <= xiegangIndex2)
- {
- FillMonth();
- Fillyear();
- }
- else
- {
- FillMonth();
- FillDay();
- }
- break;
- #endregion
- case Keys.End:
- break;
- case Keys.Home:
- break;
- #region 上下键增减年月日
- case Keys.Up:
- selectIndex++;
- if (selectIndex < )
- {
- if (month < )
- {
- month++;
- FillMonth(month);
- }
- txtMain.Select(, );
- }
- else if (selectIndex < )
- {
- bool condition1 = (month == || month == || month == || month == || month == || month == || month == ) && day < ;
- bool condition2 = (month == || month == || month == || month == ) && day < ;
- bool condition3 = month == && (DateTime.IsLeapYear(year) && day < || !DateTime.IsLeapYear(year) && day < );
- if (condition1 || condition2 || condition3)
- {
- day++;
- FillDay(day);
- }
- txtMain.Select(, );
- }
- else
- {
- if (year < )
- {
- year++;
- Fillyear(year);
- }
- txtMain.Select(, );
- }
- break;
- case Keys.Down:
- selectIndex--;
- if (selectIndex < )
- {
- if (month > )
- {
- month--;
- FillMonth(month);
- }
- txtMain.SelectionStart = ;
- txtMain.Select(, );
- }
- else if (selectIndex < )
- {
- if (day > )
- {
- day--;
- FillDay(day);
- }
- txtMain.SelectionStart = ;
- txtMain.Select(, );
- }
- else
- {
- if (year > )
- {
- year--;
- Fillyear(year);
- }
- txtMain.SelectionStart = ;
- txtMain.Select(, );
- }
- break;
- #endregion
- case Keys.Delete:
- break;
- #region 松开数字键
- case Keys.D0:
- case Keys.D1:
- case Keys.D2:
- case Keys.D3:
- case Keys.D4:
- case Keys.D5:
- case Keys.D6:
- case Keys.D7:
- case Keys.D8:
- case Keys.D9:
- case Keys.NumPad0:
- case Keys.NumPad1:
- case Keys.NumPad2:
- case Keys.NumPad3:
- case Keys.NumPad4:
- case Keys.NumPad5:
- case Keys.NumPad6:
- case Keys.NumPad7:
- case Keys.NumPad8:
- case Keys.NumPad9:
- if ((month > || selectIndex > ) && txtMain.Text.Split('/')[].Length < || (selectIndex == && txtMain.Text.Split('/')[].Length == ))
- {
- EidtDay(month);
- FillMonth();
- txtMain.Select(, );
- }
- if ((month == && day > || month != && day > || (selectIndex < || selectIndex > )) && txtMain.Text.Split('/')[].Length < || (selectIndex == && txtMain.Text.Split('/')[].Length == ))
- {
- FillDay();
- txtMain.Select(, );
- }
- if (selectIndex > && txtMain.Text.Split('/')[].Length >= )
- {
- EidtDay(month, year);
- Fillyear();
- }
- break;
- #endregion
- default:
- break;
- }
- }
五、
- private void txtMain_Leave(object sender, EventArgs e)
- {
- FillMonth();
- FillDay();
- Fillyear();
- }
六、
- private void txtMain_MouseDown(object sender, MouseEventArgs e)
- {
- FillMonth();
- FillDay();
- Fillyear();
- }
七、
- private void btnUp_Click(object sender, EventArgs e)
- {
- int selectIndex = txtMain.SelectionStart;
- int month = ;
- int.TryParse(txtMain.Text.Split('/')[], out month);
- int day = ;
- int.TryParse(txtMain.Text.Split('/')[], out day);
- int year = ;
- int.TryParse(txtMain.Text.Split('/')[], out year);
- if (selectIndex < )
- {
- if (month < )
- {
- month++;
- FillMonth(month);
- }
- txtMain.Select();
- txtMain.Select(, );
- }
- else if ( <= selectIndex && selectIndex < )
- {
- bool condition1 = (month == || month == || month == || month == || month == || month == || month == ) && day < ;
- bool condition2 = (month == || month == || month == || month == ) && day < ;
- bool condition3 = month == && (DateTime.IsLeapYear(year) && day < || !DateTime.IsLeapYear(year) && day < );
- if (condition1 || condition2 || condition3)
- {
- day++;
- FillDay(day);
- }
- txtMain.Select();
- txtMain.Select(, );
- }
- else if (selectIndex >= )
- {
- if (year < )
- {
- year++;
- Fillyear(year);
- }
- txtMain.Select();
- txtMain.Select(, );
- }
- }
- private void btnDown_Click(object sender, EventArgs e)
- {
- int selectIndex = txtMain.SelectionStart;
- int month = ;
- int.TryParse(txtMain.Text.Split('/')[], out month);
- int day = ;
- int.TryParse(txtMain.Text.Split('/')[], out day);
- int year = ;
- int.TryParse(txtMain.Text.Split('/')[], out year);
- if (selectIndex < )
- {
- if (month > )
- {
- month--;
- FillMonth(month);
- }
- txtMain.Select();
- txtMain.Select(, );
- }
- else if ( <= selectIndex && selectIndex < )
- {
- if (day > )
- {
- day--;
- FillDay(day);
- }
- txtMain.Select();
- txtMain.Select(, );
- }
- else if (selectIndex >= )
- {
- if (year > )
- {
- year--;
- Fillyear(year);
- }
- txtMain.Select();
- txtMain.Select(, );
- }
- }
八、
- private bool Fillyear(int year = )
- {
- bool editBool = false;
- if (year == )
- {
- int.TryParse(txtMain.Text.Split('/')[], out year);
- if (year == )
- {
- year = DateTime.Now.Year;
- editBool = true;
- }
- }
- if (year < && year > )
- {
- txtMain.Text = txtMain.Text.Split('/')[] + "/" + txtMain.Text.Split('/')[] + "/" + DateTime.Now.Year;
- editBool = true;
- }
- else
- {
- txtMain.Text = txtMain.Text.Split('/')[] + "/" + txtMain.Text.Split('/')[] + "/" + year;
- editBool = true;
- }
- return editBool;
- //txtMain.Select(0, 2);
- }
- private bool FillMonth(int month = )
- {
- bool editBool = false;
- if (month == )
- {
- int.TryParse(txtMain.Text.Split('/')[], out month);//txtMain.Text.Substring(0, 2)
- if (month == )
- {
- month = DateTime.Now.Month;
- editBool = true;
- }
- }
- if ( >= month && month > )
- {
- txtMain.Text = "" + month.ToString() + "/" + txtMain.Text.Split('/')[] + "/" + txtMain.Text.Split('/')[];
- editBool = true;
- }
- else
- {
- txtMain.Text = month + "/" + txtMain.Text.Split('/')[] + "/" + txtMain.Text.Split('/')[];
- editBool = true;
- }
- return editBool;
- //txtMain.Select(3, 2);
- }
- private bool FillDay(int day = )
- {
- bool editBool = false;
- if (day == )
- {
- int.TryParse(txtMain.Text.Split('/')[], out day);//txtMain.Text.Substring(0, 2)
- if (day == )
- {
- day = DateTime.Now.Day;
- editBool = true;
- }
- }
- if ( >= day && day > )
- {
- txtMain.Text = txtMain.Text.Split('/')[] + "/" + "" + day.ToString() + "/" + txtMain.Text.Split('/')[];
- editBool = true;
- }
- else
- {
- txtMain.Text = txtMain.Text.Split('/')[] + "/" + day.ToString() + "/" + txtMain.Text.Split('/')[];
- editBool = true;
- }
- return editBool;
- //txtMain.Select(6, 4);
- }
九、
- /// <summary>
- /// 根据年月判断日期是否超出范围 是则修改
- /// </summary>
- private void EidtDay(int month = , int year = )
- {
- if (month == )
- {
- int.TryParse(txtMain.Text.Split('/')[], out month);
- }
- int day = ;
- int.TryParse(txtMain.Text.Split('/')[], out day);
- if (year == )
- {
- int.TryParse(txtMain.Text.Split('/')[], out year);
- }
- if (month == )
- {
- month = DateTime.Now.Month;
- }
- if (day == )
- {
- day = DateTime.Now.Day;
- }
- if (year == )
- {
- year = DateTime.Now.Year;
- }
- if ((month == || month == || month == || month == ) && day > )
- {
- if (month < )
- {
- txtMain.Text = "" + month + "/30/" + year;
- }
else
{
txtMain.Text = month + "/30/" + year;
}
}
- if (DateTime.IsLeapYear(year) && month == && day > )
- {
- txtMain.Text = "" + month + "/29/" + year;
- }
- if (!DateTime.IsLeapYear(year) && month == && day > )
- {
- txtMain.Text = "" + month + "/28/" + year;
- }
- }
winform自定义日期控件,要求可以手动输入日期DatePicker的更多相关文章
- 根据条件决定My97DatePicker日期控件弹出的日期格式
代码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...
- Winform自定义键盘控件开发及使用
最近有学员提出项目中要使用键盘控件,系统自带的osk.exe不好用,于是就有了下面的内容: 首先是进行自定义键盘控件的开发,其实核心大家都知道,就是利用SendKeys.Send发送相应 的字符,但是 ...
- WinForm自定义验证控件
本文转载:http://blog.csdn.net/ziyouli/article/details/7583824 此篇博文不错:http://blog.csdn.net/sony0732/artic ...
- Winform自定义分页控件的实现
实现效果 有点丑陋 但是功能是没问题的 测试过 实现思路 先创建一个用户控件 代码实现 public partial class PagerControl : UserControl { ; /// ...
- winform 自定义分页控件 及DataGridview数据绑定
分页效果如上图所示,用到的控件均为基本控件 ,其方法如下 右击项目-添加-新建项 选择用户控件 然后在用户控件中拖入所需要的Label,Button,Text 用户控件全部代码: using Syst ...
- winform自定义分页控件
1.控件代码: public partial class PagerControl : UserControl { #region 构造函数 public PagerControl() { Initi ...
- C# winform自定义Label控件使其能设置行距
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- WINFORM 自定义开关按钮控件-
本文章转载:http://www.cnblogs.com/feiyangqingyun/archive/2013/06/15/3137597.html OK,大工告成,上图演示效果. 源码下载:htt ...
- FineUIPro v5.2.0已发布(jQuery升级,自定义图标,日期控件)
FineUIPro/MVC/Core/JS v5.2.0 已经于 2018-8-20 发布,官网示例已更新,如果大家在测试中发现任何问题,请回复本帖,谢谢了. 在线示例: FineUI Pro:htt ...
- 【经验】Angularjs 中使用 layDate 日期控件
layDate 控件地址:http://laydate.layui.com/ 前情:原来系统中使用的日期控件是UI bootstrap(地址:https://angular-ui.github.io/ ...
随机推荐
- Spring-Context之六:基于Setter方法进行依赖注入
上文讲了基于构造器进行依赖注入,这里讲解基于Setter方法进行注入.在Java世界中有个约定(Convention),那就是属性的设置和获取的方法名一般是:set+属性名(参数)及get+属性名() ...
- Knockout JS实现任务管理应用程序
1.1.1 摘要 在博文<Ember.js实现单页面应用程序>中,我们介绍了使用Ember JS实现一个单页应用程序 (SPA),这使我想起了几年前写过一个任务管理程序,通过选择日期,然后 ...
- Flash AS实现时钟效果(全脚本实现)
最近工作中用到个Flash效果,好久没有写FlashAS脚本了,就想从以前写的代码中找一些实例.竟然看到硬盘中还留有若干年前的代码. 这个时钟效果是全部采用脚本实现,图形也是用脚本绘制的.写于2005 ...
- Canvas 内部元素添加事件处理
目录 前言 自定义事件 有序数组 元素父类 事件判断 其他 立即执行函数 apply, call, bind addEventListener 传参 调用父类的构造函数 对象检测 isPointInP ...
- PHPer书单
想提升自己,还得多看书!多看书!多看书! 下面是我收集到的一些PHP程序员应该看得书单及在线教程,自己也没有全部看完.共勉吧! Github地址:https://github.com/52fhy/ph ...
- h5原生拖拽
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- FIR.im Weekly - 劳动节我们也没有停下来
五一到五四的节假日对勤劳的开发者们似乎是零存在,各种干货好资源也并未因假期的到来而减少,所以这周的 Weekly 依然多产. Swift 样式指南:2015年4月更新 这是 @开发技术前线 收录的由 ...
- C#与JS实现 获取指定字节长度 中英文混合字符串 的方法
平时在作数据库插入操作时,如果用 INSERT 语句向一个varchar型字段插入内容时,有时会因为插入的内容长度超出规定的长度而报错. 尤其是插入中英文混合字符串时,SQL Server中一般中文要 ...
- 锋利的jQuery——19个jQuery 常用片段整理
/** * Created by yu on 2016/11/20 0020. */// 1.禁用页面右键菜单$(function () { $(document).on('contextmenu', ...
- 使用 CSS3 伪元素实现立体的照片堆叠效
CSS3 里引入的伪元素让 Web 开发人员能够在不需要额外添加 HTML 标签的情况下制作出复杂的视觉效果.例如,:before 和 :after 这个两个 CSS3 伪元素就可以帮助你实现很多有趣 ...