📄 public.cs
字号:
/// <summary>
/// 检查给定的字符串是否符合指定的位数,如果不符合则在前补上剩余位数的0
/// </summary>
/// <param name="str"></param>
/// <param name="num"></param>
/// <returns></returns>
public static string SetStartZero(string str,int num){
if(str != null && num > 0){
for(int i=str.Length;i<num;i++){
str = "0" + str;
}
}
return str;
}
/// <summary>
/// 检查系统的自动编号是否正确
/// 指被应用了自动编号的位置的编号的有效性
/// </summary>
/// <returns></returns>
public static bool SystemSignIsRight(string sign){
if(sign != null && sign.Trim().Length == 14){
string year = sign.Substring(0,4);
string line1 = sign.Substring(4,1);
string dm = sign.Substring(5,4);
string line2 = sign.Substring(9,1);
string num = sign.Substring(10,4);
try{
int _year = Int32.Parse(year);
int _dm = Int32.Parse(dm);
int _num = Int32.Parse(num);
}catch{
return false;
}
if(line1 != "-" || line2 != "-"){
return false;
}
return true;
}else{
return false;
}
}
/// <summary>
/// 处理打印时的页对,适用于列表打印
/// </summary>
/// <param name="tj">用$隔开的条件</param>
/// <returns></returns>
public static string PrintHeader(string tj){
string header = null;
if(tj != null && tj.Trim() != ""){
tj = tj.Replace("NULL$","");
tj = tj.Replace("NULL","");
if(tj.StartsWith("$")) tj = tj.Substring(1,tj.Length-1);
if(tj != ""){
string[] tjArr = tj.Split('$');
//bool first = true;
for(int i=0;i<tjArr.Length;i++){
//if(first){
header += "{[\\n]"+ tjArr[i].ToString() +"}";
// //first = false;
//}else{
// header += "{[\\l100]"+ tjArr[i].ToString() +"}";
// first = true;
//}
}
}
}
return header;
}
/// <summary>
/// 返回指定编号的客户资料(一行)
/// </summary>
/// <returns></returns>
public static DataRow GetCorpRow(string sign){
DataRow row = null;
try{
string sql = "select * from CorpColl where sign = '"+ sign +"'";
OleDbDataAdapter ad = new OleDbDataAdapter(sql,Public.conn);
DataSet ds = new DataSet();
ad.Fill(ds,"info");
if(ds.Tables[0].Rows.Count == 1){
row = ds.Tables[0].Rows[0];
}
}catch(Exception ex){
MessageBox.Show(ex.Message.ToString());
return row;
}
return row;
}
/// <summary>
/// 将给定字符串每个字符间用空格分隔
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string SpaceForString(string str,string spaceChar){
if(str != null && str != ""){
string _temp = "";
char[] s = str.ToCharArray();
for(int i=0;i<s.Length;i++){
if(_temp == "") _temp = s[i].ToString();
else _temp += spaceChar + s[i].ToString();
}
str = _temp;
}
return str;
}
/// <summary>
/// 返回中文年月日
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static string GetChineseDate(DateTime date){
return date.Year.ToString() + "年" + date.Month.ToString() + "月" + date.Day.ToString() + "日";
}
/// <summary>
/// 返回主单位的联系信息
/// </summary>
/// <returns></returns>
public static string GetPrintFooter(){
return "地址:" + Public.publicAddress + " 电话:" + Public.publicTel + " 传真:" + Public.publicFax;
}
/// <summary>
/// 是否手工输入了公司名称
/// 如果是则反回true,反之false
/// </summary>
/// <returns></returns>
public static bool CheckCorpNameInput(string cn,ComboBox sel){
if(cn == null || cn.Trim() == "") return false;
if(sel != null && sel.Items.Count > 0){
bool reVal = false;
for(int i=0;i<sel.Items.Count;i++){
if(cn == sel.Items[i].ToString().Trim()){
reVal = true;
i = sel.Items.Count;
}
}
return reVal;
}else{
return false;
}
}
/// <summary>
/// 统计汉字和英文字符的个数
/// </summary>
/// <returns>返回数组,0表示汉字的个数,1表示的是英文的个数</returns>
public static int TotalChrNum(string str){
int[] result = new int[2];
result[0] = 0;
result[1] = 0;
if(str != null && str != ""){
char[] strs = str.ToCharArray();
for(int i=0;i<strs.Length;i++){
if(System.Text.RegularExpressions.Regex.IsMatch(strs[i].ToString(),"[\u4e00-\u9fa5]"))
{
result[0] += 2;
}
else{
result[1] ++;
}
}
}
return result[0] + result[1];
}
/// <summary>
/// 将待打印的地址字符按每行能显示下的字符个数进行分隔
/// </summary>
/// <param name="address"></param>
/// <param name="cutNum"></param>
/// <returns></returns>
public static string[] CutAddressForPrint(string address,int cutNum){
string[] result = new string[2];
result[0] = "";
result[1] = "";
if(address != null && cutNum > 0){
if(Public.TotalChrNum(address) <= cutNum){
result[0] = address;
}else{
char[] addressChars = address.ToCharArray();
int leftNum = 0;
for(int i=0;i<addressChars.Length;i++){
if( leftNum <= cutNum){
if(System.Text.RegularExpressions.Regex.IsMatch(addressChars[i].ToString(),"[\u4e00-\u9fa5]")){
leftNum += 2;
}else{
leftNum ++;
}
result[0] += addressChars[i].ToString();
}else{
result[1] += addressChars[i].ToString();
}
}
}
}
return result;
}
#region 使grid根据内容适应列宽
public static void SizeColumnsToContent (System.Windows.Forms.DataGrid dataGrid, int nRowsToScan,System.Data.DataTable _dataTable,string[] hideColumn) {
Graphics Graphics = dataGrid.CreateGraphics();
System.Data .DataTable dataTable =_dataTable;
DataGridTableStyle tableStyle = null;
if(dataGrid.TableStyles.Count == 0){
tableStyle = new DataGridTableStyle();
dataGrid.TableStyles.Clear();
tableStyle.MappingName = dataTable.TableName;
//设置grid中蓝白交错效果
tableStyle.AlternatingBackColor =Color.FromArgb(224,226,228);
tableStyle.BackColor =Color.White ;
}else tableStyle = dataGrid.TableStyles[0];
tableStyle.PreferredRowHeight = 22;
try {
if (-1 == nRowsToScan) {
nRowsToScan = dataTable.Rows.Count;
}else {
nRowsToScan = System.Math.Min(nRowsToScan, dataTable.Rows.Count);
}
DataGridTextBoxColumn columnStyle;
int iWidth;
for (int iCurrCol = 0; iCurrCol < dataTable.Columns.Count; iCurrCol++) {
DataColumn dataColumn = dataTable.Columns[iCurrCol];
columnStyle = new DataGridTextBoxColumn();
columnStyle.TextBox.Enabled = true;
columnStyle.HeaderText = dataColumn.ColumnName;
columnStyle.MappingName = dataColumn.ColumnName;
columnStyle.NullText = "";
iWidth = (int)(Graphics.MeasureString(columnStyle.HeaderText, dataGrid.Font).Width);
DataRow dataRow;
for (int iRow = 0; iRow < nRowsToScan; iRow++) {
dataRow = dataTable.Rows[iRow];
if (null != dataRow[dataColumn.ColumnName]) {
int iColWidth = (int)(Graphics.MeasureString(dataRow.ItemArray[iCurrCol].ToString(), dataGrid.Font).Width);
iWidth = (int)System.Math.Max(iWidth, iColWidth);
}
}
bool isHide = false;
if(hideColumn != null){
for(int x=0;x<hideColumn.Length;x++){
if(hideColumn[x].ToString().Equals(dataColumn.ColumnName)){
isHide = true;
}
}
}
if(isHide)
{
columnStyle.Width = 0;
}
else{
columnStyle.Width = iWidth + 4;
}
tableStyle.GridColumnStyles.Add(columnStyle);
}
dataGrid.TableStyles.Add(tableStyle);
}catch(Exception e) {
MessageBox.Show(e.Message);
}finally {
Graphics.Dispose();
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -