Пример #1
0
 private void ATextBox_KeyDown(object sender, KeyEventArgs e)
 {
     if (e.KeyCode == Keys.Enter)
     {
         BTextBox.Focus();
     }
 }
Пример #2
0
        public static NumericValidation_Result CheckNumericRange(string ParameterName, NumericValidation_DataType dataType, BTextBox bTxtBox, NumericValidation_RangeOption minValueOption, double minValue, NumericValidation_RangeOption maxValueOption, double maxValue, bool ShowMessageBoxOnError)
        {
            MultiLanguageHandler ml = null;

            if (ShowMessageBoxOnError)
            {
                ml = MultiLanguageHandler.getInstance();
            }

            //Check the value is empty.
            if (bTxtBox == null || bTxtBox.Text == null || bTxtBox.Text.Trim() == "")
            {
                bTxtBox.SelectionStart  = 0;
                bTxtBox.SelectionLength = bTxtBox.Text.Length;
                bTxtBox.Focus();
                if (ShowMessageBoxOnError)
                {
                    MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_PRE_SET"), ParameterName));
                }
                return(NumericValidation_Result.Empty);
            }

            //Check the value is non-numeric.
            if (!IsNumeric(bTxtBox.Text))
            {
                bTxtBox.SelectionStart  = 0;
                bTxtBox.SelectionLength = bTxtBox.Text.Length;
                bTxtBox.Focus();
                if (ShowMessageBoxOnError)
                {
                    MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_INFO_075"), ParameterName));
                }
                return(NumericValidation_Result.NonNumeric);
            }

            try
            {
                //Check range of the value.
                if (dataType == NumericValidation_DataType.Double)
                {
                    double doubleValue = double.NaN;

                    try
                    {
                        doubleValue = double.Parse(bTxtBox.Text.Trim());
                    }
                    catch
                    {
                        bTxtBox.SelectionStart  = 0;
                        bTxtBox.SelectionLength = bTxtBox.Text.Length;
                        bTxtBox.Focus();
                        if (ShowMessageBoxOnError)
                        {
                            MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_INFO_075"), ParameterName));
                        }
                        return(NumericValidation_Result.InvalidDataType);
                    }

                    if (minValueOption != NumericValidation_RangeOption.NotInUse)
                    {
                        if (minValueOption == NumericValidation_RangeOption.IncludeValue)
                        {
                            if (doubleValue < minValue)
                            {
                                bTxtBox.SelectionStart  = 0;
                                bTxtBox.SelectionLength = bTxtBox.Text.Length;
                                bTxtBox.Focus();
                                if (ShowMessageBoxOnError)
                                {
                                    if (maxValueOption == NumericValidation_RangeOption.NotInUse)
                                    {
                                        MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_INFO_069"), ParameterName, minValue.ToString()));
                                    }
                                    else
                                    {
                                        MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_INFO_068"), ParameterName, minValue.ToString(), maxValue.ToString()));
                                    }
                                }
                                return(NumericValidation_Result.OutOfRange);
                            }
                        }
                        else
                        {
                            if (doubleValue <= minValue)
                            {
                                bTxtBox.SelectionStart  = 0;
                                bTxtBox.SelectionLength = bTxtBox.Text.Length;
                                bTxtBox.Focus();
                                if (ShowMessageBoxOnError)
                                {
                                    if (maxValueOption == NumericValidation_RangeOption.NotInUse)
                                    {
                                        MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_INFO_069"), ParameterName, minValue.ToString()));
                                    }
                                    else
                                    {
                                        MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_INFO_068"), ParameterName, minValue.ToString(), maxValue.ToString()));
                                    }
                                }
                                return(NumericValidation_Result.OutOfRange);
                            }
                        }
                    }

                    if (maxValueOption != NumericValidation_RangeOption.NotInUse)
                    {
                        if (maxValueOption == NumericValidation_RangeOption.IncludeValue)
                        {
                            if (doubleValue > maxValue)
                            {
                                bTxtBox.SelectionStart  = 0;
                                bTxtBox.SelectionLength = bTxtBox.Text.Length;
                                bTxtBox.Focus();
                                if (ShowMessageBoxOnError)
                                {
                                    if (minValueOption == NumericValidation_RangeOption.NotInUse)
                                    {
                                        MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_INFO_077"), ParameterName, maxValue.ToString()));
                                    }
                                    else
                                    {
                                        MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_INFO_068"), ParameterName, minValue.ToString(), maxValue.ToString()));
                                    }
                                }
                                return(NumericValidation_Result.OutOfRange);
                            }
                        }
                        else
                        {
                            if (doubleValue >= maxValue)
                            {
                                bTxtBox.SelectionStart  = 0;
                                bTxtBox.SelectionLength = bTxtBox.Text.Length;
                                bTxtBox.Focus();
                                if (ShowMessageBoxOnError)
                                {
                                    if (minValueOption == NumericValidation_RangeOption.NotInUse)
                                    {
                                        MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_INFO_077"), ParameterName, maxValue.ToString()));
                                    }
                                    else
                                    {
                                        MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_INFO_068"), ParameterName, minValue.ToString(), maxValue.ToString()));
                                    }
                                }
                                return(NumericValidation_Result.OutOfRange);
                            }
                        }
                    }
                }
                else
                {
                    int intValue = int.MinValue;

                    try
                    {
                        intValue = int.Parse(bTxtBox.Text.Trim());
                    }
                    catch
                    {
                        bTxtBox.SelectionStart  = 0;
                        bTxtBox.SelectionLength = bTxtBox.Text.Length;
                        bTxtBox.Focus();
                        if (ShowMessageBoxOnError)
                        {
                            if (minValueOption == NumericValidation_RangeOption.NotInUse && maxValueOption == NumericValidation_RangeOption.NotInUse)
                            {
                                MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_INFO_078"), ParameterName));
                            }
                            else if (minValueOption == NumericValidation_RangeOption.NotInUse)
                            {
                                MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_INFO_076"), ParameterName, maxValue.ToString()));
                            }
                            else if (maxValueOption == NumericValidation_RangeOption.NotInUse)
                            {
                                MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_INFO_070"), ParameterName, minValue.ToString()));
                            }
                            else
                            {
                                MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_INFO_074"), ParameterName, minValue.ToString(), maxValue.ToString()));
                            }
                        }
                        return(NumericValidation_Result.InvalidDataType);
                    }

                    if (minValueOption != NumericValidation_RangeOption.NotInUse)
                    {
                        if (minValueOption == NumericValidation_RangeOption.IncludeValue)
                        {
                            if (intValue < minValue)
                            {
                                bTxtBox.SelectionStart  = 0;
                                bTxtBox.SelectionLength = bTxtBox.Text.Length;
                                bTxtBox.Focus();
                                if (ShowMessageBoxOnError)
                                {
                                    if (maxValueOption == NumericValidation_RangeOption.NotInUse)
                                    {
                                        MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_INFO_070"), ParameterName, minValue.ToString()));
                                    }
                                    else
                                    {
                                        MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_INFO_074"), ParameterName, minValue.ToString(), maxValue.ToString()));
                                    }
                                }
                                return(NumericValidation_Result.OutOfRange);
                            }
                        }
                        else
                        {
                            if (intValue <= minValue)
                            {
                                bTxtBox.SelectionStart  = 0;
                                bTxtBox.SelectionLength = bTxtBox.Text.Length;
                                bTxtBox.Focus();
                                if (ShowMessageBoxOnError)
                                {
                                    if (maxValueOption == NumericValidation_RangeOption.NotInUse)
                                    {
                                        MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_INFO_070"), ParameterName, minValue.ToString()));
                                    }
                                    else
                                    {
                                        MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_INFO_074"), ParameterName, minValue.ToString(), maxValue.ToString()));
                                    }
                                }
                                return(NumericValidation_Result.OutOfRange);
                            }
                        }
                    }

                    if (maxValueOption != NumericValidation_RangeOption.NotInUse)
                    {
                        if (maxValueOption == NumericValidation_RangeOption.IncludeValue)
                        {
                            if (intValue > maxValue)
                            {
                                bTxtBox.SelectionStart  = 0;
                                bTxtBox.SelectionLength = bTxtBox.Text.Length;
                                bTxtBox.Focus();
                                if (ShowMessageBoxOnError)
                                {
                                    if (minValueOption == NumericValidation_RangeOption.NotInUse)
                                    {
                                        MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_INFO_076"), ParameterName, maxValue.ToString()));
                                    }
                                    else
                                    {
                                        MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_INFO_074"), ParameterName, minValue.ToString(), maxValue.ToString()));
                                    }
                                }
                                return(NumericValidation_Result.OutOfRange);
                            }
                        }
                        else
                        {
                            if (intValue >= maxValue)
                            {
                                bTxtBox.SelectionStart  = 0;
                                bTxtBox.SelectionLength = bTxtBox.Text.Length;
                                bTxtBox.Focus();
                                if (ShowMessageBoxOnError)
                                {
                                    if (minValueOption == NumericValidation_RangeOption.NotInUse)
                                    {
                                        MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_INFO_076"), ParameterName, maxValue.ToString()));
                                    }
                                    else
                                    {
                                        MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_INFO_074"), ParameterName, minValue.ToString(), maxValue.ToString()));
                                    }
                                }
                                return(NumericValidation_Result.OutOfRange);
                            }
                        }
                    }
                }

                return(NumericValidation_Result.OK);
            }
            catch
            {
                bTxtBox.SelectionStart  = 0;
                bTxtBox.SelectionLength = bTxtBox.Text.Length;
                bTxtBox.Focus();
                if (ShowMessageBoxOnError)
                {
                    MSGHandler.DisplayMessage(MSGType.Warning, string.Format(ml.GetMessage("VMS_WRONG"), ParameterName));
                }
                return(NumericValidation_Result.Etc);
            }
        }