3 Replies - 565 Views - Last Post: 13 October 2009 - 08:24 PM Rate Topic: -----

#1 mbrother64  Icon User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 78
  • Joined: 30-August 09

How do I get it that you can type in a number between 1 and 100?

Post icon  Posted 11 October 2009 - 07:38 PM

No matter what I enter into the english textbox, it keeps on coming up with the OK messagebox. How do I get it that you can type in a number between 1 and 100.


Private Sub btnTotavrg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTotavrg.Click
		'calculating the average and total
		Dim num1 As Integer
		Dim num2 As Integer
		Dim num3 As Integer
		Dim num4 As Integer
		Dim avg As Decimal
		Dim total As Integer
		num1 = Val(txtEnglish.Text)
		num2 = Val(txtMaori.Text)
		num3 = Val(txtMaths.Text)
		num4 = Val(txtSocial.Text)
		avg = (num1 + num2 + num3 + num4) / 4
		total = (num1 + num2 + num3 + num4)
		txtAvrg.Text = avg
		txtTotal.Text = total

		'checking if the user has entered in all marks
		If txtEnglish.Text = "" Or txtMaori.Text = "" Or txtMaths.Text = "" Or txtSocial.Text = "" Then
			MessageBox.Show("You must enter all marks", "Name Entry Error", _
			MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
		End If
	End Sub


	Private Sub txtEnglish_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEnglish.Leave
		'Making sure the form dosent crash if a non-number is entered
		Dim number As Integer
		If Not IsNumeric(txtAge.Text) Then
			MessageBox.Show("Marks must be between 1 and 100 both inclusive", "Invalid Entry", _
			MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
			txtEnglish.Focus()
			txtEnglish.Clear()
		Else
			number = Integer.Parse(txtEnglish.Text)
			number = Integer.Parse(txtEnglish.Text)

			'making sure the marks entered is between 1 and 100
			If number < 15 Or number > 95 Then
				MessageBox.Show("Marks must be between 1 and 100 both inclusive", "Name Entry Error", _
				MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
				txtEnglish.Focus()
				txtEnglish.Clear()
			End If
		End If
	End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: How do I get it that you can type in a number between 1 and 100?

#2 Orillian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 09-October 09

Re: How do I get it that you can type in a number between 1 and 100?

Posted 11 October 2009 - 11:21 PM

				Dim number As Integer
		If Not IsNumeric(txtEnglish.Text) Then ' <- you have txtAge.Text here instead of txtEnglish! 
			MessageBox.Show("Marks must be between 1 and 100 both inclusive", "Invalid Entry", _
			MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
			txtEnglish.Focus()
			txtEnglish.Clear()
		Else
			number = Integer.Parse(txtEnglish.Text)
			number = Integer.Parse(txtEnglish.Text)

			'making sure the marks entered is between 1 and 100
		   If number < 1 Or number > 100 Then ' you had this between 15 and 95 not sure why???
				MessageBox.Show("Marks must be between 1 and 100 both inclusive", "Name Entry Error", _
				MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
				txtEnglish.Focus()
				txtEnglish.Clear()
			End If
		End If



You have the txt.English.Text as txtAge.txt in the first if statement I've copied here and you had your second If as <15 or >95 which won't get all the values between 1 and 100 either. Hope that helps.

On a side note you should try to be a bit more explicit with your variables. Letting Visual Studio deal with the conversions on it's own can be bad.

 
		num1 = CInt(txtEnglish.Text)
		num2 = CInt(txtMaori.Text)
		num3 = CInt(txtMaths.Text)
		num4 = CInt(txtSocial.Text)
		avg = CDec((num1 + num2 + num3 + num4) / 4)
		total = (num1 + num2 + num3 + num4)
		txtAvrg.Text = avg.ToString
		txtTotal.Text = total.ToString



Turning Option Strict on can help you catch these types of things. <Tools - Options - Projects and Solutions - VB Defaults - Option Strict> I'm just learning VB.Net myself, and maybe you have a good reason for it. Or maybe someone else will feel differently then I do, but being explicit just seems like the best idea for when you code starts to grow, it will be a lot easier to find errors if you make it a habit to do those conversions yourself in code.

Anyways hope this gets you pointed in the right direction.

O.

This post has been edited by Orillian: 11 October 2009 - 11:29 PM

Was This Post Helpful? 0
  • +
  • -

#3 rgayan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-September 09

Re: How do I get it that you can type in a number between 1 and 100?

Posted 12 October 2009 - 10:40 AM

what about validating textbox
or put a

if textbox.text 1<>10000 then

action

endif
Was This Post Helpful? 0
  • +
  • -

#4 Metitron  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 76
  • Joined: 13-October 09

Re: How do I get it that you can type in a number between 1 and 100?

Posted 13 October 2009 - 08:24 PM

You could also use a masked-textbox and set the mask to 000 this would however allow inputs of upto 999 i would by pass the text box idea completely however as i do not like having to account for every dumb thing the user might input so i would use a combobox and set the items to 1-100 and set all calls to it to the index and not have anything happen on text input or if an index is not selected. Hope this gives you some other ideas.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1