VB.NET按钮计算然后汇总



我正在创建一个程序,如果我单击button5(summary),它将不会显示摘要报告,直到我第一次单击button1(compute)。我该怎么做?

相关代码:

Private Sub Button5_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles Button5.Click
   If Not IsNumeric(TextBox6.Text) Or _
       Not IsNumeric(TextBox7.Text) Or _
       Not IsNumeric(TextBox8.Text) Or _
       Not IsNumeric(TextBox9.Text) Then
     MessageBox.Show("Invalid Input", "Warning", _
       MessageBoxButtons.OK, MessageBoxIcon.Warning)
   ElseIf 99 >= TextBox6.Text Or _
       99 >= TextBox7.Text Or _
       99 >= TextBox8.Text Or _
       99 >= TextBox9.Text Then
     MessageBox.Show("Invalid Amount", "Warning", _
       MessageBoxButtons.OK, MessageBoxIcon.Warning)
   Else
     Form5.Show()
     Me.Hide()
     Form5.Label3.Text = TextBox1.Text
     Form5.Label5.Text = TextBox2.Text
     Form5.Label7.Text = TextBox3.Text
     Form5.Label9.Text = ComboBox1.Text
     Form5.Label11.Text = TextBox4.Text
     Form5.Label13.Text = ComboBox2.Text
     Form5.Label15.Text = Label27.Text
     Form5.Label22.Text = Label14.Text
     Form5.Label17.Text = Label22.Text
     Form5.Label19.Text = Label25.Text
   End If
End Sub

尝试在Button5_Click处理程序开始时调用Button1_Click()处理程序:

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    Button1_Click() 'Call the handler at the start of your subroutine
    If Not IsNumeric(TextBox6.Text) Or Not IsNumeric(TextBox7.Text) Or Not IsNumeric(TextBox8.Text) Or Not IsNumeric(TextBox9.Text) Then
        MessageBox.Show("Invalid Input", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    ElseIf 99 >= TextBox6.Text Or 99 >= TextBox7.Text Or 99 >= TextBox8.Text Or 99 >= TextBox9.Text Then
        MessageBox.Show("Invalid Amount", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)

    Else
        Form5.Show()
        Me.Hide()
        Form5.Label3.Text = TextBox1.Text
        Form5.Label5.Text = TextBox2.Text
        Form5.Label7.Text = TextBox3.Text
        Form5.Label9.Text = ComboBox1.Text
        Form5.Label11.Text = TextBox4.Text
        Form5.Label13.Text = ComboBox2.Text
        Form5.Label15.Text = Label27.Text
        Form5.Label22.Text = Label14.Text
        Form5.Label17.Text = Label22.Text
        Form5.Label19.Text = Label25.Text
    End If
End Sub

假设Button1_Click()处理"计算"部分,这应该对您有效。

一种更简单的方法是编写上下文命名的子例程,然后从单击处理程序中调用这些例程。

示例:

Private Sub Compute()
    'Do computations
    'Use a function instead of a sub if you need to return a value.
End Sub
Private Sub Summarize()
    If Not IsNumeric(TextBox6.Text) Or Not IsNumeric(TextBox7.Text) Or Not IsNumeric(TextBox8.Text) Or Not IsNumeric(TextBox9.Text) Then
        MessageBox.Show("Invalid Input", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    ElseIf 99 >= TextBox6.Text Or 99 >= TextBox7.Text Or 99 >= TextBox8.Text Or 99 >= TextBox9.Text Then
        MessageBox.Show("Invalid Amount", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    Else
        Form5.Show()
        Me.Hide()
        Form5.Label3.Text = TextBox1.Text
        Form5.Label5.Text = TextBox2.Text
        Form5.Label7.Text = TextBox3.Text
        Form5.Label9.Text = ComboBox1.Text
        Form5.Label11.Text = TextBox4.Text
        Form5.Label13.Text = ComboBox2.Text
        Form5.Label15.Text = Label27.Text
        Form5.Label22.Text = Label14.Text
        Form5.Label17.Text = Label22.Text
        Form5.Label19.Text = Label25.Text
    End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Compute() 'Call compute subroutine
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    Compute()   'Call compute subroutine
    Summarize() 'Call summarize subroutine
End Sub

最新更新