📄 usercls.cs
字号:
//下面这一句就可以打印DataGridView
//(webmis.Body as GoldPrinter.Body).DataSource = ToStringArray(dataGridView1, true);
//为人特性化,自定义表体,可以设置字体、列宽、列对齐方式
GoldPrinter.Body gridBody = new GoldPrinter.Body();
//任意二维的数据通通打印,或者是设置GridText属性
gridBody.DataSource = ToStringArray(dataGridView1, true); //此处使用gridBody.DataSource =dataGridView1.DataSource 也可实现
gridBody.Font = dataGridView1.Font;
//gridBody.ColsWidth = GetColsWidth(dataGridView1);
webmis.Body = gridBody;
// webmis.Preview();//打印预览
webmis.PageSetup();//页面设置
webmis.Dispose();//撤消打印资源
}
public void DataPreview(DataGridView dataGridView1, string PrintTitle)
{
webmis.Title = PrintTitle; //标题,还可设置子标题
webmis.Footer = System.DateTime.Now.ToString();
(webmis.Title as GoldPrinter.Title).Font = new System.Drawing.Font("宋体", 12, System.Drawing.FontStyle.Bold);
//下面这一句就可以打印DataGridView
//(webmis.Body as GoldPrinter.Body).DataSource = ToStringArray(dataGridView1, true);
//为人特性化,自定义表体,可以设置字体、列宽、列对齐方式
GoldPrinter.Body gridBody = new GoldPrinter.Body();
//任意二维的数据通通打印,或者是设置GridText属性
gridBody.DataSource = ToStringArray(dataGridView1, true); //此处使用gridBody.DataSource =dataGridView1.DataSource 也可实现
gridBody.Font = dataGridView1.Font;
//gridBody.ColsWidth = GetColsWidth(dataGridView1);
webmis.Body = gridBody;
// webmis.Preview();//打印预览
webmis.Preview();//打印
webmis.Dispose();//撤消打印资源
}
//DataGridView转换为二维数组
/// <summary>
/// 将VS.Net 2005 DataGridView控件的数据导出到二维数组。
/// </summary>
/// <param name="dataGridView">VS.Net 2005 DataGridView控件。</param>
/// <param name="includeColumnText">是否要把列标题文本也导到数组中。</param>
/// <作者>殷庆飞</作者>
/// <日期></日期>
public string[,] ToStringArray(DataGridView dataGridView, bool includeColumnText)
{
#region 实现...
string[,] arrReturn = null;
int rowsCount = dataGridView.Rows.Count;
int colsCount = dataGridView.Columns.Count;
if (rowsCount > 0)
{
//最后一行是供输入的行时,不用读数据。
if (dataGridView.Rows[rowsCount - 1].IsNewRow)
{
rowsCount--;
}
}
int i = 0;
//包括列标题
if (includeColumnText)
{
rowsCount++;
arrReturn = new string[rowsCount, colsCount];
for (i = 0; i < colsCount; i++)
{
arrReturn[0, i] = dataGridView.Columns[i].HeaderText;
}
i = 1;
}
else
{
arrReturn = new string[rowsCount, colsCount];
}
//读取单元格数据
int rowIndex = 0;
for (; i < rowsCount; i++, rowIndex++)
{
for (int j = 0; j < colsCount; j++)
{
arrReturn[i, j] = dataGridView.Rows[rowIndex].Cells[j].Value.ToString();
}
}
return arrReturn;
#endregion 实现
}
/// <summary>
/// 使DataGridView隔行变色
/// </summary>
/// <param name="dataGridView1"></param>
public void SetDGVColor(DataGridView dataGridView1)
{
//datagridview1的隔行变色
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.YellowGreen;
dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black;
//设定交替行颜色
dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Wheat;
dataGridView1.RowsDefaultCellStyle.BackColor = Color.AliceBlue;
}
/// <summary>
/// 验正字符串的长度
/// </summary>
/// <param name="strCheck">要验正的字符串</param>
/// <param name="strLength">限制的最大长度</param>
/// <returns></returns>
public bool CheckString(string strCheck, int strLength)
{
byte[] buffer = System.Text.Encoding.Default.GetBytes(strCheck.Trim());//将字符串转换为字节,来计算其长度
if (buffer.Length > strLength)//字符串大于指定的长度
{
MessageBox.Show("长度大于" + strLength.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
else
{
return true;
}
}
// Function to test for Positive Integers
/// <summary>
/// 验证是否为自然数
/// </summary>
/// <param name="strNumber">要验正的字符串</param>
/// <returns></returns>
public bool IsNaturalNumber(String strNumber)
{
Regex objNotNaturalPattern = new Regex("[^0-9]");
Regex objNaturalPattern = new Regex("0*[1-9][0-9]*");
return !objNotNaturalPattern.IsMatch(strNumber) &&
objNaturalPattern.IsMatch(strNumber);
}
// Function to test for Positive Integers with zero inclusive
/// <summary>
/// 验正是否为数字串
/// </summary>
/// <param name="strNumber">要验正的字符串</param>
/// <returns></returns>
public bool IsWholeNumber(String strNumber)
{
Regex objNotWholePattern = new Regex("[^0-9]");
return !objNotWholePattern.IsMatch(strNumber);
}
// Function to Test for Integers both Positive & Negative
/// <summary>
/// 验正是否为正数
/// </summary>
/// <param name="strNumber">要验正的字符串</param>
/// <returns></returns>
public bool IsInteger(String strNumber)
{
Regex objNotIntPattern = new Regex("[^0-9-]");
Regex objIntPattern = new Regex("^-[0-9]+$|^[0-9]+$");
return !objNotIntPattern.IsMatch(strNumber) &&
objIntPattern.IsMatch(strNumber);
}
// Function to Test for Positive Number both Integer & Real
/// <summary>
/// 验正是否为整型和实型
/// </summary>
/// <param name="strNumber">要验正的字符串</param>
/// <returns></returns>
public bool IsPositiveNumber(String strNumber)
{
Regex objNotPositivePattern = new Regex("[^0-9.]");
Regex objPositivePattern = new Regex("^[.][0-9]+$|[0-9]*[.]*[0-9]+$");
Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
return !objNotPositivePattern.IsMatch(strNumber) &&
objPositivePattern.IsMatch(strNumber) &&
!objTwoDotPattern.IsMatch(strNumber);
}
// Function to test whether the string is valid number or not
/// <summary>
/// 验是否为有效的数字
/// </summary>
/// <param name="strNumber">要验正的字符串</param>
/// <returns></returns>
public bool IsNumber(String strNumber)
{
Regex objNotNumberPattern = new Regex("[^0-9.-]");
Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");
return !objNotNumberPattern.IsMatch(strNumber) &&
!objTwoDotPattern.IsMatch(strNumber) &&
!objTwoMinusPattern.IsMatch(strNumber) &&
objNumberPattern.IsMatch(strNumber);
}
// Function To test for Alphabets.
/// <summary>
/// 验正是否为字母
/// </summary>
/// <param name="strToCheck">要验正的字符串</param>
/// <returns></returns>
public bool IsAlpha(String strToCheck)
{
Regex objAlphaPattern = new Regex("[^a-zA-Z]");
return !objAlphaPattern.IsMatch(strToCheck);
}
// Function to Check for AlphaNumeric.
/// <summary>
/// 验正是否为字母和数字组成的串
/// </summary>
/// <param name="strToCheck">要验正的字符串</param>
/// <returns></returns>
public bool IsAlphaNumeric(String strToCheck)
{
Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9]");
return !objAlphaNumericPattern.IsMatch(strToCheck);
}
/// <summary>
/// 启动线程对局域网内的IP进行扫描
/// </summary>
/// <param name="listView1">存放所有扫描出IP的listView</param>
/// <param name="strNetWork">所要扫描的网段</param>
/// <param name="strStartIP">开始IP</param>
/// <param name="strEndIP">结束IP</param>
public void StartThreadForIP(ListView listView1, string strNetWork, string strStartIP, string strEndIP)
{
//Thread 类: 创建并控制线程
//Thread thScan = new Thread(new ThreadStart(ScanTarget));
//Thread thScan = new Thread(new ThreadStart(ScanIP));
//Thread.Start 方法:启动线程
//thScan.Start();
}
/// <summary>
/// 对局域网内的IP进行扫描
/// </summary>
/// <param name="listView1">存放所有扫描出IP的listView</param>
/// <param name="strNetWork">所要扫描的网段</param>
/// <param name="strStartIP">开始IP</param>
/// <param name="strEndIP">结束IP</param>
private void ScanIP(ListView listView1, string strNetWork, string strStartIP, string strEndIP)
{
listView1.Items.Clear();
ListViewItem listviewitem; //清空listView1的项
string strIPaddress = strNetWork + "."; //或取前24位的IP地址
int start = Int32.Parse(strStartIP);
int end = Int32.Parse(strEndIP);
if (start > end)
MessageBox.Show("终止地止必须大于启始地址", "警告"); //比较开始地址与接束地址,不允许终止地止必须小于启始地址
else
{
for (int i = start; i <= end; i++)
{
string strscanip = strIPaddress + i.ToString();//构造IP地址
IPAddress sacanip = IPAddress.Parse(strscanip);//转换成IP地址
listviewitem = new ListViewItem(strscanip, 0);//在ListViewItem添加IP地址
try
{
IPHostEntry hostinfo = Dns.GetHostByAddress(sacanip); //根据ip地址或取主机的DNS信息
string hostname = hostinfo.HostName.ToString(); //或取主机名
listviewitem.SubItems.Add(hostname); //在ListViewItem添加主机名
listviewitem.SubItems.Add("可到达");
listView1.Items.Add(listviewitem); //将项加入listView1
}
catch (Exception ew)
{
//listviewitem.SubItems.Add("未知");
//listviewitem.SubItems.Add("不可到达");
string strError = string.Format("ERROR:{0}", ew.Message);
MessageBox.Show(strError, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -