📄 globleutility1.cs
字号:
///////////////////////////////////////////////////////////////////////////////
/// 北京交通大学版权所有 2004.07 ///
/// 文件名: GlobleUtitity1.cs ///
/// 描述:本文件包含了电子图书馆一卡通系统的公共组件类 ///
/// 创建时间: 2004.07.19 ///
///////////////////////////////////////////////////////////////////////////////
using System;
using System.Windows.Forms;
using Microsoft.Win32;
using System.ComponentModel;
namespace GlobleUtility
{
/// <summary>
/// 类名:NumberTextBox
/// 功能:输入数字的编辑框,可以指定最大值、最小值和小数位位数
/// 属性:MaxValue 编辑框所能接收的最大值,Decimal类型,默认是+79,228,162,514,264,337,593,543,950,335
/// MinValue 编辑框所能接收的最小值,Decimal类型,默认是-79,228,162,514,264,337,593,543,950,335
/// Decimal 小数点后的位数,默认是零
/// 使用:用程序生成实例。详细例子见VSS文档中的第八项。
/// </summary>
public class NumberTextBox: System.Windows.Forms.TextBox
{
private string sOldString = "";
private decimal fMaxValue = System.Decimal.MaxValue;
private decimal fMinValue = System.Decimal.MinValue;
private short nDecimal = 0;
private char[] ValidChar = new char[13] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '-', '\x08'};
/// <summary>
/// 编辑框所能接收的最大值
/// </summary>
public decimal MaxValue
{
get
{
return fMaxValue;
}
set
{
fMaxValue = value;
}
}
/// <summary>
/// 编辑框所能接收的最小值
/// </summary>
public decimal MinValue
{
get
{
return fMinValue;
}
set
{
fMinValue = value;
}
}
/// <summary>
/// 小数点后的位数,默认是零
/// </summary>
public short Decimal
{
get
{
return nDecimal;
}
set
{
nDecimal = value;
}
}
protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{
bool bValid = false;
for(int i = 0; i < ValidChar.Length; i++)
{
if(e.KeyChar == ValidChar[i])
{
bValid = true;
break;
}
}
if(!bValid)
{
Kernel32.Beep(900, 100);
e.Handled = true;
}
else
{
if(Text != "")
{
if(e.KeyChar == '-')
{
if(Text.IndexOf('-') >= 0)
{
Kernel32.Beep(900, 100);
e.Handled = true;
}
else
{
Text = "-" + Text;
e.Handled = true;
}
}
if(((e.KeyChar == '.') & (Text.IndexOf('.') >= 0)) |
((e.KeyChar == '.') & (nDecimal == 0)))
{
Kernel32.Beep(900, 100);
e.Handled = true;
}
}
}
base.OnKeyPress(e);
}
protected override void OnTextChanged(EventArgs e)
{
string sTemp = "";
try
{
if(Text != "")
{
if(Text != "-")
{
if(nDecimal > 0)
{
Convert.ToDecimal(Text.Trim());
sTemp = Text.Trim();
int i = sTemp.IndexOf('.');
if((i != -1) & (sTemp.Length > i + 1 + nDecimal))
{
sTemp = Text.Substring(0, i + 1 + nDecimal);
Kernel32.Beep(900, 100);
}
Text = sTemp;
sOldString = sTemp;
}
else
{
Convert.ToInt32(Text.Trim());
sTemp = Text.Trim();
// if(Convert.ToInt32(sTemp.Trim()) > fMaxValue)
// {
// MessageBox.Show("输入的数据不能大于 " +
// Convert.ToString(fMaxValue) + " 。", "提示",
// MessageBoxButtons.OK, MessageBoxIcon.Information);
//
// Text = sOldString;
// }
// else if(Convert.ToInt32(sTemp.Trim()) < fMinValue)
// {
// MessageBox.Show("输入的数据不能小于 " +
// Convert.ToString(fMinValue) + " 。", "提示",
// MessageBoxButtons.OK, MessageBoxIcon.Information);
//
// Text = sOldString;
// }
// else
{
Text = sTemp;
sOldString = sTemp;
}
}
}
else
{
sOldString = "-";
}
}
else
{
sOldString = "";
}
}
catch
{
Text = sOldString;
Kernel32.Beep(900, 100);
}
base.OnTextChanged(e);
}
protected override void OnLeave(EventArgs e)
{
if (Text.Trim().Length == 0)
{
Text = "0";
}
if(Text != "-")
{
if(nDecimal > 0)
{
if(Convert.ToDecimal(Text.Trim()) > fMaxValue)
{
MessageBox.Show("输入的数据不能大于 " +
Convert.ToString(fMaxValue) + " 。", "提示",
MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Focus();
}
else if(Convert.ToDecimal(Text.Trim()) < fMinValue)
{
MessageBox.Show("输入的数据不能小于 " +
Convert.ToString(fMinValue) + " 。", "提示",
MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Focus();
}
}
else
{
if(Convert.ToInt32(Text.Trim()) > fMaxValue)
{
MessageBox.Show("输入的数据不能大于 " +
Convert.ToString(fMaxValue) + " 。", "提示",
MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Focus();
}
else if(Convert.ToInt32(Text.Trim()) < fMinValue)
{
MessageBox.Show("输入的数据不能小于 " +
Convert.ToString(fMinValue) + " 。", "提示",
MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Focus();
}
}
}
else
{
MessageBox.Show("输入的数字无效!", "提示",
MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Focus();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -