6 Replies - 9200 Views - Last Post: 09 August 2012 - 09:12 PM Rate Topic: -----

#1 havit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-August 12

Visual Basic 2005 btnCalculate_Click issue

Posted 04 August 2012 - 09:27 PM

Hi Everyone,
I have a problem with a program I have been assigned that's driving me crazy(short trip)and I hope someone could point me in the right direction. I'm using Visual Basic 2005 on Win7 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.
It is not calculating the shipping charges and giving me a total, or calculating the rush charge if checked, or validating my input by not accepting orders for less than 1 spool, also if there are no backordered spools it should return zero. I have fooled with this until I'm sure I have over thought it and over complicated it :)
When I run the code it will take the number of spools ordered and then ask me for how many are in stock; it will calculate if the amount ordered is greater and the amount in stock is less and then put the difference in the back ordered box. This is my first class in VB and I just don't have the hang of it yet. Any help or suggestions will be appreciated.

Public Class Form1

    Private Function GetInStock() As Integer

        Dim intSpoolinStock As Integer = InputBox("Enter the number of spools in stock")

        Return intSpoolinStock

    End Function
    Private Function ReadyToShip() As Integer
        Dim intSpoolReadyShip As Integer

        Return intSpoolReadyShip
    End Function

    Private Function BackOrdered() As Integer

    End Function



    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 decTotalDue 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 
        Dim intSpoolsOrdered, intSpoolInStock, intSpoolReadyShip As Integer
        Dim intSpoolsOnBackOrder As Integer
        



        intReady = ReadyToShip()
        txtSpoolsReadyToShip.Text = intReady.ToString("d")
        intBackOrder = BackOrdered()
        txtSpoolsOnBackOrder.Text = intBackOrder.ToString("d")
        decShipping = ShippingCharges()
        txtShippingHandling.Text = decShipping.ToString("c")
        txtTotalDue.Text = decTotalDue.ToString("c")
        decSpoolsTotal = ReadyToShip() * 100D
        decTotalDue = ShippingCharges() + decSpoolsTotal

        intSpoolInStock = GetInStock()

        intSpoolReadyShip = (intSpoolInStock)+(intSpoolsOrdered)
        txtSpoolsReadyToShip.Text = intSpoolReadyShip.ToString

        intSpoolsOnBackOrder = (intSpoolsOrdered) - (intSpoolInStock)
        txtSpoolsOnBackOrder.Text = intSpoolsOnBackOrder.ToString

        

    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 
        ChkRushDelivery.Checked = False
        txtNumberSpoolsOrdered.Clear()
        txtSpoolsReadyToShip.Clear()
        txtSpoolsOnBackOrder.Clear()
        txtShippingHandling.Clear()
        txtTotalDue.Clear()
    End Sub


    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click


        ' 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 
       
        txtNumberSpoolsOrdered.CausesValidation = False

        If ChkRushDelivery.Checked = True Then
            decTotalShipping = ReadyToShip() * 15D
        Else
            decTotalShipping = ReadyToShip() * 10D
        End If

        Return decTotalShipping
    End Function




End Class


Is This A Good Question/Topic? 0
  • +

Replies To: Visual Basic 2005 btnCalculate_Click issue

#2 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 328
  • View blog
  • Posts: 1,990
  • Joined: 26-March 09

Re: Visual Basic 2005 btnCalculate_Click issue

Posted 05 August 2012 - 12:29 AM

Hi,

Firstly this is VB.Net code, so you're actually in the wrong forum, but I'll try to give you some pointers.

1. You shipping charges function calls the ReadyToShip function as part of it's calculation, but the ReadyToShip function has no code in it?


' Your calculation code
If ChkRushDelivery.Checked = True Then
  decTotalShipping = ReadyToShip() * 15D
Else
  decTotalShipping = ReadyToShip() * 10D
End If

'The ReadyToShip function
Private Function ReadyToShip() As Integer
Dim intSpoolReadyShip As Integer
  
  ' YOU NEED SOME CODE HERE
  
  Return intSpoolReadyShip
End Function





2. Your BackOrdered function also has no code in it. Both the BackOrdered and ReadyToShip functions are used as part of you calculations. With no code in them, you will never get a correct result.

This post has been edited by maj3091: 05 August 2012 - 12:32 AM

Was This Post Helpful? 0
  • +
  • -

#3 havit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-August 12

Re: Visual Basic 2005 btnCalculate_Click issue

Posted 06 August 2012 - 08:33 PM

Thanks for your help, I was very tired and definitely left a lot of things out and had a few in the wrong place! I am learning the hard way! Here is the reworked code.

Public Class Form1

    Function GetInStock() As Integer

        Dim intSpoolinStock As Integer = InputBox("Enter the number of spools in stock")

        Return intSpoolinStock

    End Function
    Function ReadyToShip(ByVal intSpoolinStock As Integer, ByVal intSpoolsOrdered AS Integer) As Integer
        If intSpoolinStock > intSpoolsOrdered Then
            Return intSpoolsOrdered
        Else
            Return intSpoolinStock

        End If

    End Function

    Function BackOrdered(intSpoolinStock AS Integer, intSpoolsOrdered As Integer) As Integer
        Dim intSpoolsOnBackOrder As Integer
        If intSpoolinStock >= intSpoolsOrdered Then
            intSpoolsOnBackOrder = 0
        Else
            intSpoolsOnBackOrder = (intSpoolsOrdered - intSpoolinStock)
        End If
        Return intSpoolsOnBackOrder

    End Function


    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 decTotalDue 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 
        Dim intSpoolsOrdered, intSpoolInStock As Integer

        intSpoolsOrdered = txtNumberSpoolsOrdered.Text
        intSpoolInStock = GetInStock()
        intReady = ReadyToShip(intSpoolInStock,intSpoolsOrdered )
        txtSpoolsReadyToShip.Text = intReady.ToString("d")
        intBackOrder = BackOrdered(intSpoolInStock, intSpoolsOrdered)
        txtSpoolsOnBackOrder.Text = intBackOrder.ToString("d")
        decShipping = ShippingCharges(intReady)
        txtShippingHandling.Text = decShipping.ToString("c")
        decTotalDue = decShipping + intReady * 100
        txtTotalDue.Text = decTotalDue.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 
        ChkRushDelivery.Checked = False
        txtNumberSpoolsOrdered.Clear()
        txtSpoolsReadyToShip.Clear()
        txtSpoolsOnBackOrder.Clear()
        txtShippingHandling.Clear()
        txtTotalDue.Clear()
    End Sub


    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

        ' End the application 
        Me.Close()
    End Sub

    Function ShippingCharges(ByVal intSpoolReadyShip As Integer)
        ' This function returns the cost of shipping. 
        Dim decTotalShipping As Decimal ' Holds the total shipping cost
        If ChkRushDelivery.Checked = True Then
            decTotalShipping = intSpoolReadyShip * 15D
        Else
            decTotalShipping = intSpoolReadyShip * 10D
        End If

        Return decTotalShipping
    End Function

End Class


Was This Post Helpful? 0
  • +
  • -

#4 torind_2000  Icon User is offline

  • D.I.C Regular

Reputation: 57
  • View blog
  • Posts: 293
  • Joined: 22-August 11

Re: Visual Basic 2005 btnCalculate_Click issue

Posted 09 August 2012 - 10:11 AM

View Posthavit, on 06 August 2012 - 08:33 PM, said:

intSpoolsOrdered = txtNumberSpoolsOrdered.Text ' you need to convert this to a number.


so maybe something like.
integer.tryparse(txtNumberSpoolsOrdered.Text, intSpoolsordered)

or Maybe
intSpoolsOrdered = Cint(txtNumberSpoolsOrdered.text)

View Posthavit, on 06 August 2012 - 08:33 PM, said:

decTotalDue = decShipping + intReady * 100

Why do you multiply by 100 here?
Was This Post Helpful? 0
  • +
  • -

#5 havit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-August 12

Re: Visual Basic 2005 btnCalculate_Click issue

Posted 09 August 2012 - 07:48 PM

View Posttorind_2000, on 09 August 2012 - 11:11 AM, said:

View Posthavit, on 06 August 2012 - 08:33 PM, said:

intSpoolsOrdered = txtNumberSpoolsOrdered.Text ' you need to convert this to a number.


so maybe something like.
integer.tryparse(txtNumberSpoolsOrdered.Text, intSpoolsordered)

or Maybe
intSpoolsOrdered = Cint(txtNumberSpoolsOrdered.text)

View Posthavit, on 06 August 2012 - 08:33 PM, said:

decTotalDue = decShipping + intReady * 100

Why do you multiply by 100 here?



why do I need to convert to a number there? I can't answer your question (I am newbie), I just know it is working as is. The $100 is the cost of each spool. Thanks for your input and I used both of your suggestions and they work also. Can you explain to me why i need to convert to a number there?

Thanks,
hav
Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1822
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Visual Basic 2005 btnCalculate_Click issue

Posted 09 August 2012 - 08:07 PM

Text property returns string and what you need there is a numeric value to do arithmetics. So to make sure user do not put spaces or so so we first parse that string to numeric value, and if successful then we use it as number and not string...
Was This Post Helpful? 0
  • +
  • -

#7 havit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-August 12

Re: Visual Basic 2005 btnCalculate_Click issue

Posted 09 August 2012 - 09:12 PM

View Postsmohd, on 09 August 2012 - 09:07 PM, said:

Text property returns string and what you need there is a numeric value to do arithmetics. So to make sure user do not put spaces or so so we first parse that string to numeric value, and if successful then we use it as number and not string...

"smacking my head" I knew that, somewhere in the back of my brain LOL. Thanks for the explanation.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1