WinForm BaseClass类常用通用方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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         }

以上是关于WinForm BaseClass类常用通用方法的主要内容,如果未能解决你的问题,请参考以下文章

Winform 各种属性方法控件

winform各种属性方法控件

Winform 使用通用方法将按钮文本复制到文本框

WinForm通用自动更新器AutoUpdater项目实战

WinForm通用自动更新器AutoUpdater项目实战

WinForm通用自动更新器AutoUpdater项目实战