winform文本框,如何只允许输入数字和小数点?

menganafff 2008-01-26 08:43:08
多谢!
...全文
921 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
myhomeland 2008-02-18
  • 打赏
  • 举报
回复
用正则表达式吧,用KeyPress事件下判断输入的字符的ASCII编码值有一定的局限,比如说对两个小数点及以上的还要单独判断,其实用现成的numericUpDown就可以了
menganafff 2008-01-26
  • 打赏
  • 举报
回复
用ASCII编码值来判断,英文虽然不能输入了,但中文还是能输入的
ydfy6 2008-01-26
  • 打赏
  • 举报
回复
或者用ASCII判断
ydfy6 2008-01-26
  • 打赏
  • 举报
回复
用正则表达式可以

benniaoyaofei 2008-01-26
  • 打赏
  • 举报
回复
^[-]?\d+[.]?\d*$
Sali 2008-01-26
  • 打赏
  • 举报
回复
在要求输入数字的控件的KeyPress事件下判断输入的字符的ASCII编码值(0的ASCII编码值是48,9的ASCII编码值是57,小数点的ASCII编码值是46),如果输入的符合要求则不作处理,如果不符合要求,则添加代码:e.Handle=true
peterb 2008-01-26
  • 打赏
  • 举报
回复
在onchange事件用正则表达式验证也可以
menganafff 2008-01-26
  • 打赏
  • 举报
回复
我用的是2003,找不到MaskedTextBox

只接受数字不行,我还要输入小数点啊
jimgreat 2008-01-26
  • 打赏
  • 举报
回复
这是个继承TextBox写成的只接收数字的TextBox

using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Windows.Forms.Design;

namespace CustomControls
{
/// <summary>
/// NumbersOnlyTextBox class
/// </summary>
[Designer(typeof(NumbersOnlyTextBox.NumbersOnlyTextBoxDesigner))]
public class NumbersOnlyTextBox:System.Windows.Forms.TextBox
{
[DllImport("user32.dll")]
private static extern bool MessageBeep(uint uType);

public override bool PreProcessMessage(ref Message msg)
{
int keyValue = msg.WParam.ToInt32();
if ((keyValue > 47 && keyValue < 58) || keyValue == 46 || (keyValue > 34 && keyValue < 41) || keyValue == 8)
{
return base.PreProcessMessage(ref msg);
}
else
{
//Delete Key
if (msg.Msg == 256 && keyValue == 46)
{
return base.PreProcessMessage(ref msg);
}
//Windows message id used to limit 1 beep per keystroke
if (msg.Msg == 258)
{
MessageBeep(0);
}

return true;

}

}

public NumbersOnlyTextBox()
{


}


#region Internal Classes
/// <summary>
///
/// </summary>
internal class NumbersOnlyTextBoxDesigner : System.Windows.Forms.Design.ControlDesigner
{
#region Method Overrides
protected override void PostFilterProperties(System.Collections.IDictionary properties)
{
//base.PostFilterProperties(properties);
properties.Remove("Text");
}
#endregion
}

#endregion

}



}

lalac 2008-01-26
  • 打赏
  • 举报
回复
1、改用MaskedTextBox,然后设置Mask即可。这里有一个示例:

下面的代码示例初始化 MaskedTextBox 以接受日期,并且将使用 MaskInputRejected 和 TypeValidationCompleted 事件来警告用户输入了无效值。

private void Form1_Load(object sender, EventArgs e)
{
maskedTextBox1.Mask = "00/00/0000";

maskedTextBox1.MaskInputRejected += new MaskInputRejectedEventHandler(maskedTextBox1_MaskInputRejected);
maskedTextBox1.KeyDown += new KeyEventHandler(maskedTextBox1_KeyDown);
}

void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
{
if (maskedTextBox1.MaskFull)
{
toolTip1.ToolTipTitle = "Input Rejected - Too Much Data";
toolTip1.Show("You cannot enter any more data into the date field. Delete some characters in order to insert more data.", maskedTextBox1, maskedTextBox1.Location.X, maskedTextBox1.Location.Y, 5000);
}
else if (e.Position == maskedTextBox1.Mask.Length)
{
toolTip1.ToolTipTitle = "Input Rejected - End of Field";
toolTip1.Show("You cannot add extra characters to the end of this date field.", maskedTextBox1, maskedTextBox1.Location.X, maskedTextBox1.Location.Y, 5000);
}
else
{
toolTip1.ToolTipTitle = "Input Rejected";
toolTip1.Show("You can only add numeric characters (0-9) into this date field.", maskedTextBox1, maskedTextBox1.Location.X, maskedTextBox1.Location.Y, 5000);
}
}

void maskedTextBox1_KeyDown(object sender, KeyEventArgs e)
{
// The balloon tip is visible for five seconds; if the user types any data before it disappears, collapse it ourselves.
toolTip1.Hide(maskedTextBox1);
}


2、在KeyPress中处理键入的值,并把e.Handle设置为true表示处理了。
lovexin 2008-01-26
  • 打赏
  • 举报
回复
欢望中呀..
manonroad 2008-01-26
  • 打赏
  • 举报
回复
重写OnKeysDown或者OnKeysUp
applethink 2008-01-26
  • 打赏
  • 举报
回复
在keydown 事件里判断键入的值就行了

110,567

社区成员

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

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

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