FormUtil.cs 5.2 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;

namespace URSoldering.Common
{
    public class FormUtil
    { 
        /// <summary> 
      /// 计算某日结束日期(礼拜日的日期) 
      /// </summary> 
      /// <param name="someDate">该周中任意一天</param> 
      /// <returns>返回礼拜日日期,后面的具体时、分、秒和传入值相等</returns> 
        public static DateTime GetSundayDate(DateTime someDate)
        {
            int i = someDate.DayOfWeek - DayOfWeek.Sunday;
            if (i != 0) i = 7 - i;// 因为枚举原因,Sunday排在最前,相减间隔要被7减。 
            TimeSpan ts = new TimeSpan(i, 0, 0, 0);
            DateTime time= someDate.Add(ts);
            return new DateTime(time.Year, time.Month, time.Day, 23, 59, 59);
        }
        public static DateTime GetMondayDate(DateTime someDate)
        {
            int i = someDate.DayOfWeek - DayOfWeek.Monday;
            if (i == -1) i = 6;// i值 > = 0 ,因为枚举原因,Sunday排在最前,此时Sunday-Monday=-1,必须+7=6。   
            TimeSpan ts = new TimeSpan(i, 0, 0, 0);
            DateTime time= someDate.Subtract(ts);
            return new DateTime(time.Year, time.Month, time.Day, 0, 0, 1);
        }
        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 ushort GetUShortValue(TextBox txt)
        {
            ushort value = 0;
            try
            {
                value = Convert.ToUInt16(txt.Text); 
            }
            catch (Exception ex)
            {
                value = 0;
            }
            return value;
        }

        public static int GetIntValue(Label txt)
        {
            int value = 0;
            try
            {
                value = int.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 double getDoubleValue(Label 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 bool IsIp(string ip)
        {
            string[] ipstr = ip.Split('.');

            if (ipstr.Length == 4)
            {
                foreach (string str in ipstr)
                {
                    int count = 0;
                    try
                    {
                        count = Convert.ToInt32(str);
                    }
                    catch (Exception ex)
                    {
                        return false;
                    }
                    if (count <0 || count > 255)
                    {
                        return false;
                    }
                }

                return true;
            }
            else
            {
                return false;
            }
        }

        public static string getValue(TextBox txtBoardName)
        {
            return txtBoardName.Text.Trim();
        }
        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;
        }
    }
}