Results 1 to 9 of 9

Thread: [RESOLVED] Radio Buttons and Select Case Statements

  1. #1

    Thread Starter
    Member
    Join Date
    May 2010
    Posts
    34

    Resolved [RESOLVED] Radio Buttons and Select Case Statements

    Hello Everyone,

    I need help to figure out how to associate a select case statement with, which point to four Function procedures, to four radio buttons that are used to +, -, *, and / two simple fractions in a fractions calculator. This is a class assignment for which I have already been graded. I used an If...ElseIf statment and it worked perfectly. Now I am trying to utilize the Case Statments with Function Procedures to do the same thing. I do not get graded for this, but I was told to research it an find an answer. My book has not helped me. Please help. Anything you can do to assist me would be appreciated. My current code is listed below.

    Option Strict On

    Public Class frmFractionsCalculator

    ' Class variables declared

    Private decFractionOne As Decimal
    Private decFractionTwo As Decimal
    Private decResult As Decimal

    Private Sub btnCalculateResult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateResult.Click
    ' This event handler allows the user to enter two simple fractions
    ' and calculate them returning a result in decimal format to the hundredths
    ' place. This handler will also prevent the user from dividing by zero.

    ' Deaclare and initalize variables
    Dim decFractionOneNumerator As Decimal
    Dim decFractionOneDenominator As Decimal
    Dim decFrationTwoNumerator As Decimal
    Dim decFractionTwoDenominator As Decimal


    ' Try-Catch SystemException validation for thw numerators and demoninators
    Try
    decFractionOneNumerator = Convert.ToDecimal(Me.txtFractionOneNumerator.Text)
    decFractionOneDenominator = Convert.ToDecimal(Me.txtFractionOneDenominator.Text)
    decFrationTwoNumerator = Convert.ToDecimal(Me.txtFractionTwoNumerator.Text)
    decFractionTwoDenominator = Convert.ToDecimal(Me.txtFractionTwoDenominator.Text)
    Catch Exception As SystemException
    MsgBox("Error. Please try again", , "Error")
    Me.txtFractionOneNumerator.Focus()
    Me.txtFractionOneNumerator.Clear()
    Me.txtFractionOneDenominator.Clear()
    Me.txtFractionTwoNumerator.Clear()
    Me.txtFractionTwoDenominator.Clear()
    End Try

    ' Try-Catch DivideByZeroException validation
    Try
    decFractionOne = decFractionOneNumerator / decFractionOneDenominator
    decFractionTwo = decFrationTwoNumerator / decFractionTwoDenominator
    Catch Exception As DivideByZeroException
    MsgBox("Attempted to divide by zero", , "Error")
    Me.txtFractionOneNumerator.Focus()
    Me.txtFractionOneNumerator.Clear()
    Me.txtFractionOneDenominator.Clear()
    Me.txtFractionTwoNumerator.Clear()
    Me.txtFractionTwoDenominator.Clear()
    End Try

    Select Case decResult
    Case 0
    Addition()
    Case 1
    Subtraction()
    Case 2
    Multiplication()
    Case 3
    Division()
    End Select

    ' Display calculation results
    Me.lblResults.Visible = True
    Me.lblResults.Text = decResult.ToString("F2")

    End Sub

    ' Function Procedure Addition
    Private Function Addition() As Decimal
    decResult = decFractionOne + decFractionTwo
    Return decResult
    End Function

    ' Function Procedure Subtraction
    Private Function Subtraction() As Decimal
    decResult = decFractionOne - decFractionTwo
    Return decResult
    End Function

    ' Function Procedur Multiplication
    Private Function Multiplication() As Decimal
    decResult = decFractionOne * decFractionTwo
    Return decResult
    End Function

    ' Function Procedur Division
    Private Function Division() As Decimal
    decResult = decFractionOne / decFractionTwo
    Return decResult
    End Function

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
    ' This event handler clears the calculator form and sets the focus
    ' on the first fractions numerator and the +(Plus) radio button.

    Me.txtFractionOneNumerator.Text = " "
    Me.txtFractionOneDenominator.Text = " "
    Me.txtFractionTwoNumerator.Text = " "
    Me.txtFractionTwoDenominator.Text = " "
    Me.lblResults.Text = " "
    Me.radPlus.Checked = True
    Me.radMinus.Checked = False
    Me.radMultiply.Checked = False
    Me.radDivide.Checked = False
    Me.txtFractionOneNumerator.Focus()
    End Sub

    Private Sub frmFractionsCalculator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Threading.Thread.Sleep(5000)
    ' This event handler pauses the splash screen for five seconds and loads
    ' the form clearing the calculator form and setting the focus
    ' on the first fractions numerator and the +(Plus) radio button.

    Me.txtFractionOneNumerator.Focus()
    Me.txtFractionOneNumerator.Text = " "
    Me.txtFractionOneDenominator.Text = " "
    Me.txtFractionTwoNumerator.Text = " "
    Me.txtFractionTwoDenominator.Text = " "
    Me.lblResults.Text = " "
    Me.radPlus.Checked = True
    Me.radMinus.Checked = False
    Me.radMultiply.Checked = False
    Me.radDivide.Checked = False

    End Sub
    End Class

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Radio Buttons and Select Case Statements

    That's a lotta code and not in a code tag which makes it even harder to read.

    To the point, here's the easiest way to figure out which radio button is selected:
    Code:
    Select Case True
        Case AddRadioButton.Checked
            'Addition was selected, call the function
        Case SubtractRadioButton.Checked
            'Subtraction was selected, call the function
        Case MultiplyRadioButton.Checked
            'Multiplication was selected, call the function
        Case DivideRadioButton.Checked
            'Division was selected, call the function
    End Select
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  3. #3

    Thread Starter
    Member
    Join Date
    May 2010
    Posts
    34

    Re: Radio Buttons and Select Case Statements

    Quote Originally Posted by JuggaloBrotha View Post
    That's a lotta code and not in a code tag which makes it even harder to read.

    To the point, here's the easiest way to figure out which radio button is selected:
    Code:
    Select Case True
        Case AddRadioButton.Checked
            'Addition was selected, call the function
        Case SubtractRadioButton.Checked
            'Subtraction was selected, call the function
        Case MultiplyRadioButton.Checked
            'Multiplication was selected, call the function
        Case DivideRadioButton.Checked
            'Division was selected, call the function
    End Select
    What is code tag? I may know, but may not be familiar with the terminology. When it is viewed in VB it is coded with color and is indented and so forth. When I copied the code it left the formatting. I will try to do better in the future. Thank you for your help!!

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Radio Buttons and Select Case Statements

    You supply tags here when you post code.

    You can use code ,vb, vb.net, sql .... and others inside square brackets with highlight = at the start of the section
    and /highlight at the end
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Radio Buttons and Select Case Statements

    Quote Originally Posted by Joseph Storey View Post
    What is code tag? I may know, but may not be familiar with the terminology. When it is viewed in VB it is coded with color and is indented and so forth. When I copied the code it left the formatting. I will try to do better in the future. Thank you for your help!!
    Here's the code tag: [code]Your code goes here[/code] It's what I did in my post around the Select case example
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

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

    Re: Radio Buttons and Select Case Statements

    Did you get a good grade?
    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
    Member
    Join Date
    May 2010
    Posts
    34

    Re: Radio Buttons and Select Case Statements

    Quote Originally Posted by dbasnett View Post
    Did you get a good grade?
    I made an 85 because I used and IF...ElseIf statemnet to do the calculations. He want me to go back and research how to do it with Select Case Statements and Function procedures. My instructor just wanted to make sure I understood what was coded. It's my fault, I did not read the requirements document as closely as I should have and rushed into the program. The advice above helped me more than you could know.

    Thanks again to all the advive and help!!

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

    Re: [RESOLVED] Radio Buttons and Select Case Statements

    You can tell your instructor that I said he needs to pay attention to real world, good coding practices. Example:

    Code:
      decFractionOneNumerator = Convert.ToDecimal(Me.txtFractionOneNumerator.Text)
      decFractionOneDenominator = Convert.ToDecimal(Me.txtFractionOneDenominator.Text)
      decFrationTwoNumerator = Convert.ToDecimal(Me.txtFractionTwoNumerator.Text)
      decFractionTwoDenominator = Convert.ToDecimal(Me.txtFractionTwoDenominator.Text)
    What if the user entered something other than numbers into the text fields? Ask him that?
    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

  9. #9
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [RESOLVED] Radio Buttons and Select Case Statements

    Quote Originally Posted by dbasnett View Post
    You can tell your instructor that I said he needs to pay attention to real world, good coding practices. Example:

    Code:
      decFractionOneNumerator = Convert.ToDecimal(Me.txtFractionOneNumerator.Text)
      decFractionOneDenominator = Convert.ToDecimal(Me.txtFractionOneDenominator.Text)
      decFrationTwoNumerator = Convert.ToDecimal(Me.txtFractionTwoNumerator.Text)
      decFractionTwoDenominator = Convert.ToDecimal(Me.txtFractionTwoDenominator.Text)
    What if the user entered something other than numbers into the text fields? Ask him that?
    If you want good coding practices then you'd be using Decimal.TryParse() (or Decimal.Parse() at the very minimum) instead of Ctype(, Decimal), CDec() or Convert.ToDecimal()
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

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