我正在开发一个计算门票销售额的可视化基础应用程序(带有Visual Studio 2010)。这个是非常具有挑战性的,因为我是视觉基础的新手。
我无法获取计算按钮 以下是从功能中获取成本并使用它们计算总数的说明。以下是他们给我的指示。
我被困在计算按钮上。我只需要能够从按钮中捕获成本,然后使用按钮来计算它。
Public Class Form1
'Global Variables
Dim intTicketChoice As Integer
Dim finalCost As Decimal
Dim cost As Decimal
Private Sub cboTicketType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboTicketType.SelectedIndexChanged
intTicketChoice = Me.cboTicketType.SelectedIndex
Me.lstSeatType.Items.Clear()
Select Case intTicketChoice
Case 0 : SingleGame()
Case 1 : Seasonal()
End Select
'Make Items visible
Me.lblCostDisplay.Visible = True
Me.lblSeats.Visible = True
Me.lblTickets.Visible = True
Me.lstSeatType.Visible = True
Me.txtTicketNum.Visible = True
Me.btnClear.Visible = True
Me.btnCompute.Visible = True
Me.txtTicketNum.Focus()
End Sub
Private Sub SingleGame()
Dim seatType As Integer
'Add List Items
Me.lstSeatType.Items.Add("Box Seats $55")
Me.lstSeatType.Items.Add("Lower Deck Seats $35")
Me.lstSeatType.Items.Add("Upper Deck Seats $25")
Me.lstSeatType.Items.Add("Standing Room Only $15")
If lstSeatType.SelectedItem = "Box Seats $55" Then
seatType = 0
End If
If lstSeatType.SelectedItem = "Lower Deck Seats $35" Then
seatType = 1
End If
If lstSeatType.SelectedItem = "Upper Deck Seats $25" Then
seatType = 2
End If
If lstSeatType.SelectedItem = "Standing Room Only $15" Then
seatType = 3
End If
End Sub
Private Sub Seasonal()
Dim seatType As Integer
'Add List Items
Me.lstSeatType.Items.Add("Box Seats $2500")
Me.lstSeatType.Items.Add("Lower Deck Seats $1500")
'Price Items for Single Games
If lstSeatType.SelectedItem = "Box Seats $2500" Then
seatType = 4
End If
If lstSeatType.SelectedItem = "Lower Deck Seats $1500" Then
seatType = 5
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click
Dim ticketNum As Integer
Dim totalCost As Decimal
ticketNum = Convert.ToInt32(Me.txtTicketNum.Text)
intTicketChoice = Me.cboTicketType.SelectedIndex
Select Case intTicketChoice
Case 0 : totalCost = SingleGameCost()
Case 1 : totalCost = SeasonalCost()
End Select
'try and catch number textbox
Try
ticketNum = Convert.ToInt32(txtTicketNum.Text)
Catch Exception As FormatException
MsgBox("Number of tickets must be numeric")
Return
End Try
'display cost of tickets
Me.lblCostDisplay.Text = "The total cost of tickets purchased:" & totalCost.ToString("C")
End Sub
Private Function SingleGameCost(ByVal seatType As Integer, ByVal ticketNum As Integer)
finalCost = ticketNum * cost
'Price Items for Single Games
If seatType = 0 Then
cost = 55D
End If
If seatType = 1 Then
cost = 35D
End If
If seatType = 2 Then
cost = 25D
End If
If seatType = 3 Then
cost = 15D
End If
Return finalCost
End Function
Private Function SeasonalCost(ByVal seatType As Integer, ByVal ticketNum As Integer, ByRef cost As Decimal)
Dim finalCost As Decimal
If seatType = 4 Then
cost = 2500D
End If
If seatType = 0 Then
cost = 1500D
End If
finalCost = cost * ticketNum
Return finalCost
End Function
End Class
错误发生在这里:
If intTicketChoice = 0 Then
SingleGameCost()
End If
If intTicketChoice = 1 Then
SeasonalCost()
End If
使用singlegamecost()
函数和seasonacost()
函数。
答案 0 :(得分:0)
您需要将seatType作为公共变量。 然后,一旦他们选择了座位,您就设置了变量 然后在计算时使用变量。
然后返回费用。
在计算功能
中获得最终费用之后
me.lblCostDisplay.Text = finalCost.ToString()
答案 1 :(得分:0)
我已经为你修改了一下。从中得到你想要的东西。 我没有测试过。希望这有帮助...不确定为什么要形成 没出来很对..
Public Class Form1
Private SeatType As Integer = 0
Private Sub cboTicketType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboTicketType.SelectedIndexChanged
Dim intTicketChoice As Integer = Me.cboTicketType.SelectedIndex
Me.lstSeatType.Items.Clear()
Select Case intTicketChoice
Case 0
Me.lstSeatType.Items.Add("Box Seats $55")
Me.lstSeatType.Items.Add("Lower Deck Seats $35")
Me.lstSeatType.Items.Add("Upper Deck Seats $25")
Me.lstSeatType.Items.Add("Standing Room Only $15")
Case 1
Me.lstSeatType.Items.Add("Box Seats $2500")
Me.lstSeatType.Items.Add("Lower Deck Seats $1500")
Case Else
End Select
'Make Items visible
Me.lblCostDisplay.Visible = True
Me.lblSeats.Visible = True
Me.lblTickets.Visible = True
Me.lstSeatType.Visible = True
Me.txtTicketNum.Visible = True
Me.btnClear.Visible = True
Me.btnCompute.Visible = True
Me.txtTicketNum.Focus()
End Sub
Private Sub lstSeatType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstSeatType.SelectedIndexChanged
If lstSeatType.SelectedItem.ToString.ToLower = "box seats $55" Then
SeatType = 0
ElseIf lstSeatType.SelectedItem.ToString.ToLower = "lower deck seats $35" Then
SeatType = 1
ElseIf lstSeatType.SelectedItem.ToString.ToLower = "upper deck seats $25" Then
SeatType = 2
ElseIf lstSeatType.SelectedItem.ToString.ToLower = "standing room only $15" Then
SeatType = 3
ElseIf lstSeatType.SelectedItem.ToString.ToLower = "box seats $2500" Then
SeatType = 4
ElseIf lstSeatType.SelectedItem.ToString.ToLower = "lower deck seats $1500" Then
SeatType = 5
End If
End Sub
Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click
Dim sTicketNum As String = txtTicketNum.Text
If Not String.IsNullOrEmpty(sTicketNum) Then
Try
Dim ticketNum As Integer = Convert.ToInt32(Me.txtTicketNum.Text)
Dim totalCost As Decimal = GetGameCost(ticketNum)
'try and catch number textbox
Me.lblCostDisplay.Text = "The total cost of tickets purchased: " & totalCost.ToString("C")
Catch Exception As FormatException
MsgBox("Number of tickets must be numeric")
Return
End Try
'display cost of tickets
Else
MsgBox("Please input a ticket number.")
txtTicketNum.focus()
Return
End If
End Sub
Private Function GetGameCost(ByVal ticketNum As Integer) As Decimal
Dim finalCost As Decimal = 0
'Price Items for Single Games
Select Case SeatType
Case 0
finalCost = (ticketNum * 55D)
Case 1
finalCost = (ticketNum * 35D)
Case 2
finalCost = (ticketNum * 25D)
Case 3
finalCost = (ticketNum * 15D)
Case 4
finalCost = (ticketNum * 1500D)
Case 5
finalCost = (ticketNum * 2500D)
Case Else
End Select
'
Return finalCost
'
End Function
结束班
答案 2 :(得分:0)
上述代码存在许多问题。它需要重重构。但即使没有这个问题,由于以下原因,目前的形式无法完全回答这个问题 - 您在没有参数的情况下调用SingleGameCost
和SeasonalCost
,但是它们是用这样的方式声明的。
此代码无法编译。如果 编译在您身边,您可以使用一些代码来支持该问题。如果它没有编译,这就是答案。
关于重构,这样的条款:
If lstSeatType.SelectedItem = "Box Seats $55" Then
seatType = 0
End If
If lstSeatType.SelectedItem = "Lower Deck Seats $35" Then
seatType = 1
End If
If lstSeatType.SelectedItem = "Upper Deck Seats $25" Then
seatType = 2
End If
If lstSeatType.SelectedItem = "Standing Room Only $15" Then
seatType = 3
End If
可以改写:
Select Case lstSeatType.SelectedItem
Case "Box Seats $55" : seatType = 0
Case "Lower Deck Seats $35" : seatType = 1
Case "Upper Deck Seats $25" : seatType = 2
Case "Standing Room Only $15" : seatType = 3
End Select
即使如上所述,seatType
的值也会丢失,因为它被声明为局部变量。
答案 3 :(得分:0)
奇怪。我编写代码时没有错误,但我仍然需要为它编写清除代码 - 但我会继续并提供我目前的代码。
Public Class BaseballTickets
Private _strBoxSeats As String = "Box Seats"
Private _strLowerDeck As String = "Lower Deck"
Private _strUpperDeck As String = "Upper Deck"
Private _strStandingRoomOnly As String = "Standing Room Only"
Private Sub lblBaseballTicketSales_Click(sender As System.Object, e As System.EventArgs) Handles lblBaseballTicketSales.Click
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub cboxTicketType_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cboxTicketType.SelectedIndexChanged
Dim intTicketType As Integer
intTicketType = Me.cboxTicketType.SelectedIndex
lstSeatingType.Items.Clear()
Select Case intTicketType
Case 0
SeasonTickets()
Case 1
SingleGameTickets()
End Select
lblBaseballTicketSales.Visible = True
lblNumberOfGroup.Visible = True
lblTicketChoice.Visible = True
lblTicketCost.Visible = True
lblTicketCost.Text = ""
txtAmountOfPeople.Focus()
End Sub
Private Sub SeasonTickets()
lstSeatingType.Items.Add(_strBoxSeats)
lstSeatingType.Items.Add(_strLowerDeck)
End Sub
Private Sub SingleGameTickets()
lstSeatingType.Items.Add(_strBoxSeats)
lstSeatingType.Items.Add(_strLowerDeck)
lstSeatingType.Items.Add(_strStandingRoomOnly)
lstSeatingType.Items.Add(_strUpperDeck)
End Sub
Private Sub btnCost_Click(sender As System.Object, e As System.EventArgs) Handles btnCost.Click
Dim intGroupSize As Integer
Dim blnNumberInPartyIsValid As Boolean = False
Dim blnTicketIsSelected = False
Dim intTicketType As Integer
Dim strSelectedTicket As String = ""
Dim intTicketChoice As Integer
Dim decTotalCost As Decimal
blnNumberInPartyIsValid = ValidateNumberInParty()
intTicketChoice = ValidateTicketIsSelected(blnTicketIsSelected, strSelectedTicket)
If (blnNumberInPartyIsValid And blnTicketIsSelected) Then
intGroupSize = Convert.ToInt32(txtAmountOfPeople.Text)
intTicketType = Me.cboxTicketType.SelectedIndex
Select Case intTicketType
Case 0
decTotalCost = SeasonTicketsFindCost(intTicketType, _
intGroupSize)
Case 1
decTotalCost = SingleGameTicketsFindCost(intTicketType, _
intGroupSize)
End Select
lblTicketCost.Text = "The total cost of tickets purchased: " & decTotalCost.ToString("C")
End If
End Sub
Private Function ValidateNumberInParty() As Boolean
Dim intNumberOfPeople As Integer
Dim blnValidityCheck As Boolean = False
Dim strNumberInPartyErrorMessage As String = _
"Please Enter the Number of people Accompanying you (1-99)"
Dim strMessageErrorTitle As String = "Error"
Try
intNumberOfPeople = Convert.ToInt32(txtAmountOfPeople.Text)
If intNumberOfPeople > 0 And intNumberOfPeople < 100 Then
blnValidityCheck = True
Else
MsgBox(strNumberInPartyErrorMessage, , strMessageErrorTitle)
txtAmountOfPeople.Focus()
txtAmountOfPeople.Clear()
End If
Catch Exception As FormatException
MsgBox(strNumberInPartyErrorMessage, , strMessageErrorTitle)
txtAmountOfPeople.Focus()
txtAmountOfPeople.Clear()
Catch Exception As OverflowException
MsgBox(strNumberInPartyErrorMessage, , strMessageErrorTitle)
txtAmountOfPeople.Focus()
txtAmountOfPeople.Clear()
Catch Exception As SystemException
MsgBox(strNumberInPartyErrorMessage, , strMessageErrorTitle)
txtAmountOfPeople.Focus()
txtAmountOfPeople.Clear()
End Try
Return blnValidityCheck
End Function
Private Function ValidateTicketIsSelected(ByRef blnTicket As Boolean, _
ByRef strTicket As String) As Integer
Dim intTicketChoice As Integer
Try
intTicketChoice = Convert.ToInt32(lstSeatingType.SelectedIndex)
strTicket = lstSeatingType.SelectedItem.ToString()
blnTicket = True
Catch Exception As SystemException
' Detects if a tour not selected
MsgBox("Select a Ticket Choice", , "Error")
blnTicket = False
End Try
Return intTicketChoice
End Function
Private Function SeasonTicketsFindCost(ByVal intTicketSelection As Integer, _
ByVal intGroupSize As Integer) As Decimal
Dim decTicketCost As Decimal
Dim decFinalCost As Decimal
Dim decBoxSeats As Decimal = 2500D
Dim decLowerdeck As Decimal = 1500D
Select Case intTicketSelection
Case 0
decTicketCost = decBoxSeats
Case 1
decTicketCost = decLowerdeck
End Select
decFinalCost = decTicketCost * intGroupSize
Return decFinalCost
End Function
Private Function SingleGameTicketsFindCost(ByVal intTicketSelection As Integer, _
ByVal intGroupSize As Integer) As Decimal
Dim decTicketCost As Decimal
Dim decFinalCost As Decimal
Dim decBoxSeats As Decimal = 55D
Dim decLowerDeck As Decimal = 35D
Dim decUpperDeck As Decimal = 25D
Dim decStandingRoomOnly As Decimal = 15D
Select Case intTicketSelection
Case 0
decTicketCost = decBoxSeats
Case 1
decTicketCost = decLowerDeck
Case 2
decTicketCost = decUpperDeck
Case 3
decTicketCost = decStandingRoomOnly
End Select
decFinalCost = decTicketCost * intGroupSize
Return decFinalCost
End Function
End Class