common Convert functions

16/09/2011 11:27

#region CommomMethods

        public static int ConvToInt(string value)
        {
            if (value != string.Empty && value != null)
                return Int32.Parse(value);
            else
                return 0;
        }

        public static DateTime ConvToDT(string value)
        {
            if (value != string.Empty && value != null)
                return DateTime.Parse(string.Format("{0:yy/MM/dd}", value));
            else
                return DateTime.Parse("1/1/01");
        }

        public static string ConvToStringDtTm(string value)
        {

            string[] tempdttmpart = value.ToString().Split(' ');
            string[] datpartonly = tempdttmpart[0].Split('/');
            return datpartonly[2] + "/" + datpartonly[1] + "/" + datpartonly[0] + " " + tempdttmpart[tempdttmpart.Length - 1];


        }
        public static string ConvToStringDt(string value)
        {

            string[] datpartonly = value.Split('/');
            return datpartonly[2] + "/" + datpartonly[1] + "/" + datpartonly[0];

        }

        public static string ConvToStringDT(string value)
        {
            if (value != string.Empty && value != null)
                return DateTime.Parse(value).ToString("yy/MM/dd");
            else
                return "1/1/01";
        }

        public static string GetDate()
        {
            return string.Format("{0:yy/MM/dd}", DateTime.Now);
        }

        public static string GetTime()
        {
            return string.Format("{0:hh:mm:ss}", DateTime.Now);
        }

        public static void NumericOnly(KeyPressEventArgs e)
        {
            try
            {
                string allowedChars = "0123456789";

                if (allowedChars.IndexOf(e.KeyChar) == -1)
                {
                    // Invalid Character
                    e.Handled = true;
                }
            }
            catch
            {

            }
        }

        public static void DecimalOnly(KeyPressEventArgs e)
        {
            try
            {
                string allowedChars = "0123456789.";

                if (allowedChars.IndexOf(e.KeyChar) == -1)
                {
                    // Invalid Character
                    e.Handled = true;
                }
            }
            catch
            {

            }
        }

        public static void DateOnly(KeyPressEventArgs e)
        {
            try
            {
                string allowedChars = "0123456789/";

                if (allowedChars.IndexOf(e.KeyChar) == -1)
                {
                    // Invalid Character
                    e.Handled = true;
                }
            }
            catch
            {

            }
        }

        public static void StringOnly(string allowedChars, KeyPressEventArgs e)
        {
            try
            {

                if (!char.IsLetter(e.KeyChar) || allowedChars.IndexOf(e.KeyChar) == -1)
                {
                    e.Handled = true;
                }
            }
            catch
            {

            }
        }

        static ErrorProvider ep = new ErrorProvider();
        public static bool ValidateEmptyTB(GroupBox gb1, GroupBox gb2)
        {

            bool ErrorinGB1 = false;
            bool ErrorinGB2 = false;

            ep.Clear();

            bool result = false;

            if (gb1 != null)
            {
                foreach (Control ctrl in gb1.Controls)
                {
                    if (ctrl is TextBox)
                    {

                        TextBox txt = (TextBox)ctrl;
                        if (txt.Text == "")
                        {
                            ErrorinGB1 = true;
                            ep.SetError(txt, "Empty TextBox");
                        }
                        else
                        {
                            ep.SetError(txt, "");

                        }

                    }
                    if (ctrl is ComboBox)
                    {
                        ComboBox cmb = (ComboBox)ctrl;

                        if (cmb.SelectedIndex == -1)
                        {
                            ErrorinGB1 = true;
                            ep.SetError(cmb, "Empty TextBox");
                        }
                        else
                        {
                            ep.SetError(cmb, "");

                        }
                    }
                }
            }


            if (gb2 != null)
            {
                foreach (Control ctrl in gb2.Controls)
                {
                    if (ctrl is TextBox)
                    {
                        TextBox txt = (TextBox)ctrl;
                        if (txt.Text == "")
                        {
                            ErrorinGB2 = true;
                            ep.SetError(txt, "Empty TextBox");
                        }
                        else
                        {
                            ep.SetError(txt, "");

                        }

                    }
                    if (ctrl is ComboBox)
                    {
                        ComboBox cmb = (ComboBox)ctrl;

                        if (cmb.SelectedIndex == -1)
                        {
                            ErrorinGB2 = true;
                            ep.SetError(cmb, "Empty TextBox");
                        }
                        else
                        {
                            ep.SetError(cmb, "");

                        }
                    }
                }
            }

            if (ErrorinGB1 == false && ErrorinGB2 == false)
            {
                ep.Dispose();
                ep.Clear();
                return result = true;
            }
            else
                return result = false;
        }


        public static void ClearForm(Control Frm)
        {
            ResetFormControlValues(Frm);
        }

        private static void ResetFormControlValues(Control parent)
        {
            foreach (Control c in parent.Controls)
            {
                if (c.Controls.Count > 0)
                {
                    ResetFormControlValues(c);
                }
                else
                {
                    switch (c.GetType().ToString())
                    {
                        case "System.Windows.Forms.TextBox":
                            ((TextBox)c).Text = "";
                            break;
                        //case "System.Windows.Forms.CheckBox":
                        //    ((CheckBox)c).Checked = false;
                        //    break;
                        case "System.Windows.Forms.ComboBox":
                            if (((ComboBox)c).Items.Count > 0)
                                ((ComboBox)c).SelectedIndex = -1;
                            break;

                    }
                }
            }

        }





        #endregion