写一个自定义的TextBox,继承自TextBox,在这个自定义控件中实现只能输入数字的功能,然后窗体上的60个TextBox使用这个自定义控件。
解决方案 »
- 签约都应该问些什么问题啊。。。
- XmlSerializer如何序列化Hashtable?
- 请教高手:C# winform程序如何在新选项卡中打开一个网页
- 还是关于dll调用问题。
- 在日历控件中如何规定用户只能选择每个星期的星期四
- 用Remoting激活远程对象时出现“类型的权限被拒绝”的异常,是什么回事?
- 如何让窗体一运行就全屏
- webbrowser在打开网页时,会抢焦点,如何能让它不抢焦点
- 求个思路:关于listView中双击不同图片,触发事件。欢迎探讨,集思广益!
- C#中如何操作Oracle里面的Clob字段,急
- 急急急啊!!!有个问题弄不明白请帮忙(Webfrom间怎么样在页面的public定义的方法中传参数
- 辛苦积攒200分向大家请教:使用remote连接上远程对象,在随后的引用中发生了错误.....(ok后,立马结帐)
using System.Windows.Forms;
using System.ComponentModel;
using Caiao.MiniBms.Public;
using System.Drawing;namespace MYControls
{
/// <summary>
/// 数字文本框控件
/// </summary>
[ToolboxBitmap(typeof(TextBox))]
public class MNumberTextBox : MTextBox
{
public MNumberTextBox():base()
{
this.InputType = NumEditType.Integer;
this.ContextMenu = new ContextMenu();
}
#region 变量 Variable
private bool allowMinus; #endregion
#region 属性
/// <summary>
/// 是否允许负号
/// </summary>
[Category(Const.CaiaoCompany),
Description("是否允许负号")]
public bool AllowMinus
{
get { return allowMinus;}
set{ allowMinus = value;}
}
#endregion private NumEditType m_inpType;
public enum NumEditType
{
Currency,
Decimal,
Single,
Double,
SmallInteger,
Integer,
LargeInteger
} [Description("设置数字类型"), Category( Const.CaiaoCompany )]
public NumEditType InputType
{
get{return m_inpType;}
set
{
m_inpType = value;
}
}
public override string Text
{
get{return base.Text;}
set
{
if(IsValid(value, true))
base.Text = value;
}
}
private bool IsValid(string val, bool user)
{
// this method validates the ENTIRE string
// not each character
// Rev 1: Added bool user param. This bypasses preliminary checks
// that allow -, this is used by the OnLeave event
// to prevent
bool ret = true;
user = allowMinus; if(val == null || val.Equals("")
|| val.Equals(String.Empty))
return ret;
if(user)
{
// allow first char == '-'
if(val.Equals("-"))
return ret;
} // if(Min < 0 && val.Equals("-"))
// return ret; // parse into dataType, errors indicate invalid value
// NOTE: parsing also validates data type min/max
try
{
switch(m_inpType)
{
case NumEditType.Currency:
decimal dec = decimal.Parse(val);
int pos = val.IndexOf(".");
if(pos != -1)
ret = val.Substring(pos).Length <= 3; // 2 decimals + "."
//ret &= Min <= (double)dec && (double)dec <= Max;
break;
case NumEditType.Single:
float flt = float.Parse(val);
//ret &= Min <= flt && flt <= Max;
break;
case NumEditType.Double:
double dbl = double.Parse(val);
//ret &= Min <= dbl && dbl <= Max;
break;
case NumEditType.Decimal:
decimal dec2 = decimal.Parse(val);
//ret &= Min <= (double)dec2 && (double)dec2 <= Max;
break;
case NumEditType.SmallInteger:
short s = short.Parse(val);
//ret &= Min <= s && s <= Max;
break;
case NumEditType.Integer:
int i = int.Parse(val);
//ret &= Min <= i && i <= Max;
break;
case NumEditType.LargeInteger:
long l = long.Parse(val);
//ret &= Min <= l && l <= Max;
break;
default:
throw new ApplicationException();
}
}
catch
{
ret = false;
}
return ret;
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
// trap Ctrl-V paste and prevent invalid values
// return false to allow further processing
if(keyData == (Keys)Shortcut.CtrlV || keyData == (Keys)Shortcut.ShiftIns)
{
IDataObject iData = Clipboard.GetDataObject();
// assemble new string and check IsValid
string newText;
newText = base.Text.Substring(0, base.SelectionStart)
+ (string)iData.GetData(DataFormats.Text)
+ base.Text.Substring(base.SelectionStart + base.SelectionLength); // check if data to be pasted is convertable to inputType
if(!IsValid(newText, true))
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
} protected override void OnLeave(EventArgs e)
{
// handle - and leading zeros input since KeyPress handler must allow this
if(base.Text != "")
{
if(!IsValid(base.Text, false))
base.Text = "";
else if(Double.Parse(base.Text) == 0) // this used for -0, 000 and other strings
base.Text = "0";
}
base.OnLeave(e);
} protected override void OnKeyPress(KeyPressEventArgs e)
{
// assemble new text with new KeyStroke
// and pass to validation routine. // NOTES;
// 1) Delete key is NOT passed here
// 2) control passed here after ProcessCmdKey() is run char c = e.KeyChar;
if(!Char.IsControl(c)) // not sure about this?? nothing in docs about what is Control char??
{
// prevent spaces
if(c.ToString() == " ")
{
e.Handled = true;
return;
} string newText = base.Text.Substring(0, base.SelectionStart)
+ c.ToString() + base.Text.Substring(base.SelectionStart + base.SelectionLength);
if(!IsValid(newText, true))
e.Handled = true;
}
base.OnKeyPress(e);
}
}
其它也这样做,可以直接复制这个textBox