vb.net Visual Basic 温度转换程序显示错误值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14372396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:54:28  来源:igfitidea点击:

Visual basic temperature conversion program displaying incorrect values

vb.net

提问by Hus

I haven't programmed in visual basic in years but I have this code that is suppose to convert temperatures from fahrenheit to celcius. The problem is when you enter a number into one of the text boxes, you get repeating numbers and the values are incorrect. I think it has something to do with the Handles but I am pretty lost here, any ideas?

我已经多年没有在 Visual Basic 中编程了,但我有这段代码可以将温度从华氏温度转换为摄氏度。问题是当您在其中一个文本框中输入一个数字时,您会得到重复的数字并且值不正确。我认为它与手柄有关,但我在这里很迷茫,有什么想法吗?

Public Class MainForm

    Private Sub FahrenheitTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FahrenheitTextBox.TextChanged
        CelciusTextBox.Text = 5 / 9 * (Val(FahrenheitTextBox.Text) - 32)
    End Sub

    Private Sub CelciusTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CelciusTextBox.TextChanged
        FahrenheitTextBox.Text = (9 / 5 * (Val(CelciusTextBox.Text)) + 32)
    End Sub
End Class

回答by prprcupofcoffee

This isn't an answer, per se, but I wanted to make some suggestions that will not show up well in a comment. If you are using the TextChanged event, you should guard against unwanted events, e.g., typing into one text box, which fires TextChanged, which causes the other text box to change, firing TextChanged, causing the text box you're typing into to change.

这本身并不是一个答案,但我想提出一些不会很好地出现在评论中的建议。如果您正在使用 TextChanged 事件,您应该防范不需要的事件,例如,在一个文本框中键入内容会触发 TextChanged,从而导致另一个文本框发生更改,触发 TextChanged,从而导致您正在键入的文本框发生更改.

Try something like this:

尝试这样的事情:

Public Class MainForm

    Private textChanging As Boolean = False

    Private Sub FahrenheitTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FahrenheitTextBox.TextChanged
        If Not textChanging Then
            textChanging = True
            CelciusTextBox.Text = 5 / 9 * (Val(FahrenheitTextBox.Text) - 32)
            textChanging = False
        End If
    End Sub

    Private Sub CelciusTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CelciusTextBox.TextChanged
        If Not textChanging Then
            textChanging = True
            FahrenheitTextBox.Text = (9 / 5 * (Val(CelciusTextBox.Text)) + 32)
            textChanging = False
        End If
    End Sub

End Class

Also, you should use CStrto convert from a number to a string, in the same way you use Valto convert from a string to a number:

此外,您应该使用CStr将数字转换为字符串,就像Val将字符串转换为数字一样:

Private Sub FahrenheitTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FahrenheitTextBox.TextChanged
    If Not textChanging Then
        textChanging = True
        CelciusTextBox.Text = CStr(5 / 9 * (Val(FahrenheitTextBox.Text) - 32))
        textChanging = False
    End If
End Sub

Finally, I've re-tagged your question - this is VB.NET, not VB6. Thanks!

最后,我重新标记了您的问题 - 这是 VB.NET,而不是 VB6。谢谢!