WinForms中自定义DateTime/TimeSpan数据掩码需求
WinForms 实现TimeSpan/DateTime时间部分的自定义输入与显示
针对你需要的24小时制无秒显示(如8:45、17:36)、输入无需冒号(如845→8:45、1736→17:36)且不使用字符串类型的需求,可从以下几个方向实现:
1. 基于MaskedTextBox的自定义输入处理
MaskedTextBox本身支持掩码,但默认的00:00掩码需要手动输入冒号,可通过事件处理自动补全冒号并适配输入长度:
- 设置MaskedTextBox的
Mask为00:00,TextAlign设为Left - 监听
KeyPress事件,处理输入逻辑:private void maskedTextBox1_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar)) { e.Handled = true; return; } var mtb = sender as MaskedTextBox; if (mtb == null) return; var currentText = mtb.Text.Replace(":", ""); if (char.IsDigit(e.KeyChar)) { currentText += e.KeyChar; if (currentText.Length == 3) { mtb.Text = $"{currentText[0]}:{currentText.Substring(1)}"; mtb.SelectionStart = mtb.Text.Length; e.Handled = true; } else if (currentText.Length == 4) { mtb.Text = $"{currentText.Substring(0,2)}:{currentText.Substring(2)}"; mtb.SelectionStart = mtb.Text.Length; e.Handled = true; } } } - 绑定数据时,直接将
TimeSpan或DateTime的时间部分转换后赋值给控件,或从控件文本解析为对应类型。
2. 基于DateTimePicker的自定义格式与输入解析
如果使用DateTime类型,DateTimePicker可以通过自定义格式实现显示,并处理输入解析:
- 设置DateTimePicker的
Format为Custom,CustomFormat为HH:mm,ShowUpDown设为true(可选,方便调整时间) - 监听
KeyPress事件,将输入的纯数字字符串解析为时间:private void dateTimePicker1_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar)) { e.Handled = true; return; } var dtp = sender as DateTimePicker; if (dtp == null) return; if (char.IsDigit(e.KeyChar)) { var inputStr = dtp.Text.Replace(":", "") + e.KeyChar; if (inputStr.Length == 3) { int hour = int.Parse(inputStr[0].ToString()); int minute = int.Parse(inputStr.Substring(1)); dtp.Value = new DateTime(dtp.Value.Year, dtp.Value.Month, dtp.Value.Day, hour, minute, 0); e.Handled = true; } else if (inputStr.Length == 4) { int hour = int.Parse(inputStr.Substring(0,2)); int minute = int.Parse(inputStr.Substring(2)); dtp.Value = new DateTime(dtp.Value.Year, dtp.Value.Month, dtp.Value.Day, hour, minute, 0); e.Handled = true; } } } - 绑定DateTime属性时,直接关联
Value即可,显示会自动按HH:mm格式输出。
3. 自定义TypeConverter实现数据绑定适配
如果需要直接绑定TimeSpan属性,可通过自定义TypeConverter让WinForms自动处理输入和显示:
- 创建自定义TypeConverter类:
public class CustomTimeSpanConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); } public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { if (value is string str) { str = str.Replace(":", ""); if (str.Length == 3) { int hour = int.Parse(str[0].ToString()); int minute = int.Parse(str.Substring(1)); return new TimeSpan(hour, minute, 0); } else if (str.Length == 4) { int hour = int.Parse(str.Substring(0,2)); int minute = int.Parse(str.Substring(2)); return new TimeSpan(hour, minute, 0); } } return base.ConvertFrom(context, culture, value); } public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string) && value is TimeSpan ts) { return ts.ToString("HH\\:mm"); } return base.ConvertTo(context, culture, value, destinationType); } } - 在实体类的TimeSpan属性上标记特性:
[TypeConverter(typeof(CustomTimeSpanConverter))] public TimeSpan WorkTime { get; set; } - 将控件绑定到该属性时,会自动处理输入的纯数字字符串转换为TimeSpan,显示时自动格式化为
HH:mm。
内容的提问来源于stack exchange,提问作者KyloRen
相关产品推荐
相关产品推荐

