CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2009
    Posts
    1

    Calculation problems

    I was hoping someone could tell me why my code isnt working.

    Basically i'm trying to calculate totals and whenever I run the program the calculations comes out to zero. here is what i have for the calculations

    'Declare Constants
    Const YACHT_SIZE_22_Decimal As Decimal = 95D
    Const YACHT_SIZE_24_Decimal As Decimal = 137D
    Const YACHT_SIZE_30_Decimal As Decimal = 160D
    Const YACHT_SIZE_32_Decimal As Decimal = 192D
    Const YACHT_SIZE_36_Decimal As Decimal = 250D
    Const YACHT_SIZE_38_Decimal As Decimal = 400D
    Const YACHT_SIZE_45_Decimal As Decimal = 550D


    Private Sub OkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OkButton.Click
    'Calculate and display the current amounts and add to totals.
    Dim PriceDecimal, CharterPriceDecimal As Decimal
    Dim NumberOfHoursInteger As Integer

    'Find the Price.
    If YachtSizeList.SelectedIndex = 22 Then
    PriceDecimal = YACHT_SIZE_22_Decimal
    ElseIf YachtSizeList.SelectedIndex = 24 Then
    PriceDecimal = YACHT_SIZE_24_Decimal
    ElseIf YachtSizeList.SelectedIndex = 30 Then
    PriceDecimal = YACHT_SIZE_30_Decimal
    ElseIf YachtSizeList.SelectedIndex = 32 Then
    PriceDecimal = YACHT_SIZE_32_Decimal
    ElseIf YachtSizeList.SelectedIndex = 36 Then
    PriceDecimal = YACHT_SIZE_36_Decimal
    ElseIf YachtSizeList.SelectedIndex = 38 Then
    PriceDecimal = YACHT_SIZE_38_Decimal
    ElseIf YachtSizeList.SelectedIndex = 45 Then
    PriceDecimal = YACHT_SIZE_45_Decimal
    End If

    'Calculate the extended price and add to order total
    Try
    NumberOfHoursInteger = Integer.Parse(HoursTextBox.Text)
    CharterPriceDecimal = PriceDecimal * NumberOfHoursInteger
    PriceTextBox.Text = CharterPriceDecimal.ToString("C")
    'Allow Clear after an order has begun
    ClearForNextCharterToolStripMenuItem.Enabled = True
    ClearButton.Enabled = True

    Catch NumberOfHoursException As FormatException
    MessageBox.Show("Quantity must be numeric.", "Data entry error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    With HoursTextBox
    .Focus()
    .SelectAll()
    End With
    End Try
    End Sub

    I dont know if this is enough info to help me.. just let me know. Really would appreciate any insite on to why my totals come out to $0.00 when calculate the total

  2. #2
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: Calculation problems

    1. Please use CODE tags. It makes the post much more readable.

    2. put a break point here ...If YachtSizeList.SelectedIndex = 22 Then... and run your code. I think you will find that the "SelectedIndex" is something like 1 or 2. The text of the list may say 22, but that would probably be index 1. If you get the right index, I think you will start getting prices.

  3. #3
    Join Date
    Oct 2009
    Posts
    12

    Re: Calculation problems

    dear,

    will this help?

    form with command, listbox and 2 textboxes=>

    ========================================
    Option Explicit
    'Declare Constants
    Const YACHT_SIZE_22_Decimal As Single = 95
    Const YACHT_SIZE_24_Decimal As Single = 137
    Const YACHT_SIZE_30_Decimal As Single = 160
    Const YACHT_SIZE_32_Decimal As Single = 192
    Const YACHT_SIZE_36_Decimal As Single = 250
    Const YACHT_SIZE_38_Decimal As Single = 400
    Const YACHT_SIZE_45_Decimal As Single = 550

    Private Sub Form_Load()
    '§ populate the listbox
    YachtSizeList.AddItem "22"
    YachtSizeList.AddItem "24"
    YachtSizeList.AddItem "30"
    YachtSizeList.AddItem "32"
    YachtSizeList.AddItem "36"
    YachtSizeList.AddItem "38"
    YachtSizeList.AddItem "45"
    End Sub

    Private Sub OkButton_Click()
    '§ Calculate and display the current amounts and add to totals.
    Dim PriceDecimal, CharterPriceDecimal As Single
    'Dim NumberOfHoursInteger As Integer
    '§ Find the Price.
    Select Case YachtSizeList.Text
    Case "22"
    PriceDecimal = YACHT_SIZE_22_Decimal
    Case "24"
    PriceDecimal = YACHT_SIZE_24_Decimal
    Case "30"
    PriceDecimal = YACHT_SIZE_30_Decimal
    Case "32"
    PriceDecimal = YACHT_SIZE_32_Decimal
    Case "36"
    PriceDecimal = YACHT_SIZE_36_Decimal
    Case "38"
    PriceDecimal = YACHT_SIZE_38_Decimal
    Case "45"
    PriceDecimal = YACHT_SIZE_45_Decimal
    End Select
    ' NumberOfHoursInteger = Val(HoursTextBox.Text)
    CharterPriceDecimal = PriceDecimal * Val(HoursTextBox.Text)
    PriceTextBox.Text = Str(PriceDecimal * Val(HoursTextBox.Text)) & " $"
    '§ Allow Clear after an order has begun
    'ClearForNextCharterToolStripMenuItem.Enabled = True
    'ClearButton.Enabled = True
    '
    'Catch NumberOfHoursException As FormatException
    'MessageBox.Show("Quantity must be numeric.", "Data entry error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    'With HoursTextBox
    '.Focus()
    '.SelectAll()
    'End With
    End Sub
    ==============================================

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured