算术运算在vb.net中的插图

时间:2014-07-25 18:09:24

标签: vb.net

我有3个名为FirstNumber,SecondNumber和ThirdNumber的文本框和3个用于操作选择的单选按钮。当点击某个单选按钮时,某个结果会显示在标签中。但是,除非我在运行程序之前在文本框中输入数字,否则结果将显示为0.我试图通过以下代码执行此操作,但是没有得到预期结果,建议一些提示修复此问题?

Dim FirstNumber As Integer
Dim SecondNumber As Integer
Dim ThirdNumber As Integer
Dim Result1 As Integer
Dim Result2 As Integer
Dim Result3 As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    FirstNumber = CInt(TextBox1.Text)
    SecondNumber = CInt(TextBox2.Text)
    ThirdNumber = CInt(TextBox3.Text)
    Result1 = FirstNumber + SecondNumber - ThirdNumber
    Result2 = FirstNumber - ThirdNumber / SecondNumber
    Result3 = SecondNumber Mod ThirdNumber
End Sub

3 个答案:

答案 0 :(得分:1)

因为Form_Load在加载Form时运行,所以在事件发生之前就会看到它。 TextBox es当时仍然是空的。

您应该将代码放在(例如)Click事件中添加到表单的Button。然后在TextBoxes中输入文本并单击按钮以查看结果。

Dim FirstNumber As Integer
Dim SecondNumber As Integer
Dim ThirdNumber As Integer
Dim Result1 As Integer
Dim Result2 As Integer
Dim Result3 As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    FirstNumber = CInt(TextBox1.Text)
    SecondNumber = CInt(TextBox2.Text)
    ThirdNumber = CInt(TextBox3.Text)
    Result1 = FirstNumber + SecondNumber - ThirdNumber
    Result2 = FirstNumber - ThirdNumber / SecondNumber
    Result3 = SecondNumber Mod ThirdNumber
End Sub

答案 1 :(得分:0)

将代码从Form1_Load移动到sub,然后在需要时调用sub ...例如,当用户更改文本框中的值时。

Private Sub doCalculation()
    FirstNumber = CInt(TextBox1.Text)
    SecondNumber = CInt(TextBox2.Text)
    ThirdNumber = CInt(TextBox3.Text)
    Result1 = FirstNumber + SecondNumber - ThirdNumber
    Result2 = FirstNumber - ThirdNumber / SecondNumber
    Result3 = SecondNumber Mod ThirdNumber
End Sub

Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
    call doCalculation()
End Sub

答案 2 :(得分:0)

这是代码对应于问题中提出所有功能的问题:

Public Class Form2

    Private Sub doCalculation(ByVal op As Integer)
        Dim FirstNumber As Integer = CInt(TextBox1.Text)
        Dim SecondNumber As Integer = CInt(TextBox2.Text)
        Dim ThirdNumber As Integer = CInt(TextBox3.Text)
        Dim Result As Integer
        If SecondNumber = 0 Then
            MsgBox("unable to proceed.. Second text box cannot be left blank")
            TextBox2.Focus
        Else
        Select Case op
            Case 0 : Result = FirstNumber + SecondNumber - ThirdNumber
            Case 1 : Result = FirstNumber - ThirdNumber / SecondNumber
            Case 2 : Result = SecondNumber Mod ThirdNumber
        End Select
        MsgBox(Result)
        End If
    End Sub
    Private Sub moduls_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles moduls.Click
        doCalculation(2)
    End Sub

    Private Sub addsub_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles addsub.Click
        doCalculation(0)
    End Sub

    Private Sub subdiv_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles subdiv.Click
        doCalculation(1)
    End Sub
End Class

其中 subdiv,addsub和moduls 是单选按钮