FormUtil.cs 3.1 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
using System.Windows.Forms;

namespace OnlineStore.Common
{
    public class FormUtil
    {
        public static int GetIntValue(TextBox txt)
        {
            int value = 0;
            try
            {
                value = int.Parse(txt.Text);
            }
            catch (Exception ex)
            {              
                value = 0;
            }
            return value;
        }
        public static short GetShortValue(TextBox txt)
        {
            short value = 0;
            try
            {
                value = short.Parse(txt.Text);
            }
            catch (Exception ex)
            {
                value = 0;
            }
            return value;
        }

        public static double getDoubleValue(TextBox txt)
        {
            double value = 0;
            try
            {
                value = double.Parse(txt.Text);
            }
            catch (Exception ex)
            {
                value = 0;
            }
            return value;
        }

        public static short get16ShortValue(TextBox txt)
        {
            short value = 0;
            try
            {
                value = System.Convert.ToInt16(txt.Text,16);
            }
            catch
            {
                value = 0;
            }
            return value;
        }

        public static string GetShowStr(double value)
        {
            string wStr = "";
            if (value == (int)value)
            {
                wStr = string.Format("{0:d}", (int)value);
            }
            else
            {
                wStr = string.Format("{0:f}", value);
            }
            return wStr;
        }
        public static string GetSpanStr(TimeSpan span)
        {
            //return Convert.ToDateTime(span.ToString()).ToString("HH:mm:ss");
            string seconds = Math.Round(span.TotalSeconds % 60, 1).ToString();
            if (seconds.IndexOf(".") >= 0)
            {
                seconds = seconds.ToString().PadLeft(4, '0');
            }
            else
            {
                seconds = seconds.ToString().PadLeft(2, '0');
            }
            return span.Hours.ToString().PadLeft(2, '0') + ":" + span.Minutes.ToString().PadLeft(2, '0') + ":" + seconds;
        }

        public static bool GetBoolData(Dictionary<string, string> data, string key, bool defaultValue = false)
        {
            if (data.ContainsKey(key))
            {
                try
                {
                    return Convert.ToBoolean(data[key]);
                }
                catch (Exception ex)
                {

                }
            }
            return defaultValue;
        }
        public static int GetIntData(Dictionary<string, string> data, string key, int defaultValue = 0)
        {
            if (data.ContainsKey(key))
            {
                try
                {
                    return Convert.ToInt32(data[key]);
                }
                catch (Exception ex)
                {

                }
            }
            return defaultValue;
        }
    }
}