Homework Help????

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2009
Posts: 12
Reputation: Nawsheen is an unknown quantity at this point 
Solved Threads: 0
Nawsheen Nawsheen is offline Offline
Newbie Poster

Homework Help????

 
0
  #1
Apr 10th, 2009
I am a begginer in vb.net. Actually i am having problems to do my homework. I will be posting both the question and my answer.

Question

Create a small program that allows you to enter a number. If the number is greater than 100, you will see the "You win a lucky prize" statement in a message box. Else if the number is less that 100, the message box displays “Better luck next time”.

Answer
Private Sub TxtNum_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtNum.TextChanged

Dim Num As Integer

If Num > 100 Then
MsgBox("You win a lucky prize")
ElseIf Num < 100 Then
MsgBox("Better luck next time")

End If

End Sub
End Class

My problem

When i debug the database my form appears but however when i enter a number lets say zero in the textbox the following message is displayed "Better Luck next time". The textbox is not working for two digit numbers or three digit numbers. And neither is the condition if (Num > 100) working. Can u plz explain as to what is wrong in my code.

Thanks...
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,824
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 117
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: Homework Help????

 
0
  #2
Apr 10th, 2009
Restrict the textbox to accept number only.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Homework Help????

 
0
  #3
Apr 10th, 2009
Because you didn't assign Num value to TextBox.Text value
you need to modify your code to
  1. Private Sub TxtNum_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtNum.TextChanged
  2. Dim Num As Integer
  3. Num = Int.Parse(TxtNum.Text)
  4. If Num > 100 Then
  5. MsgBox("You win a lucky prize")
  6. ElseIf Num < 100 Then
  7. MsgBox("Better luck next time")
  8. End If
  9. End Sub
  10. End Class
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Dec 2002
Posts: 461
Reputation: waynespangler is on a distinguished road 
Solved Threads: 56
waynespangler waynespangler is offline Offline
Posting Pro in Training

Re: Homework Help????

 
0
  #4
Apr 10th, 2009
By using text changed you are checking everytime a new character is entered. So the first character entered is 1 and you get a <100. The next character makes it a 10 so it is still less than 100. If the next character is a 0 you get a 100 so it doesn't do any thing. Instead use a button click event and I would use <100 and >=100.
Wayne

It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 44
Reputation: crazyhorse09 is an unknown quantity at this point 
Solved Threads: 4
crazyhorse09 crazyhorse09 is offline Offline
Light Poster

Re: Homework Help????

 
0
  #5
Apr 10th, 2009
waynespangler is correct, you don't want to use TextChanged event for that.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 66
Reputation: c0deFr3aK is an unknown quantity at this point 
Solved Threads: 10
c0deFr3aK's Avatar
c0deFr3aK c0deFr3aK is offline Offline
Junior Poster in Training

Re: Homework Help????

 
0
  #6
Apr 13th, 2009
Originally Posted by crazyhorse09 View Post
waynespangler is correct, you don't want to use TextChanged event for that.
That's right textchanged is not recommended.

My suggestion is to use a KeyPress or KeyUp/KeyDown event.

Try this one:

  1. Private Sub Textbox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Textbox1.KeyPress
  2. If e.KeyChar = Chr(Keys.Enter) Then
  3. If Me.Textbox1.Text = "" Or Not IsNumeric(Me.Textbox1.Text) Then
  4. MsgBox("Invalid Number Format", MsgBoxStyle.Critical)
  5. Me.Textbox1.Focus()
  6. Me.Textbox1.SelectAll()
  7. Exit Sub
  8. Else
  9. Dim i As Integer = Val(Textbox1.Text)
  10. If i >= 100 Then
  11. MsgBox("you win")
  12. Exit Sub
  13. Else
  14. MsgBox("you lose")
  15. End If
  16. End If
  17. End If
  18. End Sub
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Homework Help????

 
0
  #7
Apr 13th, 2009
Hey friends!!!! how could you answer their question without looking at their code?!!!! which line they assigned textbox value to Num object?!!!!!
Last edited by Ramy Mahrous; Apr 13th, 2009 at 5:00 am. Reason: Adding question mark
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 1
Reputation: onedizzydevil is an unknown quantity at this point 
Solved Threads: 1
onedizzydevil onedizzydevil is offline Offline
Newbie Poster

Re: Homework Help????

 
0
  #8
Apr 15th, 2009
Originally Posted by Nawsheen View Post
Dim Num As Integer
You are not setting the Num variable to value that was entered into the textbox; therefore, it will always be 0.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Homework Help????

 
0
  #9
Apr 15th, 2009
onedizzydevil
You are not setting the Num variable to value that was entered into the textbox; therefore, it will always be 0.
By Ramy Mahrous

Hey friends!!!! how could you answer their question without looking at their code?!!!! which line they assigned textbox value to Num object?!!!!!
Didn't you see my reply?!!
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 12
Reputation: Nawsheen is an unknown quantity at this point 
Solved Threads: 0
Nawsheen Nawsheen is offline Offline
Newbie Poster

Re: Homework Help????

 
0
  #10
Apr 18th, 2009
thnaks...that worked ))))
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC