2011-02-23 76 views
0

我正在研究计算门票销售的视觉基础应用程序(带视觉工作室2010)。这是一个非常具有挑战性,因为我是一个新手,当涉及到视觉基础。棒球票销售(Visual Basic)

我很难得到计算按钮到 以下是从函数中获取成本并使用它们来计算总计的说明。以下是他们给我的指示。

  1. 用户选择是否购买季票或单比赛门票
  2. 用户输入所需的票的数目和基于它们是否选择季节单比赛门票座位的类型。
  3. 用户点击计算机票费用按钮,显示最终成本
  4. 用户点击清除表单按钮清除响应

我卡上的计算按钮。我只需要能够从按钮捕获成本,然后使用按钮来计算它。

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

它现在做了什么?任何错误? – mellamokb 2011-02-23 04:31:28

+0

我不能准确地看到发生了什么问题?你是否收到错误?你想在哪里提供价值? – 2011-02-23 04:34:36

+0

不应该'finalCost = ticketNum * cost'在'SingleGameCost'的底部,在'Return'语句的正上方? – mellamokb 2011-02-23 04:34:46

回答

0

您需要将seatType作为公共变量。 然后,一旦他们选择座位,您设置变量 ,然后在计算时使用该变量。

然后返回成本。

在计算功能

你已经拿到了最终的成本则

me.lblCostDisplay.Text = finalCost.ToString() 
0

我已经修改了这一点给你之后。采取你想要的。 我没有测试过它。希望这有助于...不知道为什么格式化 没有出来完全正确..

公共类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 

末级

0

有与上面的代码许多问题。它需要重构重构。但即使没有这些,目前的形式也无法完全解答这个问题,原因如下 - 您不需要参数即可呼叫SingleGameCostSeasonalCost,但它们是用这样的参数声明的。

此代码不会编译。如果它确实编译在你身边,那么你确实包含了一些代码来支持这个问题。如果它不是编译,这是答案。

关于重构,条款如下:

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价值会丢失,因为它被声明为一个局部变量。

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 
+0

希望这有助于提高:) – Austin 2014-12-15 15:03:51