使用复选框在VB中设置“If”语句的问题

时间:2017-02-07 05:16:10

标签: vb.net

我正在尝试弄清楚如何在选中复选框并在文本框中显示时分配给复选框的值。这是一个奖金问题,并没有在课堂上覆盖,所以作为一个初学者,我可能会在多个领域迷失。提前谢谢。

Public Class frmBooking
    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged

    End Sub

    Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles btnCompute.Click
        Dim baseCruiseCosts, prepaidGratuities, salesTax, grandTotal As Single
        Dim travelOptions As Double
        baseCruiseCosts = txtCosts.Text
        travelOptions = txtTravelOps.Text
        prepaidGratuities = txtGratuities.Text
        salesTax = txtTax.Text
        grandTotal = baseCruiseCosts + travelOptions + prepaidGratuities + salesTax
        txtGrandTotal.Text = FormatCurrency(grandTotal)
    End Sub

    Private Sub frmBooking_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        lblDate.Text = Format(Now, "MM/dd/yyyy")
    End Sub

    Private Sub chkInsurance_CheckedChanged(sender As Object, e As EventArgs) Handles chkInsurance.CheckedChanged
        Dim sum, travelOptions As Double
        If chkInsurance.Checked Then
            sum += 90
        End If
        If chkInsurance.Checked = False Then
            txtTravelOps.Text = travelOptions
        End If
    End Sub

    Private Sub chkTickets_CheckedChanged(sender As Object, e As EventArgs) Handles chkTickets.CheckedChanged
        Dim sum, travelOptions As Double
        If chkTickets.Checked Then
            sum += 30
        End If
        If chkTickets.Checked = False Then
            txtTravelOps.Text = travelOptions
        End If
    End Sub

    Private Sub chkBottomless_CheckedChanged(sender As Object, e As EventArgs) Handles chkBottomless.CheckedChanged
        Dim sum, travelOptions As Double
        If chkBottomless.Checked Then
            sum += 85
        End If
        If chkBottomless.Checked = False Then
            txtTravelOps.Text = travelOptions
        End If
    End Sub

    Private Sub chkPreReg_CheckedChanged(sender As Object, e As EventArgs) Handles chkPreReg.CheckedChanged
        Dim sum, travelOptions As Double
        If chkPreReg.Checked Then
            sum += 19.95
        End If
        If chkPreReg.Checked = False Then
            txtTravelOps.Text = travelOptions
        End If
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

你在checkChanged中创建了sum变量,因此它只在checkChanged中可用。你必须在表单加载之上创建一个全局变量或主变量(我不知道它的术语,所以我只称它为global / main。)

您可以执行以下操作:

创建4个变量: 1. grandTotal,sumofTicket,sumofInsurance,sumofBottomless,sumofPreReg。

当他们检查复选框时,值必须转移到指定的变量,当他们点击btncompute时,将所有变量添加到grandTotal变量。复选框where = false的条件中的变量也没有任何值。