📄 dateedit.cs
字号:
{
if (this.ContainsFocus)
{
SendKeys.Send("{Right}");
_isDelete = true;
return true;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
/// <summary>
/// 获取输入的数字应该在的有效位置
/// </summary>
/// <param name="selectIndex"></param>
/// <param name="inputValue"></param>
/// <returns>返回空-1;表示操作可以继续,否则将返回的串替换将要更新的串</returns>
private int getPlace(int selectIndex, int inputValue)
{
if (selectIndex == -1 || selectIndex > this.Text.Length - 1)
return -1;
string text = this.Text;
if (selectIndex == 4 || selectIndex == 7)
selectIndex++;
if (selectIndex == 5 || selectIndex == 6)
{
return getPlaceBySection(5, 12, text, selectIndex, inputValue);
}
else
{
if (selectIndex == 8 || selectIndex == 9)
{
return getPlaceBySection(8, 31, text, selectIndex, inputValue);
}
//当前时间格式是“yyyy-MM-dd HH:mm”
if (_isLongType)
{
if (selectIndex == 10 || selectIndex == 13)
selectIndex++;
if (selectIndex == 11 || selectIndex == 12)
{
return getPlaceBySection(11, 23, text, selectIndex, inputValue);
}
if (selectIndex == 14 || selectIndex == 15)
{
return getPlaceBySection(14, 59, text, selectIndex, inputValue);
}
}
return -1;
}
}
/// <summary>
/// 根据时间格式中不同部分段的最大值,返回有效的位置
/// </summary>
/// <param name="start"></param>
/// <param name="MaxValue"></param>
/// <param name="text"></param>
/// <param name="selectIndex"></param>
/// <param name="inputValue"></param>
/// <returns></returns>
private int getPlaceBySection(int start, int MaxValue, string text, int selectIndex, int inputValue)
{
string temp = text;
temp = temp.Remove(selectIndex, 1);
temp = temp.Insert(selectIndex, inputValue.ToString());
temp = temp.Substring(start, 2);
if (temp[0] == '0')
temp = temp.Remove(0, 1);
if (Int16.Parse(temp) <= MaxValue)
return -1;
else
{
if (selectIndex == start)
{
temp = text;
temp = temp.Remove(start, 2);
temp = temp.Insert(start, "0");
temp = temp.Insert(start, "0");
this.Text = temp;
start++;
return start;
}
if (selectIndex == start+1)
{
temp = text;
temp = temp.Remove(start, 2);
temp = temp.Insert(start, "0");
temp = temp.Insert(start, "0");
this.Text = temp;
return selectIndex;
}
else
{
return -2;//表示应该到下一个有效位置
}
}
}
/// <summary>
/// 获取数字键对应的数字
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
private int getIntByCode(KeyEventArgs e)
{
int key = -1;
switch (e.KeyCode)
{
case Keys.D0:
case Keys.NumPad0:
key = 0;
break;
case Keys.D1:
case Keys.NumPad1:
key = 1;
break;
case Keys.D2:
case Keys.NumPad2:
key = 2;
break;
case Keys.D3:
case Keys.NumPad3:
key = 3;
break;
case Keys.D4:
case Keys.NumPad4:
key = 4;
break;
case Keys.D5:
case Keys.NumPad5:
key = 5;
break;
case Keys.D6:
case Keys.NumPad6:
key = 6;
break;
case Keys.D7:
case Keys.NumPad7:
key = 7;
break;
case Keys.D8:
case Keys.NumPad8:
key = 8;
break;
case Keys.D9:
case Keys.NumPad9:
key = 9;
break;
default:
break;
}
return key;
}
/// <summary>
/// 将选中的字符串重置为默认值(提示符的值)
/// </summary>
/// <param name="startIndex">字符串的起始字符的位置</param>
/// <param name="length">maskedTextbox的Text的长度</param>
/// <returns></returns>
private string removeString(int startIndex, int length)
{
string text = this.Text;
int indexTemp = startIndex;
int selectLength = this.SelectionLength;
for (int index = 0; index < selectLength; index++)
{
if (checkChar(indexTemp))
{
text = text.Remove(indexTemp, 1);
text = text.Insert(indexTemp, "0");
}
indexTemp++;
if (indexTemp >= length)
break;
}
return text;
}
/// <summary>
/// 检查指定位置字符是否是有效数字
/// </summary>
/// <param name="index">指定位置</param>
/// <returns>true为有效字符,false为非法字符</returns>
private bool checkChar(int index)
{
if (index < 0 || index > this.Text.Length)
return false;
Regex regex = new Regex(@"^[0-9]$");
string oneChar = this.Text[index].ToString();
if (!regex.IsMatch(oneChar))
return false;
else
return true;
}
/// <summary>
/// 获取用户输入的字符个数
/// </summary>
/// <param name="msktbText">maskedTextbox</param>
/// <returns>用户输入的字符个数</returns>
public int getInputCount(MaskedTextBox msktbText)
{
if (msktbText == null)
return -1;
MaskedTextBox msktbMy = new MaskedTextBox();
msktbMy.Mask = msktbText.Mask;
msktbMy.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
msktbMy.PromptChar = '0';
msktbMy.Text = msktbText.Text;
return msktbMy.Text.Length;
}
/// <summary>
/// 获取指定日期的完整时间字符串
/// </summary>
/// <param name="dt">指定日期</param>
/// <returns>指定日期的完整时间字符串</returns>
public String getTime(DateTime dt)
{
DateTime dtTemp;
DateTime dtNow = DateTime.Now;
dtTemp = dt.Add(new TimeSpan(dtNow.Hour, dtNow.Minute, dtNow.Second));
return dtTemp.ToString("yyyy-MM-dd HH:mm:ss");
}
#endregion
}
/// <summary>
/// Mask格式种类枚举
/// </summary>
public enum MaskType
{
/// <summary>
/// Mask格式为"yyyy-MM-dd HH:mm"
/// </summary>
Long,
/// <summary>
/// Mask格式为"yyyy-MM-dd"
/// </summary>
Short
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -