|
-
Feb 23rd, 2013, 03:16 PM
#1
Thread Starter
New Member
Need help with radio buttons within a select case.
I have to write a program that uses radio buttons to apply a discount to the subtotal. This has to be done with select case. Also, all the discount amounts have to be constants. I have tried many different ways, but after 2 days i give up.
Public Class Form1
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
txtCoffee.Text = "0"
txtEspresso.Text = "0"
rad0.Checked = True
rad10.Checked = False
rad25.Checked = False
rad50.Checked = False
lblSubtotal.Text = ""
lblTax.Text = ""
lblTotal.Text = ""
End Sub
Private Sub btnCheckout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckout.Click
Const CONSTCOFFEE As Decimal = CDec(1.95)
Const CONSTESPRESSO As Decimal = CDec(3.95)
Const CONSTTAX As Decimal = CDec(0.07)
Const CONSTDISC0 As Decimal = CDec(0)
Const CONSTDISC10 As Decimal = CDec(0.1)
Const CONSTDISC25 As Decimal = CDec(0.25)
Const CONSTDISC50 As Decimal = CDec(0.5)
Dim intCoffee As Integer = 0
Dim intEspresso As Integer = 0
intCoffee = CInt(txtCoffee.Text)
intEspresso = CInt(txtEspresso.Text)
Dim decSubtotal As Decimal = 0
Dim decTax As Decimal = 0
Dim decTotal As Decimal = 0
Dim decDiscount As Decimal = 0
Dim decPreSubDisc As Decimal = 0
Dim decCalculation As Decimal = 0
Dim decCoffee As Decimal
Dim decEspresso As Decimal
If Decimal.TryParse(txtCoffee.Text, decCoffee) Then
If Decimal.TryParse(txtEspresso.Text, decEspresso) Then
MessageBox.Show("You have ordered " & txtCoffee.Text & " coffee(s)" & ControlChars.CrLf & txtEspresso.Text & " espresso(s).")
Select Case CDec(decDiscount)
Case 1
If rad0.Checked = True Then
decDiscount = CONSTDISC0
End If
Case 2
If rad10.Checked = True Then
decDiscount = CONSTDISC10
End If
Case 3
If rad10.Checked = True Then
decDiscount = CONSTDISC25
End If
Case 4
If rad10.Checked = True Then
decDiscount = CONSTDISC50
End If
End Select
decPreSubDisc = CDec((CONSTCOFFEE * intCoffee) + (CONSTESPRESSO * intEspresso))
decSubtotal = CDec(decPreSubDisc - (((CONSTCOFFEE * intCoffee) + (CONSTESPRESSO * intEspresso)) * decDiscount))
lblSubtotal.Text = decSubtotal.ToString("c")
decTax = CDec(decSubtotal * CONSTTAX)
lblTax.Text = decTax.ToString("c")
decTotal = CDec(decSubtotal + decTax)
lblTotal.Text = decTotal.ToString("c")
End If
Else : MessageBox.Show("Please enter numbers only")
txtCoffee.Text = "0"
txtEspresso.Text = "0"
End If
End Sub
End Class
-
Feb 24th, 2013, 06:09 AM
#2
Hyperactive Member
Re: Need help with radio buttons within a select case.
I would replace your current Select Case with
Code:
Select Case Rad0.Checked
Case True
decDiscount = CONSTDISC0
Case Else
Select Case Rad10.Checked
Case True
decDiscount = CONSTDISC10
Case Else
Select Case Rad25.Checked
Case True
decDiscount = CONSTDISC25
Case Else
Select Case Rad50.Checked
Case True
decDiscount = CONSTDISC50
End Select
End Select
End Select
End Select
-
Feb 24th, 2013, 07:03 AM
#3
Re: Need help with radio buttons within a select case.
The example from norman_bates above solves the problem but it leaves me with a bigger problem. Select Case is not designed to check values of different variables or objects. As you have already realised, it checks a SINGLE variable for different values and deals with each case accordingly, therefore it is not an appropriate solution to the problem you have identified. If .... Then ... ElseIf is much better in this situation.
The situation you described is contrived and, I'm sorry, to me pretty ridiculous.
I would go back and check again exactly what the assignment requires.
oh no! I'm starting to sound like DataMiser now
-
Feb 24th, 2013, 07:57 AM
#4
Re: Need help with radio buttons within a select case.
Select Case True ?
-
Feb 24th, 2013, 08:07 AM
#5
Re: Need help with radio buttons within a select case.
 Originally Posted by Inferrd
Select Case True ? 
OH yes! I never even thought of that!. Feel pretty daft now for the rant But I still think it's convoluted.
-
Feb 24th, 2013, 09:28 AM
#6
Re: Need help with radio buttons within a select case.
@OP
'
Code:
'instead of
Const CONSTCOFFEE As Decimal = CDec(1.95)
'do this and save some typing
Const CONSTCOFFEE As Decimal = 1.95D
'
-
Feb 24th, 2013, 09:33 AM
#7
Thread Starter
New Member
Re: Need help with radio buttons within a select case.
 Originally Posted by Inferrd
Select Case True ? 
That it, I knew I was overlooking something simple. This worked perfect.
Select Case True
Case rad0.Checked
decDiscount = CONSTDISC_0
Case rad10.Checked
decDiscount = CONSTDISC_10
Case rad25.Checked
decDiscount = CONSTDISC_25
Case rad50.Checked
decDiscount = CONSTDISC_50
End Select
Thanks so much.
-
Feb 24th, 2013, 10:12 AM
#8
Re: Need help with radio buttons within a select case.
Another approach would be to put the discount associated with each RadioButton(RB) into the .Tag property of the RB. Then set up one .CheckedChanged event handler to set the discount based on the Tag. Here is what it might look like. Note that I only did two buttons.
Code:
Dim decDiscount As Decimal
Private Sub RadioButton_CheckedChanged(sender As Object, _
e As EventArgs) _
Handles rad10.CheckedChanged, rad25.CheckedChanged
Dim rb As RadioButton = DirectCast(sender, RadioButton) 'get the radio button affected
If rb.Checked Then 'is it checked
'yes, get the discount from the tag
decDiscount = Decimal.Parse(DirectCast(rb.Tag, String))
End If
End Sub
Add the other radio.CheckedChanged and you will be done.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|