2 Replies - 1903 Views - Last Post: 26 September 2009 - 05:48 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 you have the messagebox come up only once?

Post icon  Posted 26 September 2009 - 06:42 AM

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim num1 As Integer
		Dim num2 As Integer
		Dim num3 As Integer
		Dim total As Integer
		num1 = Val(TextBox1.Text)
		num2 = Val(TextBox2.Text)
		num3 = Val(TextBox3.Text)
		total = (num1 + num2 + num3)
		TextBox4.Text = total

		If textBox1.Text = "" Then
			MessageBox.Show("You must enter all fields", "Name Entry Error", _
			   MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
		End If

		If TextBox2.Text = "" Then
			MessageBox.Show("You must enter all fields", "Name Entry Error", _
			   MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
		End If

		If TextBox3.Text = "" Then
			MessageBox.Show("You must enter all fields", "Name Entry Error", _
			   MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
		End If

	End Sub


If I dont enter any text into any of the first three textboxes; then how do I have the messagebox only come up once? Instead it'll come up three times.

This post has been edited by mbrother64: 26 September 2009 - 06:44 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How do you have the messagebox come up only once?

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1532
  • View blog
  • Posts: 4,908
  • Joined: 25-September 09

Re: How do you have the messagebox come up only once?

Posted 26 September 2009 - 07:01 AM

View Postmbrother64, on 26 Sep, 2009 - 05:42 AM, said:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim num1 As Integer
		Dim num2 As Integer
		Dim num3 As Integer
		Dim total As Integer
		num1 = Val(TextBox1.Text)
		num2 = Val(TextBox2.Text)
		num3 = Val(TextBox3.Text)
		total = (num1 + num2 + num3)
		TextBox4.Text = total

		If textBox1.Text = "" Then
			MessageBox.Show("You must enter all fields", "Name Entry Error", _
			   MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
		End If

		If TextBox2.Text = "" Then
			MessageBox.Show("You must enter all fields", "Name Entry Error", _
			   MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
		End If

		If TextBox3.Text = "" Then
			MessageBox.Show("You must enter all fields", "Name Entry Error", _
			   MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
		End If

	End Sub


If I dont enter any text into any of the first three textboxes; then how do I have the messagebox only come up once? Instead it'll come up three times.


Check against all the TextBoxes in a single If Then statement.

If Textbox1.Text = "" or Textbox2.Text = "" or Textbox3.Text = "" then
			MessageBox.Show("You must enter all fields", "Name Entry Error", _
			   MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
		End If




You will also need to perform this check prior to performing the calculations and then exit out of the routine if any textbox is blank.

This post has been edited by CharlieMay: 26 September 2009 - 07:04 AM

Was This Post Helpful? 1
  • +
  • -

#3 illicit  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 01-August 09

Re: How do you have the messagebox come up only once?

Posted 26 September 2009 - 05:48 PM

View Postmbrother64, on 26 Sep, 2009 - 05:42 AM, said:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim num1 As Integer
		Dim num2 As Integer
		Dim num3 As Integer
		Dim total As Integer
		num1 = Val(TextBox1.Text)
		num2 = Val(TextBox2.Text)
		num3 = Val(TextBox3.Text)
		total = (num1 + num2 + num3)
		TextBox4.Text = total

		If textBox1.Text = "" Then
			MessageBox.Show("You must enter all fields", "Name Entry Error", _
			   MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
		End If

		If TextBox2.Text = "" Then
			MessageBox.Show("You must enter all fields", "Name Entry Error", _
			   MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
		End If

		If TextBox3.Text = "" Then
			MessageBox.Show("You must enter all fields", "Name Entry Error", _
			   MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
		End If

	End Sub


If I dont enter any text into any of the first three textboxes; then how do I have the messagebox only come up once? Instead it'll come up three times.

Hi there
Im not sure if you want or need the button but for an alternative you cold also try to validate as they leave the textbox
	Private Sub Validation(ByVal sender As System.Object, ByVal e As System.EventArgs) _
			Handles TextBox1.Leave, TextBox2.Leave, TextBox3.Leave, TextBox4.Leave
		Dim num1 As Integer
		Dim num2 As Integer
		Dim num3 As Integer
		Dim total As Integer
		Dim TextBoxValid As TextBox = CType(sender, TextBox)
		num1 = Val(TextBox1.Text)
		num2 = Val(TextBox2.Text)
		num3 = Val(TextBox3.Text)
		total = (num1 + num2 + num3)
		TextBox4.Text = total
		If IsNotValid(TextBoxValid.Text) Then
			MessageBox.Show("You must enter all fields", "Name Entry Error", _
				MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
			TextBoxValid.Focus()
		Else

		End If

	End Sub


	Private Function IsNotValid(ByVal Something As String) As Boolean
		Return Something = ""
	End Function



This is just an alternative, but if you need the button then what CharlieMay wrote is spot on except you may want to have it like this
If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Then
			MessageBox.Show("You must enter all fields", "Name Entry Error", _
			   MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
		Else
			total = (num1 + num2 + num3)
			TextBox4.Text = total
End If


This post has been edited by illicit: 26 September 2009 - 06:04 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1