📄 public.cs
字号:
using System;
using System.Data;
using System.Data.OleDb;
using System.Windows;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace bookmanager
{
/// <summary>
/// Public 的摘要说明。
/// </summary>
public class Public
{
public Public(){}
public static string connStr = "";
public static OleDbConnection conn = null;
public static string cuUser = "";
public static string userID = "";
public static string passCH = "";
public static DataSet ds;
public static DataTable dt;
public static DataRow dr;
public static OleDbDataReader myReader;
/// <summary>
///
/// </summary>
public static string publicCorp = "图书管理信息管理系统";
public static string publicAddress = "深圳市宝安区35区海滨花园3栋704";
public static string publicTel = "0755";
public static string publicFax = "0755";
//系统编号不正确的提示
public static string systemSignNotRight = "\n\n正确的格式为:四位数年份-两位数月份两位数日期-四位数的自动编号,例如:2005-0905-0001。";
//当前选好的单位,或者说是默认的单位
public static string currentSelfCorp = "2005-0901-0002";
/// <summary>
/// 图标
/// </summary>
public static Image lyLogo = null;
//MDI
public static MainForm mainForm = null;
//返回数据库连接
public static void NewConn()
{
if(Public.conn == null || Public.conn.State.ToString().ToUpper() != "OPEN")
{
OleDbConnection conn = new OleDbConnection(Public.connStr);
Public.conn = conn;
}
}
public static void CloseConn()
{
if(Public.conn != null && Public.conn.State.ToString().ToUpper() == "OPEN")
{
Public.conn.Close();
}
if (ds!=null) // 清除DataSet对象
{
ds.Clear();
}
}
/// <summary>
/// 从库中获取指定的选择条目上
/// </summary>
/// <param name="dcSign"></param>
/// <returns></returns>
public static DataSet GetDireItem()
{
DataSet ds = null;
try
{
string sql = "select * from orders order by code";
OleDbDataAdapter ad = new OleDbDataAdapter(sql,Public.conn);
ds = new DataSet();
ad.Fill(ds,"orders");
}
catch
{
//
}
return ds;
}
public static string RMB(string m)
{
string DX=m.ToString();
int XSD=DX.IndexOf(".",0);
string XP;
if(XSD==-1)
{
XP=Public.RY(DX);
}
else
{
string RX="";
string DX1=DX.Substring(0,XSD);
XP=Public.RY(DX1);
string DX2=DX.Substring(XSD,DX.Length-XSD).Substring(1);
if(DX2.Length==1)
if (DX2[0]!='0')
RX=Public.HZ(DX2[0])+"角零分";
if(DX2.Length==2)
{
if(DX2[1]=='0'&& DX2[0]=='0')
{
RX = "整";
}
else if(DX2[1]!='0'&& DX2[0]!='0')
RX=Public.HZ(DX2[0])+"角"+Public.HZ(DX2[1])+"分";
else
if(DX2[0]=='0')
RX="零" + Public.HZ(DX2[1])+"分";
else
RX=Public.HZ(DX2[0])+"角零分";
}
XP=XP+RX;
}
if(XP.EndsWith("元")) XP += "整";
return XP.Replace("拾零万","拾万").Replace("拾零元","拾元").Replace("仟零零零元整","仟元整").Replace("佰零零元整","佰元整").Replace("万零零零零元","万元整");
}
public static string RY(string DX)
{
string R="";
int l=DX.Length;
if(DX=="0") return R;
int b=0;
for(int i=0;i<DX.Length;i++)
{
int m=DX.Length-i;
R=R+Public.HZ(DX[i]);
if(b==0 && DX[i]=='0'&& m!=5 && m!=1 && i<DX.Length-1)
{
bool p=true;
for(int k=i+1;k<DX.Length;k++)
{
if(DX[k]=='0') p=false;
}
if(p==true)
{
R=R+"零";
b=1;
}
}
if(m==8 && DX[i]!='0') R=R+"仟";
if(m==7 && DX[i]!='0') R=R+"佰";
if(m==6 && DX[i]!='0') R=R+"拾";
if(m==5 ) R=R+"万";
if(m==4 && DX[i]!='0') R=R+"仟";
if(m==3 && DX[i]!='0') R=R+"佰";
if(m==2 && DX[i]!='0') R=R+"拾";
if(m==1) R=R+"元";
}
return R;
}
public static string HZ(char num)
{
switch(num)
{
case '1': return "壹";
case '2': return "贰";
case '3': return "叁";
case '4': return "肆";
case '5': return "伍";
case '6': return "陆";
case '7': return "柒";
case '8': return "捌";
case '9': return "玖";
default : return "零";
}
}
/// <summary>
/// 将金额小数点后两位加上
/// </summary>
/// <param name="jr"></param>
/// <returns></returns>
public static string NewJR(string jr)
{
if(jr != null)
{
int index = jr.IndexOf(".");
if(index == -1) jr += ".00";
if(jr.Length - index == 2) jr += "0";
if(jr.Length - index == 1) jr += "00";
//jr = jr.ToString().Replace(".00","");
}
return jr;
}
/// <summary>
/// 调整DataGrid整行的高度
/// </summary>
/// <param name="dg"></param>
/// <param name="colName"></param>
public static void ReSetGridCellHeight(DataGrid dg,string tableName)
{
//DataGridTableStyle dgts = new DataGridTableStyle();
//dgts.PreferredRowHeight = 25;
//dgts.MappingName = ((DataTable)dg.DataSource).TableName;
//dg.TableStyles.Add(dgts);
}
/// <summary>
/// 返回最新的自动编号
/// </summary>
/// <returns></returns>
public static string GetNewInfoID(string tableName,string fieldName)
{
//前部分的日期
string result = DateTime.Now.Year.ToString() + "-" + Public.SetStartZero(DateTime.Now.Month.ToString(),2) + Public.SetStartZero(DateTime.Now.Day.ToString(),2);
try
{
string sql = "select top 1 "+ fieldName +" from " + tableName + " where "+ fieldName +" like '"+ result +"*' order by autoID desc";
OleDbDataAdapter ad = new OleDbDataAdapter(sql,Public.conn);
DataSet ds = new DataSet();
ad.Fill(ds,"info");
if(ds.Tables["info"].Rows.Count == 0)
{
result += "-0001";
}
else
{
string _val = ds.Tables[0].Rows[0][fieldName].ToString();
int _num = Int32.Parse(_val.Substring(_val.LastIndexOf("-")+1,_val.Length-_val.LastIndexOf("-")-1));
_num ++;
result += "-" + Public.SetStartZero(_num.ToString(),4);
}
}
catch(Exception ex)
{
MessageBox.Show("不能自动获取信息编号,系统返回错误:" + ex.Message.ToString());
return null;
}
return result;
}
/// <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>
/// <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
/// <summary>
/// 返回SqlDataReader,同时搞定a.myReader
/// 要小心关闭哦!
/// </summary>
/// <param name="s">sql啦</param>
/// <returns></returns>
public OleDbDataReader getreader(string s)
{
OleDbCommand myCommand = new OleDbCommand(s,conn);
conn.Open();
myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
return myReader;
}
public static void ExeSql(string str_Sql)
{
OleDbCommand myCommand = new OleDbCommand(str_Sql,conn);
CloseConn();
conn.Open();
myCommand.ExecuteNonQuery();
myCommand.Dispose();
CloseConn();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -