现在我的窗体有60个textBox,每个都只能输入数字,那么要写60个private void textBox3_KeyDown(object sender, System.Windows.Forms.Key

65426 2003-08-21 12:04:40
现在我的窗体有60个textBox,每个都只能输入数字,那么要写60个private void textBox3_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e),
有没有别方法?
...全文
60 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
gang75vb 2004-01-10
  • 打赏
  • 举报
回复
markup
hunter4500 2003-10-12
  • 打赏
  • 举报
回复
up
minajo21 2003-08-21
  • 打赏
  • 举报
回复
http://www.csdn.net/Develop/Read_Article.asp?Id=20241
minajo21 2003-08-21
  • 打赏
  • 举报
回复
写一个公用的函数,让60个textBox的KeyDown事件都响应这一个函数

做法是:选择一个textBox的属性页的事件按钮(小闪电),在KeyDown事件里写公用函数的名字


其它也这样做,可以直接复制这个textBox
seakingii 2003-08-21
  • 打赏
  • 举报
回复
using System;
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);
}












}
TheAres 2003-08-21
  • 打赏
  • 举报
回复
写一个自定义的TextBox,继承自TextBox,在这个自定义控件中实现只能输入数字的功能,然后窗体上的60个TextBox使用这个自定义控件。
yezhitu 2003-08-21
  • 打赏
  • 举报
回复
用控件数组就是行了吗?

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧