Connecting Tech Pros Worldwide Help | Site Map

Problems with an input box

leannsmarie's Avatar
Newbie
 
Join Date: Oct 2008
Posts: 5
#1: Oct 17 '08
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.
Newbie
 
Join Date: Oct 2008
Posts: 14
#2: Oct 17 '08

re: Problems with an input box


it might help people if you gave us your code : )
leannsmarie's Avatar
Newbie
 
Join Date: Oct 2008
Posts: 5
#3: Oct 17 '08

re: Problems with an input box


Glad to oblige. Here goes.

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     ' This application calculates the total order for spools of 
  4.     ' copper wire for The Middletown Wire Company. The application
  5.     ' also displays the shipping status of the order
  6.  
  7.     Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
  8.         ' This procedure calculates the status and total of an order of spools.
  9.  
  10.         Dim decSpoolsTotal As Decimal       ' Holds the total cost of spools ordered
  11.         Dim decTotal As Decimal             ' Holds the order total
  12.         Dim decShipping As Decimal          ' Holds the shipping charges
  13.         Dim intReady As Integer             ' Holds the spools ready to ship
  14.         Dim intBackOrder As Integer         ' Holds the spools on back order
  15.  
  16.         decSpoolsTotal = ReadyToShip() * 100D
  17.         decTotal = ShippingCharges() + decSpoolsTotal
  18.  
  19.         intReady = ReadyToShip()
  20.         lblReady.Text = intReady.ToString("d")
  21.         intBackOrder = BackOrdered()
  22.         lblBackOrder.Text = intBackOrder.ToString("d")
  23.         decShipping = ShippingCharges()
  24.         lblShipping.Text = decShipping.ToString("c")
  25.         lblTotal.Text = decTotal.ToString("c")
  26.  
  27.     End Sub
  28.  
  29.     Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
  30.         ' This procedure resets the controls to default values
  31.  
  32.         ResetSpools()
  33.         ResetDelivery()
  34.         txtSpools.CausesValidation = False
  35.         chkRush.Checked = False
  36.  
  37.     End Sub
  38.  
  39.     Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
  40.  
  41.         txtSpools.CausesValidation = False
  42.  
  43.         ' End the application
  44.         Me.Close()
  45.     End Sub
  46.  
  47.     Function ShippingCharges() As Decimal
  48.         ' This function returns the cost of shipping.
  49.         Dim decTotalShipping As Decimal     ' Holds the total shipping cost
  50.  
  51.         If chkRush.Checked = True Then
  52.             decTotalShipping = ReadyToShip() * 15D
  53.         Else
  54.             decTotalShipping = ReadyToShip() * 10D
  55.         End If
  56.  
  57.         Return decTotalShipping
  58.     End Function
  59.  
  60.     Function GetInStock() As Integer
  61.         ' This function returns the amount of spools in stock.
  62.         Dim strInStock As String            ' Holds the user input of spools in stock
  63.         Dim intInstock As Integer           ' Holds the amount of in stock spools
  64.  
  65.         strInStock = InputBox("Please enter the amount of spools in stock.", "Input Needed")
  66.         If strInStock <> String.Empty Then
  67.             intInstock = CInt(strInStock)
  68.         End If
  69.  
  70.         Return intInstock
  71.     End Function
  72.  
  73.     Function ReadyToShip() As Integer
  74.         ' This function returns the amount of spools ready to ship
  75.         Dim intSpoolsOrdered As Integer
  76.         Dim intReadyToShip As Integer
  77.  
  78.         intSpoolsOrdered = CInt(txtSpools.Text)
  79.         If intSpoolsOrdered > GetInStock() Then
  80.             intReadyToShip = GetInStock()
  81.         Else
  82.             intReadyToShip = intSpoolsOrdered
  83.         End If
  84.  
  85.         Return intReadyToShip
  86.     End Function
  87.  
  88.     Function BackOrdered() As Integer
  89.         ' This function returns the amount of spools on backorder.
  90.         Dim intSpoolsOrdered As Integer
  91.         Dim intBackOrdered As Integer
  92.  
  93.         intSpoolsOrdered = CInt(txtSpools.Text)
  94.         If intSpoolsOrdered > GetInStock() Then
  95.             intBackOrdered = intSpoolsOrdered - GetInStock()
  96.         Else
  97.             intBackOrdered = 0
  98.         End If
  99.  
  100.         Return intBackOrdered
  101.     End Function
  102.  
  103.     Private Sub ResetSpools()
  104.         ' This procedure resets the shipping selection
  105.  
  106.         txtSpools.Clear()
  107.         chkRush.Checked = False
  108.     End Sub
  109.  
  110.     Private Sub ResetDelivery()
  111.         ' This procedure resets the price.
  112.  
  113.         lblBackOrder.Text = String.Empty
  114.         lblReady.Text = String.Empty
  115.         lblShipping.Text = String.Empty
  116.         lblTotal.Text = String.Empty
  117.     End Sub
  118.  
  119.     Private Sub txtSpools_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtSpools.Validating
  120.         ' Validate the number entered by the user.
  121.         If Not IsNumeric(txtSpools.Text) Then
  122.             MessageBox.Show("Spools ordered must be a number.", "Error")
  123.  
  124.             ' Select the existing text in the text box.
  125.             txtSpools.SelectAll()
  126.  
  127.             ' Set e.Cancel to true so the focus will stay in this control.
  128.             e.Cancel = True
  129.         Else
  130.             Dim intSpoolsOrdered As Integer = CInt(txtSpools.Text)
  131.  
  132.             If intSpoolsOrdered < 1 Then
  133.                 MessageBox.Show("Amount of spools ordered cannot be less than One", "Error")
  134.  
  135.                 ' Select the existing text in the text box.
  136.                 txtSpools.SelectAll()
  137.  
  138.                 ' Set e.Cancel to true so the focus will stay in this control.
  139.                 e.Cancel = True
  140.             Else
  141.                 e.Cancel = False
  142.             End If
  143.         End If
  144.     End Sub
  145. End Class
  146.  
  147.  
leannsmarie's Avatar
Newbie
 
Join Date: Oct 2008
Posts: 5
#4: Oct 18 '08

re: Problems with an input box


********bump********
Needs Regular Fix
 
Join Date: Mar 2008
Posts: 283
#5: Oct 18 '08

re: Problems with an input box


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

Expand|Select|Wrap|Line Numbers
  1.  
  2.         decSpoolsTotal = ReadyToShip() * 100D '  < 1st time
  3.         decTotal = ShippingCharges() + decSpoolsTotal
  4.  
  5.         intReady = ReadyToShip() ' < 2nd time
  6.  
  7.  
&

Expand|Select|Wrap|Line Numbers
  1.   If chkRush.Checked = True Then  ' <3rd 
  2.             decTotalShipping = ReadyToShip() * 15D
  3.         Else
  4.             decTotalShipping = ReadyToShip() * 10D 
  5.         End If
  6.  
Needs Regular Fix
 
Join Date: Mar 2008
Posts: 283
#6: Oct 18 '08

re: Problems with an input box


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 .'
Reply