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: jess99 is an unknown quantity at this point 
Solved Threads: 0
jess99 jess99 is offline Offline
Newbie Poster

Radio buttons and If...Then..If/Else

 
0
  #1
Oct 22nd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 320
Reputation: TomW is on a distinguished road 
Solved Threads: 45
TomW TomW is offline Offline
Posting Whiz
 
-1
  #2
Oct 22nd, 2009
  1. If radPackageA.Checked = True Then
  2. dblAmountDue = 9.95
  3.  
  4. If (dblHoursUsed > 10) Then
  5. dblAmountDue = 9.95 + (dblHoursUsed - 10) * 2.0
  6. End If
  7. lblTotalAmountDue.Text = CStr(dblAmountDue)
  8.  
  9. ElseIf radPackageB.Checked = True Then
  10. dblAmountDue = 14.95
  11. ElseIf (dblHoursUsed > 20) Then
  12. dblAmountDue = 14.95 + (dblHoursUsed - 20) * 1.0
  13.  
  14. If radPackageC.Checked = True Then
  15. dblAmountDue = 19.95
  16. Else(nonprofit) Then
  17. dblAmountDue = dblAmountDue * decDiscountRate
  18. End If
  19.  
  20. ' Display the amount due.
  21. lblTotalAmountDue.Text = CStr(dblAmountDue)
  22. 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: jess99 is an unknown quantity at this point 
Solved Threads: 0
jess99 jess99 is offline Offline
Newbie Poster
 
0
  #3
Oct 22nd, 2009
Thank you but I knew that ( this is my first project).My problem is that I don't know, what I am doing wrong. Now, I got another problem when I run the program, I get the following message : ' Read the value from the TextBox Control.

dblAmountDue = CDbl(txtHoursUsed.Text)
" Invalid cast exception"
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 320
Reputation: TomW is on a distinguished road 
Solved Threads: 45
TomW TomW is offline Offline
Posting Whiz
 
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: jess99 is an unknown quantity at this point 
Solved Threads: 0
jess99 jess99 is offline Offline
Newbie Poster
 
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
Last edited by jess99; Oct 22nd, 2009 at 6:33 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: jess99 is an unknown quantity at this point 
Solved Threads: 0
jess99 jess99 is offline Offline
Newbie Poster
 
0
  #6
Oct 22nd, 2009
Forget it!! It works only the first two runs, then got the same error message again. pls. help!!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: hhh0505 is an unknown quantity at this point 
Solved Threads: 1
hhh0505 hhh0505 is offline Offline
Newbie Poster
 
0
  #7
Oct 22nd, 2009
I have the exact same project lol.... and im having the same problem and i think your in my class.... lol ..
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: jess99 is an unknown quantity at this point 
Solved Threads: 0
jess99 jess99 is offline Offline
Newbie Poster
 
0
  #8
Oct 22nd, 2009
This is funny and sad. I am so overwhelmed with homework , midterms and projects that I cannot spend my time spinning my wheels like I did with the other programs.

Seriously, I get the error at runtime at the third try. I will try to fixed it tomorrow.

Good luck!!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: hhh0505 is an unknown quantity at this point 
Solved Threads: 1
hhh0505 hhh0505 is offline Offline
Newbie Poster
 
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 If

Create a Const decDiscountRate = .8D
Correct the bolded
Correct The bold areas
Last edited by hhh0505; Oct 22nd, 2009 at 11:16 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: jess99 is an unknown quantity at this point 
Solved Threads: 0
jess99 jess99 is offline Offline
Newbie Poster
 
0
  #10
Oct 23rd, 2009
Thank You! I am done with this program and I will start working in Part 2
(Shipping company) tomorrow.

Thanks for all the help
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC