Problems with an input box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • leannsmarie
    New Member
    • Oct 2008
    • 5

    Problems with an input box

    Hi Everyone,
    I have a problem with a program I have been assigned thats driving me crazy and I hope someone with a fresh eye could point me in the right direction. Im using Visual Basic Express 2008 on WinXP and the program is a Windows Form application.
    The application has to calculate a total order for wire spools and display the shipping status. The form has a text box to accept the total number of spools being ordered (Integer), labels to display # In Stock, # on Back Order, Shipping & Handling, and Order Total, and buttons for calculate, clear, and exit. The application has four functions called from the Calculate Total button's click event procedure.

    GetInStock - which displays an input box asking the user to enter the number of spools in stock. It returns the amount In Stock
    ReadyToShip - Accepts the arguments : # of spools in stock and # of spools ordered. It returns the number of spools ready to ship.
    BackOrdered - Accepts the arguments: # of spools in stock and # ordered. If the amount ordered exceeds the amount in stock, the difference is returned, else backordered = 0
    ShippingCharges - Accepts the arguments: ReadyToShip() and per spool shipping charges. It returns the total shipping

    Here's the problem:

    When the program is running, there is an integer entered into the Spools Ordered textbox, the calculate total button is clicked, and the input box appears requesting the user enter the amount of spools currently in stock. When an integer is typed and entered, the input box comes back up requesting the information again. Nothing goes ToString, at least not the first time. When the process is repeated the data may or may not go ToString from that point and what is worse, when it does, only one label goes ToString at a time until the last two labels (which go ToString at the same time.)
    I may enter the same data into the input box up to ten times before all the labels are filled in (even then, there is inconsistency as to how many times this has to be done). The labels DO display the correct data in the end but I should only have to type into the input box once. Input Validation works correctly for both the input box and the checkbox.

    Basically my question is this; What could cause the input box to keep reappearing, and why wouldnt the labels all go ToString at the same time?
    Am I providing enough information to make a determination?

    I would certainly appreciate it if someone has an idea on this I havent thought of.
    Thanks for any and all help.
  • gwbob
    New Member
    • Oct 2008
    • 14

    #2
    it might help people if you gave us your code : )

    Comment

    • leannsmarie
      New Member
      • Oct 2008
      • 5

      #3
      Glad to oblige. Here goes.

      Code:
      Public Class Form1
      
          ' This application calculates the total order for spools of 
          ' copper wire for The Middletown Wire Company. The application
          ' also displays the shipping status of the order
      
          Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
              ' This procedure calculates the status and total of an order of spools.
      
              Dim decSpoolsTotal As Decimal       ' Holds the total cost of spools ordered
              Dim decTotal As Decimal             ' Holds the order total
              Dim decShipping As Decimal          ' Holds the shipping charges
              Dim intReady As Integer             ' Holds the spools ready to ship
              Dim intBackOrder As Integer         ' Holds the spools on back order
      
              decSpoolsTotal = ReadyToShip() * 100D
              decTotal = ShippingCharges() + decSpoolsTotal
      
              intReady = ReadyToShip()
              lblReady.Text = intReady.ToString("d")
              intBackOrder = BackOrdered()
              lblBackOrder.Text = intBackOrder.ToString("d")
              decShipping = ShippingCharges()
              lblShipping.Text = decShipping.ToString("c")
              lblTotal.Text = decTotal.ToString("c")
      
          End Sub
      
          Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
              ' This procedure resets the controls to default values
      
              ResetSpools()
              ResetDelivery()
              txtSpools.CausesValidation = False
              chkRush.Checked = False
      
          End Sub
      
          Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
      
              txtSpools.CausesValidation = False
      
              ' End the application
              Me.Close()
          End Sub
      
          Function ShippingCharges() As Decimal
              ' This function returns the cost of shipping.
              Dim decTotalShipping As Decimal     ' Holds the total shipping cost
      
              If chkRush.Checked = True Then
                  decTotalShipping = ReadyToShip() * 15D
              Else
                  decTotalShipping = ReadyToShip() * 10D
              End If
      
              Return decTotalShipping
          End Function
      
          Function GetInStock() As Integer
              ' This function returns the amount of spools in stock.
              Dim strInStock As String            ' Holds the user input of spools in stock
              Dim intInstock As Integer           ' Holds the amount of in stock spools
      
              strInStock = InputBox("Please enter the amount of spools in stock.", "Input Needed")
              If strInStock <> String.Empty Then
                  intInstock = CInt(strInStock)
              End If
      
              Return intInstock
          End Function
      
          Function ReadyToShip() As Integer
              ' This function returns the amount of spools ready to ship
              Dim intSpoolsOrdered As Integer
              Dim intReadyToShip As Integer
      
              intSpoolsOrdered = CInt(txtSpools.Text)
              If intSpoolsOrdered > GetInStock() Then
                  intReadyToShip = GetInStock()
              Else
                  intReadyToShip = intSpoolsOrdered
              End If
      
              Return intReadyToShip
          End Function
      
          Function BackOrdered() As Integer
              ' This function returns the amount of spools on backorder.
              Dim intSpoolsOrdered As Integer
              Dim intBackOrdered As Integer
      
              intSpoolsOrdered = CInt(txtSpools.Text)
              If intSpoolsOrdered > GetInStock() Then
                  intBackOrdered = intSpoolsOrdered - GetInStock()
              Else
                  intBackOrdered = 0
              End If
      
              Return intBackOrdered
          End Function
      
          Private Sub ResetSpools()
              ' This procedure resets the shipping selection
      
              txtSpools.Clear()
              chkRush.Checked = False
          End Sub
      
          Private Sub ResetDelivery()
              ' This procedure resets the price.
      
              lblBackOrder.Text = String.Empty
              lblReady.Text = String.Empty
              lblShipping.Text = String.Empty
              lblTotal.Text = String.Empty
          End Sub
      
          Private Sub txtSpools_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtSpools.Validating
              ' Validate the number entered by the user.
              If Not IsNumeric(txtSpools.Text) Then
                  MessageBox.Show("Spools ordered must be a number.", "Error")
      
                  ' Select the existing text in the text box.
                  txtSpools.SelectAll()
      
                  ' Set e.Cancel to true so the focus will stay in this control.
                  e.Cancel = True
              Else
                  Dim intSpoolsOrdered As Integer = CInt(txtSpools.Text)
      
                  If intSpoolsOrdered < 1 Then
                      MessageBox.Show("Amount of spools ordered cannot be less than One", "Error")
      
                      ' Select the existing text in the text box.
                      txtSpools.SelectAll()
      
                      ' Set e.Cancel to true so the focus will stay in this control.
                      e.Cancel = True
                  Else
                      e.Cancel = False
                  End If
              End If
          End Sub
      End Class

      Comment

      • leannsmarie
        New Member
        • Oct 2008
        • 5

        #4
        ********bump*** *****

        Comment

        • jg007
          Contributor
          • Mar 2008
          • 283

          #5
          you are calling the same function multiple times, is this intended as at least when I tried to reproduce the form this caused me to get multiple popups to check the amount in stock.

          everytime you call ' readytoship ' it in turn calls 'getinstock' which asks the same question again

          Code:
                  decSpoolsTotal = ReadyToShip() * 100D '  < 1st time
                  decTotal = ShippingCharges() + decSpoolsTotal
          
                  intReady = ReadyToShip() ' < 2nd time
          &

          Code:
            If chkRush.Checked = True Then  ' <3rd 
                      decTotalShipping = ReadyToShip() * 15D
                  Else
                      decTotalShipping = ReadyToShip() * 10D 
                  End If

          Comment

          • jg007
            Contributor
            • Mar 2008
            • 283

            #6
            what you could do to avoid too much rewrite is declare the variables for ' GetInStock ' for the form and then have some code to check ' If strInStock Is Nothing ' and only then bring up the inputbox .'

            Comment

            Working...