Question General Problem calculate total

TommyEvans

Active member
Joined
Feb 19, 2010
Messages
39
Programming Experience
Beginner
It's late at night for me, and that is probably why I'm having this problem right now. But anyways. Here is the design view of the program I'm trying to make.

2yo7f4o.gif


Basically, I want the user to be able to select a rifle, and then choose the attachments that want to come with it. I don't even know where to start. Prices for the rifles are:
Marlin 270: 296.99
Savage 270: 551.49
Savage 243: 399.00

Attachment Prices:
Scope: 399.99
Sling: 13.99
BiPod: 129.49
Synthetic Stock: 187.61

Then on button click, it takes the total, with gun and selected attachments. Then displays where Label1 is.

I have no idea how to begin.
 
Well, normally this would probably be database or file driven but that doesn't seem to be what you are doing. Easiest thing you could do is store the value of each item in the .Tag property then on your calculate event for each selected item you could cast the tag property and calculate the total. You can fill the Tag property in either the designer or in the Form_Load event assign the Tag property of each control that needs a value.

VB.NET:
radMarlin270.Tag = 296.99
radSavage270.Tag = 551.49

etc...
 
make IF forms .. Why don't you make msgbox in the place of label ?
ex.
VB.NET:
If (rdoMarlin270.Checked) Then
       Sum = 296.99
     ElseIf (rdoSavage270.Checked) Then
       Sum = 551.49
     Else (rdoSavage243.Checked) Then
       Sum = 399.00
End If
 
I'm a big fan of Bushnells and Tascos. I personally have a Harris Bi-Pod on my Marlin XL7 .270.

Anyways.

Would this work, variables defining in if else statements?

VB.NET:
If (rad.Marlin270.Checked) Then
intMarlin270 = 296.99
Else 
If ...
Ect.
And are integers proper for using a decimal like number, such as prices?
 
Last edited:
No integers (Int16,Int32,Int64,Integer) are not good for decimals. Use either Double or Decimal.
VB.NET:
'Global variable 
Dim RifleValue as Decimal
Public Sub RadioButtonChangedEvent(ByVal sender as object, Byval e as System.EventArgs)

     'check for radio button objec
     If sender Is Not Nothing
          'get the radio button name and use it in select 
          'The assumption here is that the name of the radiobutton controls follow a pattern
          ' such as radMarlin270, radSavage270, etc..
          Select Case CType(sender,RadioButton).Name.SubString(3).ToUpper() 
               Case "MARLIN270"
                         RifleValue = 296.99
               Case "SAVAGE270"
                         RifleValue = 551.49
               Case "SAVAGE243"
                         RifleValue = 399
          End Select
          
     End If
End Sub

Public Sub CalculateButton(byval sender as object, byval e as System.EventArgs)
     Dim Total as Double = 0

     'add rifle value to total
     Total += RifleValue

     'add the other values
     '....
     '....

     'output total to label -- formats the value of Total as currency
     lblResults.Text = String.Format("Total for order {0:C}",Total)

End Sub
 
That completely made me lost, but I could play with it, and figure it out probably.
I have this so far.

VB.NET:
Public Class Firearms
    Dim RifleValue As Decimal



    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Application.Exit()

    End Sub

    Private Sub MarlinToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MarlinToolStripMenuItem.Click
        Process.Start("http://marlinfirearms.com/")

    End Sub

    Private Sub SavageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SavageToolStripMenuItem.Click
        Process.Start("http://www.savagearms.com/")

    End Sub

    Public Sub RadioButtonChangedEvent(ByVal sender As Object, ByVal e As System.EventArgs)

        'check for radio button objec

        'get the radio button name and use it in select 
        'The assumption here is that the name of the radiobutton controls follow a pattern
        ' such as radMarlin270, radSavage270, etc..
        Select Case CType(sender, RadioButton).Name.Substring(3).ToUpper()
            Case "MARLIN270"
                RifleValue = 296.99
            Case "SAVAGE270"
                RifleValue = 551.49
            Case "SAVAGE243"
                RifleValue = 399
        End Select


    End Sub


    Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
        Dim Total As Double = 0

        'add rifle value to total
        Total += RifleValue

        'add the other values
        '....
        '....

        'output total to label -- formats the value of Total as currency
        lblTotal.Text = String.Format("Total for order {0:C}", Total)


    End Sub
End Class

As is, without me finishing it. It should atleast show the rifle total in the total label. It does not do this. I've only been coding in VB.NET for about 2 weeks, so this is new stuff to me.
 
Now assign each of the radio buttons CheckedChange event to RadioButtonChangedEvent, basically hooking that event to each radio button. There are a couple ways to do this either in the designer you can or in code you can add 'Handles' to the end of the method signature.
VB.NET:
Public Sub RadioButtonChangedEvent(byval sender as Object, byval e as System.EventArgs) [B]Handles radMarlin270.CheckedChange, radSavage270.CheckedChange, radSavage243.CheckedChange[/B]

Then when you click to change the radio button the code will change the value of the RifleValue.

The Select Case statement is taking a value that is known, in this case the sender object which is the radio button that was clicked, and getting the Name of that radiobutton. Then since we assume that the name of the radiobutton is "rad" + the name (radMarlin270), the Substring method grabs the part of the name that we want to use as a compare value for the Select statement. I use the .ToUpper() to force this to uppercase so that way it becomes case insensitive, I just need to worry about spelling :) then.
 
Back
Top