Results 1 to 8 of 8

Thread: Need help with radio buttons within a select case.

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    2

    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

  2. #2
    Hyperactive Member
    Join Date
    May 2009
    Posts
    274

    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

  3. #3
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    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

  4. #4
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Need help with radio buttons within a select case.

    Select Case True ?

  5. #5
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: Need help with radio buttons within a select case.

    Quote Originally Posted by Inferrd View Post
    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.

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
            '
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    2

    Re: Need help with radio buttons within a select case.

    Quote Originally Posted by Inferrd View Post
    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.

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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