winform中輸入資料的驗證

iDotNetSpace發表於2009-07-13

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using CrystalDecisions.CrystalReports.Engine;

namespace CRM.BaseClass
{
    class OperateAndValidate
    {
        BaseOperate boperate = new BaseOperate();//宣告BaseOperate類的一個物件,以呼叫其方法

        #region  繫結ComboBox控制元件
        ///


        /// 對ComboBox控制元件進行資料繫結
        ///

        /// SQL語句
        /// 表名
        /// 資料表中欄位名
        /// ComboBox控制元件ID
        public void cboxBind(string P_str_sqlstr, string P_str_table, string P_str_tbMember, ComboBox cbox)
        {
            DataSet myds = boperate.getds(P_str_sqlstr, P_str_table);
            cbox.DataSource = myds.Tables[P_str_table];
            cbox.DisplayMember = P_str_tbMember;
        }
        #endregion

        #region  驗證輸入字串為數字
        ///


        /// 驗證輸入字串為數字
        ///

        /// 輸入字元
        /// 返回一個bool型別的值
        public bool validateNum(string P_str_num)
        {
            return Regex.IsMatch(P_str_num, "^[0-9]*$");
        }
        #endregion

        #region  驗證輸入字串為電話號碼
        ///


        /// 驗證輸入字串為電話號碼
        ///

        /// 輸入字串
        /// 返回一個bool型別的值
        public bool validatePhone(string P_str_phone)
        {
            return Regex.IsMatch(P_str_phone, @"\d{3,4}-\d{7,8}");
        }
        #endregion

        #region  驗證輸入字串為傳真號碼
        ///


        /// 驗證輸入字串為傳真號碼
        ///

        /// 輸入字串
        /// 返回一個bool型別的值
        public bool validateFax(string P_str_fax)
        {
            return Regex.IsMatch(P_str_fax, @"86-\d{2,3}-\d{7,8}");
        }
        #endregion

        #region  驗證輸入字串為郵政編碼
        ///


        /// 驗證輸入字串為郵政編碼
        ///

        /// 輸入字串
        /// 返回一個bool型別的值
        public bool validatePostCode(string P_str_postcode)
        {
            return Regex.IsMatch(P_str_postcode, @"\d{6}");
        }
        #endregion

        #region  驗證輸入字串為E-mail地址
        ///


        /// 驗證輸入字串為E-mail地址
        ///

        /// 輸入字串
        /// 返回一個bool型別的值
        public bool validateEmail(string P_str_email)
        {
            return Regex.IsMatch(P_str_email, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
        }
        #endregion

        #region  驗證輸入字串為網路地址
        ///


        /// 驗證輸入字串為網路地址
        ///

        /// 輸入字串
        /// 返回一個bool型別的值
        public bool validateNAddress(string P_str_naddress)
        {
            return Regex.IsMatch(P_str_naddress, @"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
        }
        #endregion

        #region  自動編號
        ///


        /// 自動編號
        ///

        /// SQL語句
        /// 資料表名
        /// 資料表欄位
        /// 編號前的字串
        /// 編號後面的數字
        /// TextBox控制元件名
        public void autoNum(string P_str_sqlstr,string P_str_table,string P_str_tbColumn,string P_str_codeIndex,string P_str_codeNum,TextBox txt)
        {
            string P_str_Code = "";
            int P_int_Code = 0;
            DataSet myds = boperate.getds(P_str_sqlstr,P_str_table);
            if (myds.Tables[0].Rows.Count == 0)
            {
                txt.Text = P_str_codeIndex + P_str_codeNum;
            }
            else
            {
                P_str_Code = Convert.ToString(myds.Tables[0].Rows[myds.Tables[0].Rows.Count - 1][P_str_tbColumn]);
                P_int_Code = Convert.ToInt32(P_str_Code.Substring(2, 7)) + 1;
                P_str_Code = P_str_codeIndex + P_int_Code.ToString();
                txt.Text = P_str_Code;
            }
        }
        #endregion

        #region  繫結報表
        ///


        /// 繫結報表
        ///

        /// 報表名稱
        /// SQL語句
        /// 返回ReportDocument物件
        public ReportDocument CrystalReports(string P_str_creportName, string P_str_sql)
        {
            ReportDocument reportDocument = new ReportDocument();
            string P_str_creportPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0,
                Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
            P_str_creportPath += @"\SumManage\CReportFile\" + P_str_creportName;
            reportDocument.Load(P_str_creportPath);
            reportDocument.DataDefinition.RecordSelectionFormula = P_str_sql;
            return reportDocument;
        }
        #endregion
    }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-608974/,如需轉載,請註明出處,否則將追究法律責任。

相關文章