|
-
May 26th, 2010, 07:58 AM
#1
Thread Starter
Member
[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
-
May 26th, 2010, 08:08 AM
#2
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
-
May 26th, 2010, 08:27 AM
#3
Thread Starter
Member
Re: Radio Buttons and Select Case Statements
 Originally Posted by JuggaloBrotha
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!!
-
May 26th, 2010, 08:30 AM
#4
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
-
May 26th, 2010, 08:32 AM
#5
Re: Radio Buttons and Select Case Statements
 Originally Posted by Joseph Storey
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
-
May 26th, 2010, 10:15 AM
#6
Re: Radio Buttons and Select Case Statements
Did you get a good grade?
-
May 26th, 2010, 01:15 PM
#7
Thread Starter
Member
Re: Radio Buttons and Select Case Statements
 Originally Posted by dbasnett
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!!
-
May 27th, 2010, 06:58 AM
#8
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?
-
May 27th, 2010, 08:14 AM
#9
Re: [RESOLVED] Radio Buttons and Select Case Statements
 Originally Posted by dbasnett
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()
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
|