Example #1
0
 //Check mandatory textbox or not
 protected override void OnLeave(EventArgs e)
 {
     base.OnLeave(e);
     if (mtry == true)
     {
         if (this.Text.Length == 0)
         {
             if (errmsg != "")
             {
                 MessageBox.Show(errmsg, msgboxtitle);
                 this.Focus();
             }
             else
             {
                 MMessageBox.Show(this, "Bạn phải nhập giá trị!", "Thông báo");
             }
         }
     }
 }
        public void Validate()
        {
            if (Text.IndexOf("/") > -1)
            {
                Text = Text.Replace("/", "");
            }

            String tmp = Text;

            if (Text.Length == 8)
            {
                try
                {
                    new DateTime(Int32.Parse(Text.Substring(4, 4))
                                 , Int32.Parse(Text.Substring(2, 2))
                                 , Int32.Parse(Text.Substring(0, 2)));

                    int year  = Convert.ToInt32(Text.Substring(4, 4));
                    int month = Convert.ToInt32(Text.Substring(2, 2));
                    int day   = Convert.ToInt32(Text.Substring(0, 2));

                    if (year < _minYear || year > _maxYear ||
                        (year == _maxYear && month > DateTime.Now.Month) ||
                        (year == _maxYear && month == DateTime.Now.Month && day > DateTime.Now.Day))
                    {
                        MMessageBox.Show(this, "Giá trị ngày sinh không hợp lệ!", "Thông báo"
                                         , MMessageBoxButtons.OK, MMessageBoxIcon.Error);
                        Focus();
                        return;
                    }
                    tmp  = tmp.Insert(2, "/");
                    tmp  = tmp.Insert(5, "/");
                    Text = tmp;
                }
                catch (Exception)
                {
                    MMessageBox.Show(this, "Giá trị ngày sinh không hợp lệ!", "Thông báo"
                                     , MMessageBoxButtons.OK, MMessageBoxIcon.Error);
                    Focus();
                    return;
                }
            }
            else if (Text.Length == 6)
            {
                int month = Convert.ToInt32(Text.Substring(2, 2));
                if ((month > 12 && month < 19) || month > 20)
                {
                    MMessageBox.Show(this, "Giá trị ngày sinh không hợp lệ!", "Thông báo"
                                     , MMessageBoxButtons.OK, MMessageBoxIcon.Error);

                    Focus();
                    return;
                }
                string strYear;
                int    year = Convert.ToInt32(Text.Substring(4, 2));
                if (month <= 12)
                {
                    if (year <= Get2SoCuoiCuaNam(DateTime.Now))
                    {
                        strYear = "20" + year;
                    }
                    else
                    {
                        strYear = "19" + year;
                    }

                    if (Convert.ToInt32(strYear) < _minYear || Convert.ToInt32(strYear) > _maxYear)
                    {
                        MMessageBox.Show(this, "Giá trị ngày sinh không hợp lệ!", "Thông báo"
                                         , MMessageBoxButtons.OK, MMessageBoxIcon.Error);

                        Focus();
                        return;
                    }

                    tmp  = Text.Substring(0, 4) + strYear;
                    tmp  = tmp.Insert(2, "/");
                    tmp  = tmp.Insert(5, "/");
                    Text = tmp;
                    return;
                }
                if (month == 19 || month == 20)
                {
                    strYear = month + "" + year;
                    if (Convert.ToInt32(strYear) < _minYear || Convert.ToInt32(strYear) > _maxYear)
                    {
                        MMessageBox.Show(this, "Giá trị ngày sinh không hợp lệ!", "Thông báo"
                                         , MMessageBoxButtons.OK, MMessageBoxIcon.Error);

                        Focus();
                        return;
                    }
                    tmp  = Text.Substring(0, 2) + strYear;
                    tmp  = tmp.Insert(2, "/");
                    Text = tmp;
                    return;
                }
            }
            else if (Text.Length == 4)
            {
                int year = Convert.ToInt32(Text.Substring(0, 4));
                if (year > _minYear && year < _maxYear)
                {
                    return;
                }

                int month = Convert.ToInt32(Text.Substring(0, 2));
                if ((month > 12 && month < 19) || month > 20)
                {
                    MMessageBox.Show(this, "Giá trị ngày sinh không hợp lệ!", "Thông báo"
                                     , MMessageBoxButtons.OK, MMessageBoxIcon.Error);

                    Focus();
                    return;
                }

                string strYear;
                year = Convert.ToInt32(Text.Substring(2, 2));
                if (month <= 12)
                {
                    if (year < Get2SoCuoiCuaNam(DateTime.Now))
                    {
                        strYear = "20" + year;
                    }
                    else
                    {
                        strYear = "19" + year;
                    }

                    if (Convert.ToInt32(strYear) < _minYear || Convert.ToInt32(strYear) > _maxYear)
                    {
                        MMessageBox.Show(this, "Giá trị ngày sinh không hợp lệ!", "Thông báo"
                                         , MMessageBoxButtons.OK, MMessageBoxIcon.Error);

                        Focus();
                        return;
                    }

                    tmp  = Text.Substring(0, 2) + strYear;
                    tmp  = tmp.Insert(2, "/");
                    Text = tmp;
                    return;
                }
            }
            else if (Text.Length > 0)
            {
                MMessageBox.Show(this, "Giá trị ngày sinh không hợp lệ!", "Thông báo"
                                 , MMessageBoxButtons.OK, MMessageBoxIcon.Error);
                Focus();
                return;
            }
        }
Example #3
0
 public virtual void DoF12KeyPress()
 {
     MMessageBox.Show(this, "Phím tắt chưa được cài đặt!", "Thông báo"
                      , MMessageBoxButtons.OK, MMessageBoxIcon.Warning);
 }
Example #4
0
        //Check mandatory textbox or not
        protected override void OnLeave(EventArgs e)
        {
            base.OnLeave(e);

            if (Text.IndexOf("/") > -1)
            {
                Text = Text.Replace("/", "");
            }

            String tmp = Text;

            if (Text.Length == 8)
            {
                try
                {
                    new DateTime(Int32.Parse(Text.Substring(4, 4))
                                 , Int32.Parse(Text.Substring(2, 2))
                                 , Int32.Parse(Text.Substring(0, 2)));
                }
                catch (Exception)
                {
                    MMessageBox.Show(this, "Giá trị ngày tháng không hợp lệ!", "Thông báo"
                                     , MMessageBoxButtons.OK, MMessageBoxIcon.Error);
                    Focus();
                    return;
                }
                int year = Convert.ToInt32(Text.Substring(4, 4));
                if (year < _minYear || year > _maxYear)
                {
                    MMessageBox.Show(this, "Giá trị ngày tháng không hợp lệ!", "Thông báo"
                                     , MMessageBoxButtons.OK, MMessageBoxIcon.Error);
                    Focus();
                    return;
                }
                tmp  = tmp.Insert(2, "/");
                tmp  = tmp.Insert(5, "/");
                Text = tmp;
            }
            else if (Text.Length == 6)
            {
                int month = Convert.ToInt32(Text.Substring(2, 2));
                if ((month > 12 && month < 19) || month > 20)
                {
                    MMessageBox.Show(this, "Giá trị ngày tháng không hợp lệ!", "Thông báo"
                                     , MMessageBoxButtons.OK, MMessageBoxIcon.Error);

                    Focus();
                    return;
                }
                string strYear;
                int    year = Convert.ToInt32(Text.Substring(4, 2));
                if (month <= 12)
                {
                    if (year < Get2SoCuoiCuaNam(DateTime.Now) + 50)
                    {
                        strYear = "20" + year;
                    }
                    else
                    {
                        strYear = "19" + year;
                    }

                    if (Convert.ToInt32(strYear) < _minYear || Convert.ToInt32(strYear) > _maxYear)
                    {
                        MMessageBox.Show(this, "Giá trị ngày tháng không hợp lệ!", "Thông báo"
                                         , MMessageBoxButtons.OK, MMessageBoxIcon.Error);

                        Focus();
                        return;
                    }

                    tmp  = Text.Substring(0, 4) + strYear;
                    tmp  = tmp.Insert(2, "/");
                    tmp  = tmp.Insert(5, "/");
                    Text = tmp;
                    return;
                }
                if (month == 19 || month == 20)
                {
                    strYear = month + "" + year;
                    if (Convert.ToInt32(strYear) < _minYear || Convert.ToInt32(strYear) > _maxYear)
                    {
                        MMessageBox.Show(this, "Giá trị ngày tháng không hợp lệ!", "Thông báo"
                                         , MMessageBoxButtons.OK, MMessageBoxIcon.Error);

                        Focus();
                        return;
                    }
                    tmp  = Text.Substring(0, 2) + strYear;
                    tmp  = tmp.Insert(2, "/");
                    Text = tmp;
                    return;
                }
            }
            else if (Text.Length > 0)
            {
                MMessageBox.Show(this, "Giá trị ngày tháng không hợp lệ!", "Thông báo"
                                 , MMessageBoxButtons.OK, MMessageBoxIcon.Error);
                Focus();
                return;
            }
        }