| | |
Radio buttons and If...Then..If/Else
Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 17
Reputation:
Solved Threads: 0
Hello,
I am a newbie and I am learning Visual Basic 2008. My homework is the following:
ublic Class Form1
' This Internet Service provider offers three subscription packages
' to its customers, plus a 20% discount in all packages
' for nonprofit organizations.
' Package A: 10 hours of access for: $ 9.95 per month;
' Additional hours are: $ 2.00 per hour.
' Package B: 20 hours of access for: $ 14.95 per month;
' Additional hours are: $ 1.00 per hour.
'Package C: Unlimited access for: $ 19.95 per month.
' The following class-level constant is used
' to calculate discount.
Const decDiscountRate As Decimal = 0.8D
I created the radio buttons for each package ( they work), and the user has to enter the number of hours using a textbox ( it works after I changed the hours from integer to double).
The display label for TotalAmountDue it works too.
The checkbox captioned ""NonProfit Organization" , it works too.
However, my IF/Then..Else IF statements does not work. The only time it works is when I check package A; then display the $9.95 due.
In addition, I have to use a textbox's validating event to do the validation that hours used in a month cannot exceed 744 (but I will take care of that later).
Could you please tell me, why my loop does not work?
Thank you
Private Sub btnOkay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOkay.Click
' Declare variable
Dim strMessage As String = String.Empty
'The following If..ElseIf..statements test the
' group of radio buttons and copies the first part
' of the message to strMessage.
If radPackageA.Checked = True Then
MessageBox.Show("You selected Package A")
ElseIf radPackageB.Checked = True Then
MessageBox.Show("You selected Package B")
ElseIf radPackageC.Checked = True Then
MessageBox.Show("You selected Package C")
End If
' The following If...Then satements the check box.
If chkNonProfit.Checked = True Then
strMessage = "Non Profit organization"
End If
' Now display the message
MessageBox.Show(strMessage)
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
' Clear the form, reset the buttons and CheckBox.
radPackageA.Checked = True
radPackageB.Checked = True
radPackageC.Checked = True
txtHoursUsed.Clear()
lblNumberofHours.Text = String.Empty
lblTotalAmountDue.Text = String.Empty
' Return the focus to txtHoursUsed
txtHoursUsed.Focus()
End Sub
Private Sub btnexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexit.Click
' End the application by closing the window.
Me.Close()
End Sub
Private Sub btnCalcAmountDue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalcAmountDue.Click
' Declare local variables.
Dim dblHoursUsed As Double ' to hold hours used
Dim dblAmountDue As Double
' Read the value from the TextBox Control.
dblAmountDue = CDbl(txtHoursUsed.Text)
'Calculate the amount due and the 20% discount for (nonprofit)
' using the If/ElseIf statement.
If radPackageA.Checked = True Then
dblAmountDue = 9.95
If (dblHoursUsed > 10) Then
dblAmountDue = 9.95 + (dblHoursUsed - 10) * 2.0
End If
lblTotalAmountDue.Text = CStr(dblAmountDue)
ElseIf radPackageB.Checked = True Then
dblAmountDue = 14.95
ElseIf (dblHoursUsed > 20) Then
dblAmountDue = 14.95 + (dblHoursUsed - 20) * 1.0
If radPackageC.Checked = True Then
dblAmountDue = 19.95
Else(nonprofit) Then
dblAmountDue = dblAmountDue * decDiscountRate
End If
' Display the amount due.
lblTotalAmountDue.Text = CStr(dblAmountDue)
End If
End Sub
Private Sub txtErrorHoursValidation_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtErrorHoursValidation.TextChanged
' Validate the number entered by the user.
If IsNumeric(txtErrorHoursValidation.Text) Then
Dim intHoursUsed As Integer
intHoursUsed = CInt(txtErrorHoursValidation.Text)
If intHoursUsed > 744 Then
lblMessage.BackColor = Color.Red
lblMessage.Text = " Incorrect Data"
End If
End If
End Sub
Private Sub txtErrorHoursValidation_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtErrorHoursValidation.Validating
End Sub
End Class
I am a newbie and I am learning Visual Basic 2008. My homework is the following:
ublic Class Form1
' This Internet Service provider offers three subscription packages
' to its customers, plus a 20% discount in all packages
' for nonprofit organizations.
' Package A: 10 hours of access for: $ 9.95 per month;
' Additional hours are: $ 2.00 per hour.
' Package B: 20 hours of access for: $ 14.95 per month;
' Additional hours are: $ 1.00 per hour.
'Package C: Unlimited access for: $ 19.95 per month.
' The following class-level constant is used
' to calculate discount.
Const decDiscountRate As Decimal = 0.8D
I created the radio buttons for each package ( they work), and the user has to enter the number of hours using a textbox ( it works after I changed the hours from integer to double).
The display label for TotalAmountDue it works too.
The checkbox captioned ""NonProfit Organization" , it works too.
However, my IF/Then..Else IF statements does not work. The only time it works is when I check package A; then display the $9.95 due.
In addition, I have to use a textbox's validating event to do the validation that hours used in a month cannot exceed 744 (but I will take care of that later).
Could you please tell me, why my loop does not work?
Thank you
Private Sub btnOkay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOkay.Click
' Declare variable
Dim strMessage As String = String.Empty
'The following If..ElseIf..statements test the
' group of radio buttons and copies the first part
' of the message to strMessage.
If radPackageA.Checked = True Then
MessageBox.Show("You selected Package A")
ElseIf radPackageB.Checked = True Then
MessageBox.Show("You selected Package B")
ElseIf radPackageC.Checked = True Then
MessageBox.Show("You selected Package C")
End If
' The following If...Then satements the check box.
If chkNonProfit.Checked = True Then
strMessage = "Non Profit organization"
End If
' Now display the message
MessageBox.Show(strMessage)
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
' Clear the form, reset the buttons and CheckBox.
radPackageA.Checked = True
radPackageB.Checked = True
radPackageC.Checked = True
txtHoursUsed.Clear()
lblNumberofHours.Text = String.Empty
lblTotalAmountDue.Text = String.Empty
' Return the focus to txtHoursUsed
txtHoursUsed.Focus()
End Sub
Private Sub btnexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexit.Click
' End the application by closing the window.
Me.Close()
End Sub
Private Sub btnCalcAmountDue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalcAmountDue.Click
' Declare local variables.
Dim dblHoursUsed As Double ' to hold hours used
Dim dblAmountDue As Double
' Read the value from the TextBox Control.
dblAmountDue = CDbl(txtHoursUsed.Text)
'Calculate the amount due and the 20% discount for (nonprofit)
' using the If/ElseIf statement.
If radPackageA.Checked = True Then
dblAmountDue = 9.95
If (dblHoursUsed > 10) Then
dblAmountDue = 9.95 + (dblHoursUsed - 10) * 2.0
End If
lblTotalAmountDue.Text = CStr(dblAmountDue)
ElseIf radPackageB.Checked = True Then
dblAmountDue = 14.95
ElseIf (dblHoursUsed > 20) Then
dblAmountDue = 14.95 + (dblHoursUsed - 20) * 1.0
If radPackageC.Checked = True Then
dblAmountDue = 19.95
Else(nonprofit) Then
dblAmountDue = dblAmountDue * decDiscountRate
End If
' Display the amount due.
lblTotalAmountDue.Text = CStr(dblAmountDue)
End If
End Sub
Private Sub txtErrorHoursValidation_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtErrorHoursValidation.TextChanged
' Validate the number entered by the user.
If IsNumeric(txtErrorHoursValidation.Text) Then
Dim intHoursUsed As Integer
intHoursUsed = CInt(txtErrorHoursValidation.Text)
If intHoursUsed > 744 Then
lblMessage.BackColor = Color.Red
lblMessage.Text = " Incorrect Data"
End If
End If
End Sub
Private Sub txtErrorHoursValidation_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtErrorHoursValidation.Validating
End Sub
End Class
•
•
Join Date: Sep 2009
Posts: 320
Reputation:
Solved Threads: 45
-1
#2 Oct 22nd, 2009
vb Syntax (Toggle Plain Text)
If radPackageA.Checked = True Then dblAmountDue = 9.95 If (dblHoursUsed > 10) Then dblAmountDue = 9.95 + (dblHoursUsed - 10) * 2.0 End If lblTotalAmountDue.Text = CStr(dblAmountDue) ElseIf radPackageB.Checked = True Then dblAmountDue = 14.95 ElseIf (dblHoursUsed > 20) Then dblAmountDue = 14.95 + (dblHoursUsed - 20) * 1.0 If radPackageC.Checked = True Then dblAmountDue = 19.95 Else(nonprofit) Then dblAmountDue = dblAmountDue * decDiscountRate End If ' Display the amount due. lblTotalAmountDue.Text = CStr(dblAmountDue) End If
The only spot your adding any info back to a label is in the first part of that If statement where radPackageA.Checked = True or if A & B are both not check and Hours are greate then 20
Last edited by TomW; Oct 22nd, 2009 at 2:26 pm.
•
•
Join Date: Sep 2009
Posts: 320
Reputation:
Solved Threads: 45
0
#4 Oct 22nd, 2009
That would be caused by a value within the textbox unable to be converted. I have two suggestions for this, the first which is the better solution may not be allowed by your class assignment and that is to use a NumericUpDown control instead of a textbox. It works the same as a textbox but only allows users to enter numbers and it returns a decimal value. My second suggestion for this would be to use Decimal.TryParse or Double.TryParse, both have examples in the help file, if you still need help with that let me know. (I would suggest using decimal over double for currency values)
Im at work now myself so it is hard for me to go line by line & figure out what your coding is doing.
Im at work now myself so it is hard for me to go line by line & figure out what your coding is doing.
•
•
Join Date: Oct 2009
Posts: 17
Reputation:
Solved Threads: 0
0
#5 Oct 22nd, 2009
I fixed!! I initialized the Dim dblHoursUsed As Double = 0 ' to hold hours used. Now, I need to make my If statements works and the nonprofit.
***************************
the If statements only work for the default values. But, then I made some changes to the IF statements, run the program again, and I got the following error message: " Conversion from String to type double is not valid"
Why it works the first time and not the second time? Here is the changes I made to the code:
Dim dblHoursUsed As Double ' to hold hours used
Dim decAmountDue As Decimal
decAmountDue = CDec(CDbl(txtHoursUsed.Text))
' Check the number of hours used and exit if it contains
' invalid data.
If Decimal.TryParse(txtHoursUsed.Text, CDec(dblHoursUsed)) Then
lblNumberofHours.Text = "Success!"
End If
thanks again
***************************
the If statements only work for the default values. But, then I made some changes to the IF statements, run the program again, and I got the following error message: " Conversion from String to type double is not valid"
Why it works the first time and not the second time? Here is the changes I made to the code:
Dim dblHoursUsed As Double ' to hold hours used
Dim decAmountDue As Decimal
decAmountDue = CDec(CDbl(txtHoursUsed.Text))
' Check the number of hours used and exit if it contains
' invalid data.
If Decimal.TryParse(txtHoursUsed.Text, CDec(dblHoursUsed)) Then
lblNumberofHours.Text = "Success!"
End If
thanks again
Last edited by jess99; Oct 22nd, 2009 at 6:33 pm.
•
•
Join Date: Oct 2009
Posts: 8
Reputation:
Solved Threads: 1
0
#9 Oct 22nd, 2009
If radPackageA.Checked = True Then
dblAmountDue = 9.95
If (dblHoursUsed > 10) Then
dblAmountDue = 9.95 + (dblHoursUsed - 10) * 2.0
End If
lblTotalAmountDue.Text = CStr(dblAmountDue)
ElseIf radPackageB.Checked = True Then
dblAmountDue = 14.95ElseIf (dblHoursUsed > 20) Then
dblAmountDue = 14.95 + (dblHoursUsed - 20) * 1.0
If radPackageC.Checked = True Then
dblAmountDue = 19.95
[B]IfElse(B] Then
dblAmountDue = dblAmountDue * decDiscountRate End IfCreate a Const decDiscountRate = .8D
Correct the bolded
Correct The bold areas
Last edited by hhh0505; Oct 22nd, 2009 at 11:16 pm.
![]() |
Similar Threads
- Jmail with radio buttons and menus (Site Layout and Usability)
- javascript radio buttons and insanity (JavaScript / DHTML / AJAX)
- dynamically generated textboxes & radio buttons php, insert into db (PHP)
- binding radio buttons (VB.NET)
- javascript: radio buttons (JavaScript / DHTML / AJAX)
- IE6 - dialogue boxes , check boxes and radio buttons work very slowly after hijacking (Viruses, Spyware and other Nasties)
Other Threads in the VB.NET Forum
- Previous Thread: Having a problem with datagrid view and combobox
- Next Thread: CountDown HH:MM:SS Question
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
.net .net2008 2008 access advanced application array basic beginner browser button buttons center click client code combo convert cuesent data database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationtopic eclipse excel exists fade filter forms function html images input lib listview map mobile module monitor msaccess net number objects open panel pdf picturebox picturebox2 port position print printing problem read regex remove right-to-left save search serial settings shutdown socket sorting sqldatbase sqlserver survey temperature textbox timer timespan transparency txttoxmlconverter user usercontol validation vb vb.net vb2008 vba vbnet visual visualbasic visualbasic.net visualstudio.net visualstudio2008 web webbrowser winforms winsock wpf wrapingcode xml year





