gpt4 book ai didi

c# - 如何使文本框在 Windows 8 中只接受数字和一位小数

转载 作者:太空狗 更新时间:2023-10-30 00:19:29 24 4
gpt4 key购买 nike

我是 Windows 8 手机的新手。我正在编写一个计算器应用程序,它只能接受文本框中的数字和一个小数点。由于计算器无法处理,我该如何防止用户在文本框中输入两个或更多小数点。

我一直在使用 Keydown Event,这是最好的还是我应该使用 Key up?

private void textbox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) {

}

最佳答案

您可以将其用于 KeyPress 事件 在表单中为您的文本框设置按键事件。像这样的设计器

this.yourtextbox.KeyPress +=new System.Windows.Forms.KeyPressEventHandler(yourtextbox_KeyPress);

然后在您的表单中使用它

//only number And single decimal point input فقط عدد و یک ممیز میتوان نوشت
public void onlynumwithsinglepoint(object sender, KeyPressEventArgs e)
{
if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == '.'))
{ e.Handled = true; }
TextBox txtDecimal = sender as TextBox;
if (e.KeyChar == '.' && txtDecimal.Text.Contains("."))
{
e.Handled = true;
}
}

然后

private void yourtextbox_KeyPress(object sender, KeyPressEventArgs e)
{
onlynumwithsinglepoint(sender, e);
}

关于c# - 如何使文本框在 Windows 8 中只接受数字和一位小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19761487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com