在 Win Forms 中验证数字格式 [英] Validating numeric formats in Win Forms

查看:33
本文介绍了在 Win Forms 中验证数字格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 win 表单控件上有许多文本框需要输入验证.

I have a number of text boxes on a win forms control that need their input validating.

如何验证以下格式的数字

How do I validate for a number in the following format

nn.nn

另外,我如何验证一个数字是一个正整数

also, how do I validate that a number is a positive integer

谢谢

推荐答案

要验证有效数字,可以在文本框的 leave 事件中使用 isnumeric 函数.如果它不是数字,则将焦点设置回 textbox1.比较 int(x) 和 x 的值,看它是否是一个整数.

To verify a valid number, you can use the isnumeric function in the leave event of the textbox. If it is not numeric, set the focust back to the textbox1. Compare the value of int(x) to x to see if it's an integer.

If Not IsNumeric(txAngle.Text) Then
  MsgBox("Enter a number between -360 and 360.")
  txAngle.Focus()
else
  x = CDbl(txAngle.Text)
  if int(x) <> x then ' test for integer
  ...
end if

这篇关于在 Win Forms 中验证数字格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆