简体   繁体   English

如何验证文本框中输入的每个新字符

[英]How to validate every new character inputted in a textbox

So I've been working on this Temperature Converter from Fahrenheit to Celsius and vice-versa, and basically what it does is that when the user inputs a number in a respective textbox it automatically shows the converted result in a label under it, I'm trying to not make possible for someone to write a letter in the textbox and I've succeeded when the person types the first character as a letter, but when the user inputs a number and then a letter the program tries to calculate and it crashes, I don't know how to make the if function verify every new character input BEFORE calculating it and displaying the result 因此,我一直在研究从华氏温度到摄氏温度,反之亦然的温度转换器,基本上它的作用是,当用户在相应的文本框中输入数字时,它会自动在其下方的标签中显示转换后的结果, m试图使某人无法在文本框中写一个字母,并且当该人将第一个字符键入字母时,我成功了,但是当用户输入数字然后输入字母时,程序尝试计算并崩溃,我不知道在计算和显示结果之前,如何使if函数验证每个新字符输入

I've tried to use Val(txtFahrenheit.Text) but it only verifies the first character and also I've tried Char.isDigit(txtFahrenheit.Text) = False and it also only verifies the first character and I get the same output. 我尝试使用Val(txtFahrenheit.Text),但它只验证第一个字符,而且我也尝试过Char.isDigit(txtFahrenheit.Text)= False,它也仅验证第一个字符,并且得到相同的输出。

' This section is executed after the respective textbox is changed
Private Sub txtFahrenheit_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtFahrenheit.TextChanged

  ' This section sets a pop-up when the user's input equals to eight characters
  If txtFahrenheit.TextLength = 8 Then
    MessageBox.Show("You have exceeded the max number of input characters")
  End If

  ' This section validates the user's input, enables the Clear Button, calculates the reuslt and displays it
  If Char.isDigit(txtFahrenheit.Text) = True Then
    btnClear.Enabled = True
    Dim fahrenCel As Integer = (txtFahrenheit.Text - 32) / 1.8
    lblResultC.Text = fahrenCel.ToString + " °C"
  End If

  ' This section verifies if the user inputs a letter first and removes it also displaying a pop-up
  If Val(txtFahrenheit.Text) = False And (txtFahrenheit.Text = "0") = False And (txtFahrenheit.Text = String.Empty) = False Then
    txtFahrenheit.Text = String.Empty
    lblResultC.Text = String.Empty
    MessageBox.Show("The input is not valid. Please use only numbers")
    Return
  End If

  ' This section clears the label result when the text box is empty
  If txtFahrenheit.Text = "" Then
    lblResultC.Text = String.Empty
    Return
  End If

End Sub

The simplest (and in my humble opinion the best) answer is to let WinForms handle the data validation for you by using a NumericUpDown control. 最简单的答案(我认为是最好的)是让WinForms通过使用NumericUpDown控件为您处理数据验证。 You would handle the ValueChanged event to do you conversion and get the Value of the control. 您将处理ValueChanged事件以进行转换并获取控件的Value

It is also worth mentioning that you should probably move your conversion into a separate function. 还值得一提的是,您可能应该将转换移动到单独的函数中。

You can try using try catch like this: 您可以尝试使用try catch如下所示:

try
{
    double d = Convert.ToDouble(txtFahrenheit.Text);//
    // Calculate the equivalent Celsius and show it
}
catch (Exception)
{
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM