/// <summary>
/// 返回不包含被选择部分的字符串
/// </summary>
private string GetNoSelectText()
{
string strText = this.Text ;

strText = this.Text.Substring(0, this.SelectionStart); strText += this.Text.Substring(this.SelectionStart + this.SelectionLength, this.Text.Length - this.SelectionStart - this.SelectionLength); return strText; } /// <summary>
/// 返回除去被选择部分的字符,包含将要输入的字符
/// </summary>
/// <param name="chr">字符</param>
private string GetFullText(char chr)
{
string strText = this.Text ;

strText = this.Text.Substring(0, this.SelectionStart) + chr; strText += this.Text.Substring(this.SelectionStart + this.SelectionLength, this.Text.Length - this.SelectionStart - this.SelectionLength); return strText; }

解决方案 »

  1.   

    /// <summary>
    /// 按键处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void LBLTextDate_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    string sDate = "";
    string sYear = "";
    string sMonth = "";
    string sDay = "";
    int iYear = 0;
    int iMonth = 0;
    int iDay = 0;
    int iL1 = -1;
    int iL2 = -1; // 非数值不可输入
    if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b' && e.KeyChar != '-')
    {
    e.Handled = true;
    return;
    } //输入 - 时,进行自动化处理
    if (e.KeyChar == '-')
    {
    //取得没有包含选择部分的字符串
    sDate = GetNoSelectText(); //取得 - 线所在位置
    iL1 = sDate.IndexOf('-');
    iL2 = sDate.IndexOf('-', iL1 + 1); //不能输入两个以上的 -
    if (iL2 > 0)
    {
    //输入日,如果在上一 - 线后再输入 - 则默认为当天,如果先前输入一位0也默认为当天
    if (this.SelectionStart > iL2)
    {
    sDay = sDate.Substring(iL2 + 1);
    if (sDay.Length == 0 || sDay.Equals("0"))
    {
    this.Text = sDate.Substring(0, iL2 + 1) + DateTime.Now.ToString("dd");
    this.SelectionStart = this.Text.Length;
    }
    } e.Handled = true;
    return;
    } //在末尾输入 -
    if (this.SelectionStart == sDate.Length)
    {  
    //输入年份,没有找到 - 线则认为是在输入年份
    if (iL1 < 0)
    {
    //年份末满4位则自动格式化成4位
    if (sDate.Length < 4)
    {
    this.Text = DateTime.Now.ToString("yyyy").Substring(0, 4 - sDate.Length) + sDate ;
    this.SelectionStart = this.Text.Length;
    return;
    } //当年份输入了4位时,年份值不能为0,不可输入 -
    if (int.Parse(sDate) == 0)
    {
    e.Handled = true;
    return;
    }
    } //输入月份,光标落在第一个 - 线和第二个 - 线之间
    if (iL1 > 0 && iL2 < 0)
    {
    sMonth = sDate.Substring(iL1 + 1); //在上一 - 线后输入 - 则默认为当前月份
    if (sMonth.Length == 0)
    {
    this.Text = sDate + DateTime.Now.ToString("MM"); 
    this.SelectionStart = this.Text.Length;
    return;
    } iMonth = int.Parse(sMonth); //月份值为0时不可输入 - 线
    if (iMonth == 0)
    {
    this.Text = sDate.Substring(0, iL1 + 1) + DateTime.Now.ToString("MM"); 
    this.SelectionStart = this.Text.Length;
    return;
    } //输入的月份只有一位时,自动格式化成两位的月份
    if (sMonth.Length == 1)
    {
    //不为0,则格式化为两位,在前面加入0
    this.Text = sDate.Substring(0, iL1 + 1) + "0" + sMonth;
    this.SelectionStart = this.Text.Length;
    return;
    }
    }
    }
    else  //不在末尾输入 - 线
    {
    //难以意料,与粘贴做相同的处理,输入框失去焦点时,如果非日期则清空
    }
    } if (e.KeyChar >= '0' && e.KeyChar <= '9')
    {
    //返回包含将要输入字符且去除选择部分的字符串
    sDate = GetFullText(e.KeyChar); //取得 - 线所在的位置
    iL1 = sDate.IndexOf('-');
    iL2 = sDate.IndexOf('-',iL1 + 1); //输入年
    if (iL1 < 0 || this.SelectionStart <= iL1)
    {
    //取得年的部分
    if (iL1 > 0)
    {
    sYear = sDate.Substring(0, iL1);
    }
    else
    {
    sYear = sDate;
    } //当输到第4位时,值不能为0
    if (sYear.Length == 4)
    {
    if (int.Parse(sYear) == 0)
    {
    e.Handled = true;
    return;
    }
    } //长度不能超过4位
    if (sYear.Length > 4)
    {
    e.Handled = true;
    return;
    } //到此检查年份通过,退出检查
    return;
    } sYear = sDate.Substring(0, iL1);
    iYear = int.Parse(sYear); //取得月的部分
    if (iL2 > 0)
    {
    sMonth = sDate.Substring(iL1 + 1, iL2 - iL1 - 1);
    }
    else
    {
    sMonth = sDate.Substring(iL1 + 1);
    } iMonth = int.Parse(sMonth); //输入月份,长度不大于两位,值不大于12,且不等于0
    if (this.SelectionStart <= iL2 || iL2 < 0)
    {
    //长度不大于两位
    if (sMonth.Length > 2)
    {
    e.Handled = true;
    return;
    } //当输入两位时,值不能大于12,且不能等于0
    if (sMonth.Length == 2 && (iMonth > 12 || iMonth <= 0))
    {
    e.Handled = true;
    return;
    } //到此检查月份通过,退出检查
    return;
    } //输入日,长度不大于2位,且不大于该月的最大日
    if (iL2 > 0 && this.SelectionStart > iL2)
    {
    sDay = sDate.Substring(iL2 + 1);
    iDay = int.Parse(sDay); if (sDay.Length > 2)
    {
    e.Handled = true;
    return;
    } if (iDay <= 0 && sDay.Length == 2)
    {
    e.Handled = true;
    return;
    } if (iMonth > 12)
    {
    return;
    } if (iDay > DateTime.DaysInMonth(iYear, iMonth))
    {
    e.Handled = true;
    return;
    }
    }
    }
    }