标签:
方式一、基于DEV TextEdit控件实现时分秒控件
public virtual void textEdit1_FormatEditValue(object sender, DevExpress.XtraEditors.Controls.ConvertEditValueEventArgs e) { TextEdit objectControl = sender as TextEdit; int hour = 0; int minute = 0; int seconds = 0; if (e.Value != null && !string.IsNullOrEmpty(e.Value.ToString())) { if (!e.Value.ToString().Contains(":") || e.Value.Equals("0")) { GetHourMinutSeconds(ref hour, ref minute, ref seconds, Convert.ToInt32(e.Value)); e.Value = hour.ToString().PadLeft(4, ‘0‘) + ":" + minute.ToString().PadLeft(2, ‘0‘) + ":" + seconds.ToString().PadLeft(2, ‘0‘); } else { string[] hourArr = e.Value.ToString().Split(‘:‘); if ((hourArr[2]!=null && Convert.ToInt32(hourArr[2]) > 59) || (hourArr[1]!=null && Convert.ToInt32(hourArr[1]) > 59)) { GetHourMinutSeconds(ref hour, ref minute, ref seconds,e.Value); if (hour < 10000) { //时间不能大于9999:59:59 e.Value = FormatTime(hour, minute, seconds); } } } } else { e.Value = "0000:00:00"; } objectControl.Properties.Mask.EditMask = @"((\d[0-9]{3}):[0-9]?\d:([0-9]?\d))"; objectControl.Properties.Mask.MaskType = MaskType.RegEx; } /// <summary> /// 获取时分秒 /// </summary> /// <param name="hour">时</param> /// <param name="minute">分</param> /// <param name="seconds">秒</param> /// <param name="initializeHour">初始时间</param> private void GetHourMinutSeconds(ref int hour, ref int minute, ref int seconds, int initializeHour) { TimeSpan ts = new TimeSpan(0,0,initializeHour); if (ts.Days > 0) hour = ts.Days * 24 + ts.Hours; else hour = ts.Hours; minute = ts.Minutes; seconds = ts.Seconds; } /// <summary> /// 获取时分秒 /// </summary> /// <param name="hour">时</param> /// <param name="minute">分</param> /// <param name="seconds">秒</param> /// <param name="initializeHour">初始时间</param> private void GetHourMinutSeconds(ref int hour, ref int minute, ref int seconds, object initializeHour) { if (initializeHour != null) { string[] hourArr = initializeHour.ToString().Split(‘:‘); TimeSpan ts = new TimeSpan(Convert.ToInt32(hourArr[0]), Convert.ToInt32(hourArr[1]), Convert.ToInt32(hourArr[2])); if (ts.Days > 0) hour = ts.Days * 24 + ts.Hours; else hour = ts.Hours; minute = ts.Minutes; seconds = ts.Seconds; }else{ hour = 0; minute = 0; seconds = 0; } } /// <summary> /// 格式化时间 /// </summary> /// <param name="hour"></param> /// <param name="minute"></param> /// <param name="seconds"></param> /// <returns></returns> private string FormatTime(int hour, int minute, int seconds) { return hour.ToString().PadLeft(4, ‘0‘) + ":" + minute.ToString().PadLeft(2, ‘0‘) + ":" + seconds.ToString().PadLeft(2, ‘0‘); } /// <summary> /// 获得总的时间(秒) /// </summary> /// <param name="initializeHour"></param> /// <returns></returns> private object GetSumSeconds(object initializeHour) { int seconds = 0; if (initializeHour != null) { string[] hourArr = initializeHour.ToString().Split(‘:‘); TimeSpan ts = new TimeSpan(Convert.ToInt32(hourArr[0]),Convert.ToInt32(hourArr[1]), Convert.ToInt32(hourArr[2])); seconds = (Int32)ts.TotalSeconds; } return seconds; }
方式二、使用key_down,key_press,key_up事件实现时分秒控件(该方式控制光标显示不友好)
#region 按键事件 /// <summary> /// 让删除键不能删除冒号 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void HourText_KeyDown(object sender, KeyEventArgs e) { if (e.KeyData == Keys.Delete) { DevExpress.XtraEditors.TextEdit editTxt = (DevExpress.XtraEditors.TextEdit)(sender); char[] txtArray = editTxt.Text.ToCharArray(); if (editTxt.SelectedText.Contains(":") || (editTxt.SelectionStart < txtArray.Length && txtArray[editTxt.SelectionStart].Equals(‘:‘))) { e.Handled = true; } } } int oldSelectionStart = -1;//记录keyPress事件中的光标的起始位置 string newEditTxt = string.Empty;//记录当光标没有选中字符,则替换掉同光标的位置的字符后的新的字符串 /// <summary> /// 控制修改后的时间符合标准格式 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void HourText_KeyPress(object sender, KeyPressEventArgs e) { DevExpress.XtraEditors.TextEdit editTxt = (DevExpress.XtraEditors.TextEdit)(sender); if ((int)e.KeyChar >= 48 & (int)e.KeyChar <= 57 | (int)e.KeyChar == 8) { char[] txtArray = editTxt.Text.ToCharArray(); if (editTxt.SelectedText.Contains(":") || ((int)e.KeyChar == 8 && (editTxt.SelectionStart - 1 >= 0) && (txtArray[editTxt.SelectionStart - 1].Equals(‘:‘) && editTxt.SelectionLength == 0))) { e.Handled = true; } else { if ((int)e.KeyChar >= 48 && (int)e.KeyChar <= 57) {//当光标没有选中字符,则替换掉同光标的位置的字符 oldSelectionStart = editTxt.SelectionStart; string[] txtArr = null; string editTxtTemp = string.Empty; if (editTxt.SelectionLength == 0) { if (editTxt.SelectionStart < txtArray.Length && (!‘:‘.Equals(txtArray[editTxt.SelectionStart]))) { txtArray[editTxt.SelectionStart] = e.KeyChar; editTxtTemp = new string(txtArray); } else if (editTxt.SelectionStart < txtArray.Length && ‘:‘.Equals(txtArray[editTxt.SelectionStart])) { txtArr = editTxt.Text.Split(‘:‘); bool isSelectFirstColon = false;//记录光标是否停留在第一个冒号的位置 if (editTxt.SelectionStart.Equals(editTxt.Text.IndexOf(‘:‘))) { isSelectFirstColon = true;//表示光标停留在第一个冒号的位置 } else { isSelectFirstColon = false; } if (isSelectFirstColon && txtArr[0].Length < 4) { txtArr[0] = txtArr[0].ToStringExtension() + e.KeyChar.ToStringExtension(); } else { if (txtArr[1].Length <= 1) { if (Maths.IsEmpty(txtArr[1])) { txtArr[1] = e.KeyChar.ToStringExtension(); } else { txtArr[1] = txtArr[1].ToStringExtension() + e.KeyChar.ToStringExtension(); } } } editTxtTemp = txtArr[0].ToStringExtension() + ":" + txtArr[1].ToStringExtension() + ":" + txtArr[2].ToStringExtension(); } if (editTxt.SelectionStart == txtArray.Length) { editTxtTemp = editTxt.Text + e.KeyChar; } txtArr = editTxtTemp.ToStringExtension().Split(‘:‘); if (txtArr.Count() == 3) { if ((!Maths.IsEmpty(txtArr[0]) && txtArr[0].Length > 4) || (!Maths.IsEmpty(txtArr[1]) && (Convert.ToInt32(txtArr[1]) >= 60 || txtArr[1].Length > 2)) || (!Maths.IsEmpty(txtArr[2]) && (Convert.ToInt32(txtArr[2]) >= 60 || txtArr[2].Length > 2))) { e.Handled = true; oldSelectionStart = -1;//清空光标位置 } else { newEditTxt = editTxtTemp; e.Handled = false; } } else { e.Handled = true; oldSelectionStart = -1;//清空光标位置 } } else { editTxtTemp = editTxt.Text.Remove(editTxt.SelectionStart, editTxt.SelectionLength).Insert(editTxt.SelectionStart, e.KeyChar.ToString()); txtArr = editTxtTemp.ToStringExtension().Split(‘:‘); if (txtArr.Count() == 3) { if ((!Maths.IsEmpty(txtArr[0]) && txtArr[0].Length > 4) || (!Maths.IsEmpty(txtArr[1]) && (Convert.ToInt32(txtArr[1]) >= 60 || txtArr[1].Length > 2)) || (!Maths.IsEmpty(txtArr[2]) && (Convert.ToInt32(txtArr[2]) >= 60 || txtArr[2].Length > 2))) { e.Handled = true; } else { newEditTxt = editTxtTemp; e.Handled = false; } } else { e.Handled = true; oldSelectionStart = -1;//清空光标位置 } } } else { e.Handled = false; } } } else { e.Handled = true; oldSelectionStart = -1;//清空光标位置 } } private void HourText_EditValueChanging(object sender, EventArgs e) { if (!Maths.IsEmpty(newEditTxt)) { ((DevExpress.XtraEditors.Controls.ChangingEventArgs)(e)).NewValue = newEditTxt; } newEditTxt = string.Empty; } /// <summary> /// 改变光标位置 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void HourText_KeyUp(object sender, KeyEventArgs e) { if (oldSelectionStart >= 0 && e.KeyValue != 37 && e.KeyValue != 39) { ((DevExpress.XtraEditors.TextEdit)(sender)).SelectionStart = oldSelectionStart + 1; } oldSelectionStart = -1;//清空光标位置 } #endregion
欢迎广大园有提出其他实现方式!
标签:
原文地址:http://www.cnblogs.com/qianlilandu/p/5152698.html