7 Replies - 1628 Views - Last Post: 02 October 2009 - 06:19 AM 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 make the messagebox automatically come up?

Post icon  Posted 02 October 2009 - 01:07 AM

Dim number As Integer
		TextBox1.Text = number
		If number < 1 Or number > 100 Then

			MessageBox.Show("You must enter a value between 1 and 100", "Name Entry Error", _
			MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
			TextBox1.Focus()
			TextBox1.Clear()
			Exit Sub


How do I make it when you type in a number above or below 1 and 100 the messagebox comes up? I can only make the msgbox come up after Ive clicked the button, how do I make it automatically come up when I try to click anywhere else?

Is This A Good Question/Topic? 0
  • +

Replies To: How do I make the messagebox automatically come up?

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: How do I make the messagebox automatically come up?

Posted 02 October 2009 - 01:19 AM

TextBox has about 50 events. The purpose of the events is to provide the correct means for this kind of things, like when you enter text to the textbox, remove it from the Form, or in your case, when you leave the control so it's no longer focused.

So, in your case, you'd have to use the Leave event of TextBox:

  Private Sub TextBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
	' Your logic here '
  End Sub


Was This Post Helpful? 1
  • +
  • -

#3 mbrother64  Icon User is offline

  • D.I.C Head

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

Re: How do I make the messagebox automatically come up?

Posted 02 October 2009 - 01:50 AM

Private Sub TextBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
		Dim number As Integer
		TextBox1.Text = number
		If number < 1 Or number > 100 Then
			MessageBox.Show("You must enter a value between 1 and 100", "Name Entry Error", _
			MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
			TextBox1.Focus()
			TextBox1.Clear()
		End If
		Exit Sub
	End Sub


I tried this, but now when I leave the textbox the messagebox automatically comes up even if I type in a number between 1 and 100.
Was This Post Helpful? 0
  • +
  • -

#4 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: How do I make the messagebox automatically come up?

Posted 02 October 2009 - 01:53 AM

You have this line wrong:
TextBox1.Text = number



It should be:
number = Integer.Parse(TextBox1.Text)


Was This Post Helpful? 0
  • +
  • -

#5 mbrother64  Icon User is offline

  • D.I.C Head

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

Re: How do I make the messagebox automatically come up?

Posted 02 October 2009 - 02:12 AM

Ive now tried that and it now lets me type in a number between 1 and 100. But now whenever I type in non-numbers it crashes. I thought being declared an integer would've taken care of that. Heres my code now:

Private Sub TextBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
		Dim number As Integer
		number = Integer.Parse(TextBox1.Text)
		If number < 1 Or number > 100 Then
			MessageBox.Show("You must enter a value between 1 and 100", "Name Entry Error", _
			MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
			TextBox1.Focus()
			TextBox1.Clear()
		End If
		Exit Sub
	End Sub

Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: How do I make the messagebox automatically come up?

Posted 02 October 2009 - 04:20 AM

You will have to check to ensure it is numeric before checking the value.
Private Sub TextBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
		Dim number As Integer
		number = Integer.Parse(TextBox1.Text)
		If Not isNumeric(TextBox1.Text) Then
			MessageBox.Show("Numbers only please","Invalid Entry", _
messageBoxButtons.OK, MesageBoxIcon.Exclamation)
TextBox1.Focus()
TextBox1.Clear()
else
		If number < 1 Or number > 100 Then
			MessageBox.Show("You must enter a value between 1 and 100", "Name Entry Error", _
			MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
			TextBox1.Focus()
			TextBox1.Clear()
		End If
End If
		Exit Sub
	End Sub


This post has been edited by CharlieMay: 02 October 2009 - 04:21 AM

Was This Post Helpful? 0
  • +
  • -

#7 mbrother64  Icon User is offline

  • D.I.C Head

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

Re: How do I make the messagebox automatically come up?

Posted 02 October 2009 - 05:51 AM

No Charlie, your code dosent work, it just crashes.
Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: How do I make the messagebox automatically come up?

Posted 02 October 2009 - 06:19 AM

Sorry mbrother,

I missed a line when I moved your code down under the else statement

the line: number = Integer.Parse(TextBox1.Text)

should be moved below the else line

		Dim number As Integer
		If Not IsNumeric(TextBox1.Text) Then
			MessageBox.Show("Numbers only please", "Invalid Entry", _
							MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
			TextBox1.Focus()
			TextBox1.Clear()
		Else
			number = Integer.Parse(TextBox1.Text)
			If number < 1 Or number > 100 Then
				MessageBox.Show("You must enter a value between 1 and 100", "Name Entry Error", _
							   MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
				TextBox1.Focus()
				TextBox1.Clear()
			End If
		End If
		Exit Sub


This post has been edited by CharlieMay: 02 October 2009 - 06:21 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1