943,666 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 977
  • VB.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 10th, 2009
0

Homework Help????

Expand Post »
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...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Nawsheen is offline Offline
12 posts
since Apr 2009
Apr 10th, 2009
0

Re: Homework Help????

Restrict the textbox to accept number only.
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Apr 10th, 2009
0

Re: Homework Help????

Because you didn't assign Num value to TextBox.Text value
you need to modify your code to
vb.net Syntax (Toggle Plain Text)
  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
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Apr 10th, 2009
0

Re: Homework Help????

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.
Reputation Points: 84
Solved Threads: 58
Posting Pro in Training
waynespangler is offline Offline
461 posts
since Dec 2002
Apr 10th, 2009
0

Re: Homework Help????

waynespangler is correct, you don't want to use TextChanged event for that.
Reputation Points: 19
Solved Threads: 7
Junior Poster in Training
crazyhorse09 is offline Offline
62 posts
since Mar 2009
Apr 13th, 2009
0

Re: Homework Help????

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:

VB.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 11
Solved Threads: 10
Junior Poster in Training
c0deFr3aK is offline Offline
71 posts
since Mar 2009
Apr 13th, 2009
0

Re: Homework Help????

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
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Apr 15th, 2009
0

Re: Homework Help????

Click to Expand / Collapse  Quote originally posted by Nawsheen ...
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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
onedizzydevil is offline Offline
1 posts
since Apr 2009
Apr 15th, 2009
0

Re: Homework Help????

onedizzydevil
Quote ...
You are not setting the Num variable to value that was entered into the textbox; therefore, it will always be 0.
Quote ...
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?!!
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Apr 18th, 2009
0

Re: Homework Help????

thnaks...that worked ))))
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Nawsheen is offline Offline
12 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Random function .. help!!
Next Thread in VB.NET Forum Timeline: how To add Multiple columns in CheckedListbox control





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC