WinForm BaseClass类常用通用方法
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Data; 6 using System.Security.Cryptography; 7 8 namespace HRPOWER.BLL 9 { 10 /// <summary> 11 /// 业务逻辑层基类 12 /// </summary> 13 public class BaseClass 14 { 15 /// <summary> 16 /// 屏幕宽度 17 /// </summary> 18 private static Decimal WidthPercent; 19 20 /// <summary> 21 /// 屏幕高度 22 /// </summary> 23 private static Decimal HeightPercent; 24 25 26 /// <summary> 27 /// 根据列名字符串数组,创建自定义DataTable 28 /// </summary> 29 /// <param name="sList">列名字符串数组</param> 30 /// <returns>DataTable</returns> 31 public static DataTable CreateSelfDataTable(String[] sList) 32 { 33 DataTable _dtSelf = new DataTable(); 34 35 foreach (String s in sList) 36 { 37 _dtSelf.Columns.Add(s); 38 } 39 _dtSelf.AcceptChanges(); 40 41 return _dtSelf; 42 } 43 /// <summary> 44 /// 消息提示,默认为OK按键的Information提示信息 45 /// </summary> 46 /// <param name="sMessage">提示信息</param> 47 /// <param name="sTitle">信息标题</param> 48 /// <returns>DialogResult</returns> 49 public static System.Windows.Forms.DialogResult DialogMessage(String sMessage, String sTitle) 50 { 51 return DialogMessage(sMessage, 52 sTitle, 53 System.Windows.Forms.MessageBoxButtons.OK, 54 System.Windows.Forms.MessageBoxIcon.Information); 55 } 56 /// <summary> 57 /// 消息提示,默认为OK按键的Information提示信息 58 /// </summary> 59 /// <param name="sMessage">提示信息</param> 60 /// <param name="sTitle">信息标题</param> 61 /// <param name="mbbButtons">MessageBoxButtons枚举</param> 62 /// <returns>DialogResult</returns> 63 public static System.Windows.Forms.DialogResult DialogMessage(String sMessage, 64 String sTitle, 65 System.Windows.Forms.MessageBoxButtons mbbButtons) 66 { 67 return DialogMessage(sMessage, sTitle, mbbButtons, System.Windows.Forms.MessageBoxIcon.Information); 68 } 69 /// <summary> 70 /// 消息提示,默认为OK按键的Information提示信息 71 /// </summary> 72 /// <param name="sMessage">提示信息</param> 73 /// <param name="sTitle">信息标题</param> 74 /// <param name="mbbButtons">MessageBoxButtons枚举</param> 75 /// <param name="mbiIcon">MessageBoxIcon枚举</param> 76 /// <returns>DialogResult</returns> 77 public static System.Windows.Forms.DialogResult DialogMessage(String sMessage, 78 String sTitle, 79 System.Windows.Forms.MessageBoxButtons mbbButtons, 80 System.Windows.Forms.MessageBoxIcon mbiIcon) 81 { 82 return System.Windows.Forms.MessageBox.Show(sMessage, sTitle, mbbButtons, mbiIcon); 83 } 84 /// <summary> 85 /// 加密一个字符串 86 /// </summary> 87 /// <param name="vStringToEncrypt">待要去加密的字符串</param> 88 /// <returns>返回一个加密后的字符串</returns> 89 public static String Encrypt(String vStrToEncrypt) 90 { 91 byte[] key = { }; 92 byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 }; 93 byte[] inputByteArray; 94 String vEncryptKey = "!@#$1234"; 95 try 96 { 97 key = System.Text.Encoding.UTF8.GetBytes(vEncryptKey.Substring(0, 8)); 98 DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 99 inputByteArray = System.Text.Encoding.UTF8.GetBytes(vStrToEncrypt); 100 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 101 CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write); 102 cs.Write(inputByteArray, 0, inputByteArray.Length); 103 cs.FlushFinalBlock(); 104 105 return Convert.ToBase64String(ms.ToArray()); 106 } 107 catch (System.Exception ex) 108 { 109 throw ex; 110 } 111 } 112 /// <summary> 113 /// 对一进行加密的字符串进行解密 114 /// </summary> 115 /// <param name="vStringToDecrypt">待要去解密的字符串</param> 116 /// <returns>返回一个明文</returns> 117 public static String Decrypt(String vStrToDecrypt) 118 { 119 vStrToDecrypt = vStrToDecrypt.Trim().Replace(" ", "+"); //process blank char for the encrypt method. 120 byte[] key = { }; 121 byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 }; 122 byte[] inputByteArray = new byte[vStrToDecrypt.Length]; 123 String vDecryptKey = "!@#$1234"; 124 try 125 { 126 key = System.Text.Encoding.UTF8.GetBytes(vDecryptKey.Substring(0, 8)); 127 DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 128 inputByteArray = Convert.FromBase64String(vStrToDecrypt); 129 130 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 131 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write); 132 cs.Write(inputByteArray, 0, inputByteArray.Length); 133 cs.FlushFinalBlock(); 134 135 System.Text.Encoding encoding = System.Text.Encoding.UTF8; 136 return encoding.GetString(ms.ToArray()); 137 } 138 catch (System.Exception ex) 139 { 140 throw ex; 141 } 142 } 143 /// <summary> 144 /// 定义一个操作类型 145 /// ADD表示新增,EDIT表示修改,DEL表示删除 146 /// </summary> 147 public enum OperationType 148 { 149 ADD = 0, 150 EDIT = 1, 151 DEL = 2 152 } 153 154 /// <summary> 155 /// 定义照片类型 156 /// </summary> 157 public enum PictureSize 158 { 159 /// <summary> 160 /// 一代身份证 161 /// </summary> 162 ID_CARD1 = 1, 163 164 /// <summary> 165 /// 驾驶证 166 /// </summary> 167 DRIVER_CARD = 2, 168 169 /// <summary> 170 /// 护照 171 /// </summary> 172 PASSPORT_CARD = 3, 173 174 /// <summary> 175 /// 二代身份证 176 /// </summary> 177 ID_CARD2 = 4, 178 179 /// <summary> 180 /// 二代身份证背面,或类似尺寸证件的拍照使用 181 /// </summary> 182 ID_CARD2_BOTTOM = 5, 183 184 /// <summary> 185 /// 台胞证 186 /// </summary> 187 TAIWAN_CARD = 6, 188 189 /// <summary> 190 /// 港澳通行证 191 /// </summary> 192 HKMACAO_CARD = 7, 193 194 /// <summary> 195 /// 广东省居住证 196 /// </summary> 197 RESIDENCE_CARD = 9, 198 199 /// <summary> 200 /// 军官证 201 /// </summary> 202 ARMY_CARD = 103, 203 204 /// <summary> 205 /// 户口本 206 /// </summary> 207 HOUSEHOLD_CARD = 106 208 } 209 210 /// <summary> 211 /// 定义照片颜色类型 212 /// </summary> 213 public enum PictureColor 214 { 215 /// <summary> 216 /// 黑白照 217 /// </summary> 218 WHITEBLACK = 0, 219 /// <summary> 220 /// 彩色照 221 /// </summary> 222 FULLCOLOR = 1 223 } 224 225 /// <summary> 226 /// 定义绑定控件数据员类型 227 /// </summary> 228 public enum DataSourceType 229 { 230 /// <summary> 231 /// 表示DataTable形式的数据员 232 /// </summary> 233 DataCollection = 0, 234 /// <summary> 235 /// 表示Items形式数据员 236 /// </summary> 237 DataItems = 1 238 } 239 /// <summary> 240 /// 加载一个“请选择”值到Combobox控件 241 /// </summary> 242 /// <param name="dtSource">Combobox控件数据源</param> 243 /// <param name="sValue">ValueMember字段</param> 244 /// <param name="sDisplay">DisplayMember字段</param> 245 /// <returns></returns> 246 public static DataTable LoadInvalidValueToCombobox(DataTable dtSource, String sValue, String sDisplay) 247 { 248 DataRow _drNew = dtSource.NewRow(); 249 _drNew[sValue] = 0; 250 _drNew[sDisplay] = "请选择"; 251 dtSource.Rows.InsertAt(_drNew, 0); 252 dtSource.AcceptChanges(); 253 254 return dtSource; 255 } 256 /// <summary> 257 /// 加载一个“全部单位”值到Combobox控件 258 /// </summary> 259 /// <param name="dtSource">Combobox控件数据源</param> 260 /// <param name="sValue">ValueMember字段</param> 261 /// <param name="sDisplay">DisplayMember字段</param> 262 /// <returns></returns> 263 public static DataTable LoadInvalidValueToCombobox1(DataTable dtSource, String sValue, String sDisplay) 264 { 265 DataRow _drNew = dtSource.NewRow(); 266 _drNew[sValue] = 0; 267 _drNew[sDisplay] = "全部单位"; 268 dtSource.Rows.InsertAt(_drNew, 0); 269 dtSource.AcceptChanges(); 270 271 return dtSource; 272 } 273 274 /// <summary> 275 /// 验证字符串是否为正整数 276 /// </summary> 277 /// <param name="str"></param> 278 /// <returns></returns> 279 public static bool IsNumeric(string str) 280 { 281 System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[0-9]*[1-9][0-9]*$"); 282 return reg1.IsMatch(str); 283 } 284 /// <summary> 285 /// 验证字符串是否为0或正浮点类型 286 /// </summary> 287 /// <param name="str"></param> 288 /// <returns></returns> 289 public static bool isDouble(String str) 290 { 291 //正则表达式验证,返回一个bool类型 292 bool isReturn = false; 293 294 try 295 { 296 System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$"); 297 if (reg1.IsMatch(str) == true || IsNumeric(str) == true) isReturn = true; ; 298 } 299 catch 300 { 301 isReturn = false; 302 } 303 304 return isReturn; 305 } 306 /// <summary> 307 /// 验证固定电话号码 308 /// </summary> 309 /// <param name="str"></param> 310 /// <returns></returns> 311 public static bool isTel(String str) 312 { 313 System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^(\d{3,4}-)?\d{7,8}$"); 314 return reg1.IsMatch(str); 315 316 } 317 /// <summary> 318 /// 验证手机号码 319 /// </summary> 320 /// <param name="str"></param> 321 /// <returns></returns> 322 public static bool isMobile(String str) 323 { 324 System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[1][3-8]\d{9}$"); 325 return reg1.IsMatch(str); 326 327 } 328 /// <summary> 329 /// 验证数字 330 /// </summary> 331 /// <param name="str"></param> 332 /// <returns></returns> 333 public static bool isNumber(String str) 334 { 335 System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^\d+$"); 336 return reg1.IsMatch(str); 337 338 } 339 /// <summary> 340 /// 验证邮政编码 341 /// </summary> 342 /// <param name="str"></param> 343 /// <returns></returns> 344 public static bool isPost(String str) 345 { 346 System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^\d{6}$"); 347 return reg1.IsMatch(str); 348 349 } 350 /// <summary> 351 /// 验证邮件地址 352 /// </summary> 353 /// <param name="str"></param> 354 /// <returns></returns> 355 public static bool isEmail(String str) 356 { 357 System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"); 358 return reg1.IsMatch(str); 359 360 } 361 362 /// <summary> 363 /// 验证IP地址 364 /// </summary> 365 /// <param name="str"></param> 366 /// <returns></returns> 367 public static bool isIP(String str) 368 { 369 return System.Text.RegularExpressions.Regex.IsMatch(str, 370 @"^(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])(\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])){3}$"); 371 } 372 373 /// <summary> 374 /// 验证输入字符串只能为数字或字母 375 /// </summary> 376 /// <param name="str"></param> 377 /// <returns></returns> 378 public static bool IsNumberOrChar(String str) 379 { 380 return System.Text.RegularExpressions.Regex.IsMatch(str, "^([0-9]|[A-Z]|[a-z])+$"); 381 } 382 383 //object类型转十进制 384 public static decimal ConvertoDecimal(object o) 385 { 386 decimal number = 0; 387 try 388 { 389 if (o != null && o.ToString() != "") 390 number = Convert.ToDecimal(o); 391 392 } 393 catch 394 { 395 number = 0; 396 } 397 398 return number; 399 } 400 401 //判断是否字符串是否为日期类型 402 public static DateTime isDataTime(string strDate) 403 { 404 DateTime dtDate; 405 406 if (DateTime.TryParse(strDate, out dtDate)) 407 { 408 return Convert.ToDateTime(strDate); 409 } 410 else 411 { 412 return Convert.ToDateTime("1753-1-1"); 413 } 414 415 } 416 417 418 /// <summary> 419 /// 生成GUID码 420 /// </summary> 421 /// <returns></returns> 422 public static string getGUID() 423 { 424 System.Guid guid = new Guid(); 425 guid = Guid.NewGuid(); 426 string str = guid.ToString(); 427 return str; 428 } 429 /// <summary> 430 /// 判断文件夹是否存在,不存在就创建 431 /// </summary> 432 /// <param name="sFileFolder">文件夹路径</param> 433 /// <returns>文件夹路径</returns> 434 public static string GetFileFolder(String sFileFolder) 435 { 436 if (!System.IO.Directory.Exists(sFileFolder)) 437 { 438 System.IO.Directory.CreateDirectory(sFileFolder); 439 } 440 return sFileFolder; 441 } 442 /// <summary> 443 /// 生成一个错误日志 444 /// </summary> 445 /// <param name="sErrMsg">错误信息</param> 446 public static void LogErrorLog(string sErrMsg) 447 { 448 String _sFolder = System.Configuration.ConfigurationManager.AppSettings["Error_Log"]; 449 450 _sFolder = GetFileFolder(_sFolder); 451 String _sFile = String.Format("{0}{1}.txt", _sFolder, DateTime.Now.ToString("yyyyMMdd")); 452 453 System.IO.FileInfo _fiFile = new System.IO.FileInfo(_sFile); 454 System.IO.StreamWriter _swTxt; 455 456 if (!_fiFile.Exists) 457 { 458 _swTxt = _fiFile.CreateText(); 459 } 460 else 461 { 462 _swTxt = _fiFile.AppendText(); 463 } 464 465 _swTxt.WriteLine(String.Format("{0}-----{1}", DateTime.Now.ToString("HH:mm:ss"), sErrMsg)); 466 _swTxt.Close(); 467 } 468 469 Boolean isFind = false; //查找文件是否找到标识位 470 /// <summary> 471 /// 查找指定文件,返回查找文件路径 472 /// </summary> 473 /// <param name="sFileName">待查找的文件名</param> 474 /// <returns>返回查找文件路径</returns> 475 public static String SearchFile(String sFileName) 476 { 477 System.IO.DriveInfo[] driList = System.IO.DriveInfo.GetDrives(); 478 BaseClass _bc = new BaseClass(); 479 480 String _sFileName = String.Empty; 481 482 foreach (System.IO.DriveInfo dri in driList) 483 { 484 _sFileName = _bc.FindFile(dri.RootDirectory, sFileName); 485 String _tmp = _sFileName.Substring(_sFileName.LastIndexOf(@"\") + 1); 486 487 if (_tmp == sFileName) 488 { 489 break; 490 } 491 } 492 493 return _sFileName; 494 } 495 496 private String FindFile(System.IO.DirectoryInfo diDirectory, String sFileName) 497 { 498 String sFindFile = String.Empty; 499 try 500 { 501 foreach (System.IO.FileInfo fi in diDirectory.GetFiles()) 502 { 503 if (fi.Name == sFileName) 504 { 505 sFindFile = fi.FullName; 506 isFind = true; 507 break; 508 } 509 } 510 511 foreach (System.IO.DirectoryInfo di in diDirectory.GetDirectories()) 512 { 513 if (!isFind) 514 sFindFile = FindFile(di, sFileName); 515 else 516 break; 517 } 518 } 519 catch 520 { 521 throw new System.IO.FileNotFoundException("文件不能找到"); 522 } 523 524 return sFindFile; 525 } 526 /// <summary> 527 /// 根据屏幕分辨率自动调整控件位置与大小 528 /// </summary> 529 /// <param name="cContainer">要调整的控件</param> 530 /// <param name="ScreenWidth">当前显示器分辨率宽度</param> 531 /// <param name="ScreenHeight">当前显示器分辨率高度</param> 532 public static void AutoChangeControlSizeByScreen(System.Windows.Forms.Control cContainer, 533 int ScreenWidth, 534 int ScreenHeight) 535 { 536 cContainer.SuspendLayout(); 537 //显示屏大于1280或1024不处理 538 if (ScreenWidth > 1280 || ScreenHeight > 1024) 539 { 540 WidthPercent = 1; 541 HeightPercent = 1; 542 } 543 else 544 { 545 WidthPercent = Math.Round((decimal)ScreenWidth / 1280, 4); 546 HeightPercent = Math.Round((decimal)ScreenHeight / 1024, 4); 547 } 548 //处理传过来控件本身等比缩放 549 cContainer.Left = (int)Math.Ceiling((decimal)cContainer.Left * WidthPercent); 550 cContainer.Top = (int)Math.Ceiling((decimal)cContainer.Top * HeightPercent); 551 cContainer.Width = (int)Math.Ceiling((decimal)cContainer.Width * WidthPercent); 552 cContainer.Height = (int)Math.Ceiling((decimal)cContainer.Height * HeightPercent); 553 ChangeControl(cContainer); 554 cContainer.ResumeLayout(); 555 } 556 557 private static void ChangeControl(System.Windows.Forms.Control cContainer) 558 { 559 foreach (System.Windows.Forms.Control c in cContainer.Controls) 560 { 561 c.Left = (int)Math.Ceiling((decimal)c.Left * WidthPercent); 562 c.Top = (int)Math.Ceiling((decimal)c.Top * HeightPercent); 563 c.Width = (int)Math.Ceiling((decimal)c.Width * WidthPercent); 564 c.Height = (int)Math.Ceiling((decimal)c.Height * HeightPercent); 565 c.Font = new System.Drawing.Font("微软雅黑", c.Font.Size * (float)((HeightPercent + WidthPercent) / 2)); 566 567 if (c.HasChildren) 568 { 569 ChangeControl(c); 570 } 571 } 572 } 573 574 /// <summary> 575 /// 统一设置字体为微软雅黑12pt 576 /// </summary> 577 /// <param name="cContainer">要调整的控件</param> 578 public static void ChangeFont(System.Windows.Forms.Control cContainer) 579 { 580 foreach (System.Windows.Forms.Control c in cContainer.Controls) 581 { 582 c.Font = new System.Drawing.Font("微软雅黑", 12); 583 if (c.HasChildren) ChangeFont(c); 584 } 585 } 586 587 /// <summary> 588 /// 判断文件夹是否存在,不存在创建 589 /// 同时返回文件夹路径 590 /// </summary> 591 /// <param name="sFolderPath">文件夹绝对路径</param> 592 /// <returns></returns> 593 public static String IsFolderExist(String sFolderPath) 594 { 595 if (System.IO.Directory.Exists(sFolderPath) == false) 596 { 597 System.IO.Directory.CreateDirectory(sFolderPath); 598 } 599 return sFolderPath; 600 } 601 602 /// <summary> 603 /// 导出Excel的文件名称 604 /// </summary> 605 /// <param name="titleName">导出标题名称</param> 606 /// <param name="suffixName">导出序列号(SN)</param> 607 /// <returns></returns> 608 public static String ExcelFileName(String titleName, String suffixName) 609 { 610 String strName; 611 if (String.IsNullOrEmpty(suffixName)) 612 { 613 strName = DateTime.Now.ToString("yyyyMMdd_HHmmss") + "_" + titleName; 614 } 615 else 616 { 617 strName = DateTime.Now.ToString("yyyyMMdd_HHmmss") + "_" + titleName + "_" + suffixName; 618 } 619 return strName; 620 } 621 622 /// <summary> 623 /// 导出Excel的文件名称 624 /// </summary> 625 /// <param name="titleName">导出标题名称</param> 626 /// <returns></returns> 627 public static String ExcelFileName(String titleName) 628 { 629 return ExcelFileName(titleName, String.Empty); 630 } 631 } 632 }
新增:
1、字符串转数组
/// <summary> /// 包含“,”的字符串转数组 /// </summary> /// <param name="str"></param> /// <returns></returns> public string[] mArray(string str) { string[] mMsg; if (str.IndexOf(',') != -1) { mMsg = str.Split(','); } else { mMsg = new string[1]; mMsg[0] = str; } return mMsg; }
2、自定义数组
/// <summary> /// 根据列名字符串数组,创建自定义DataTable /// </summary> /// <param name="sList">列名字符串数组</param> /// <returns>DataTable</returns> public static DataTable CreateSelfDataTable(String[] sList) { DataTable _dtSelf = new DataTable(); foreach (String s in sList) { _dtSelf.Columns.Add(s); } _dtSelf.AcceptChanges(); return _dtSelf; }
3、临时创建一个表
private static DataTable CreatTable() { DataTable dtSource = new DataTable(); dtSource.Columns.Add("NAME", typeof(string)); dtSource.Columns.Add("DisMembers", typeof(string)); //一百万行数据 for (int i = 0; i < 1000000; i++) { DataRow dr = dtSource.NewRow(); dr["NAME"] = i; dr["DisMembers"] = i; dtSource.Rows.Add(dr); } dtSource.AcceptChanges(); return dtSource; }
所有问题都会有一定程度的抽象和假设