| | |
Homework Help????
Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2009
Posts: 12
Reputation:
Solved Threads: 0
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...
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...
Restrict the textbox to accept number only.
Because you didn't assign Num value to TextBox.Text value
you need to modify your code to
you need to modify your code to
vb.net Syntax (Toggle Plain Text)
Private Sub TxtNum_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtNum.TextChanged Dim Num As Integer Num = Int.Parse(TxtNum.Text) 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
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: Dec 2002
Posts: 461
Reputation:
Solved Threads: 56
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.
It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
•
•
•
•
waynespangler is correct, you don't want to use TextChanged event for that.
My suggestion is to use a KeyPress or KeyUp/KeyDown event.
Try this one:
VB.NET Syntax (Toggle Plain Text)
Private Sub Textbox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Textbox1.KeyPress If e.KeyChar = Chr(Keys.Enter) Then If Me.Textbox1.Text = "" Or Not IsNumeric(Me.Textbox1.Text) Then MsgBox("Invalid Number Format", MsgBoxStyle.Critical) Me.Textbox1.Focus() Me.Textbox1.SelectAll() Exit Sub Else Dim i As Integer = Val(Textbox1.Text) If i >= 100 Then MsgBox("you win") Exit Sub Else MsgBox("you lose") End If End If End If End Sub
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
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
onedizzydevil
Didn't you see my reply?!!
•
•
•
•
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?!!!!!
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
![]() |
Similar Threads
- We only give homework help to those who show effort (Computer Science)
- Need help with Computer Science homework (Computer Science)
- Dynamic memory allocation homework (C++)
- Homework Help!! Priority Queue ?? (Computer Science)
Other Threads in the VB.NET Forum
- Previous Thread: Random function .. help!!
- Next Thread: how To add Multiple columns in CheckedListbox control
| Thread Tools | Search this Thread |
.net .net2008 30minutes 2005 2008 access account arithmetic array basic binary bing button buttons center check code combobox component connectionstring crystalreport data database databasesearch datagrid datagridview date design dissertation dissertations dropdownlist excel fade file-dialog filter folder ftp generatetags google hardcopy images input insert intel internet mobile monitor ms net networking objects output panel passingparameters peertopeervideostreaming picturebox picturebox1 port position print printing problem problemwithinstallation project save searchbox searchvb.net select serial shutdown soap survey table tcp temperature text textbox timer timespan toolbox trim update updown user vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web winforms wpf year






)))) 