468,294 Members | 1,794 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,294 developers. It's quick & easy.

Flow control does not stop after clicking OK in message box

I'm new to the VB programming world and am having a problem with flow control. In the code below I check 3 textboxes and display a messagebox if one or more are left blank. The problem lies in that I don't know how to stop the program flow after the user clicks the OK button on the message box. The program continues to run. I know that I am missing some piece of code to halt the flow. Thank you for any help with this problem. My email is <Removed by Moderator>

Expand|Select|Wrap|Line Numbers
  1. Dim test1 As Decimal  
  2.         Dim test2 As Decimal
  3.         Dim test3 As Decimal
  4.         test1 = xtboxNum1.Text.Length
  5.         test2 = xtboxNum2.Text.Length
  6.         test3 = xtboxNum3.Text.Length
  7.         If test1.Equals(0) Or test2.Equals(0) Or test3.Equals(0) Then
  8.                 MessageBox.Show("One or more numbers are not filled in!") 
  9.         End If
  10. End Sub
Nov 26 '07 #1
3 2307
Dököll
2,364 Expert 2GB
I'm new to the VB programming world and am having a problem with flow control. In the code below I check 3 textboxes and display a messagebox if one or more are left blank. The problem lies in that I don't know how to stop the program flow after the user clicks the OK button on the message box. The program continues to run. I know that I am missing some piece of code to halt the flow. Thank you for any help with this problem. My email is <Removed by Moderator>
Expand|Select|Wrap|Line Numbers
  1. Dim test1 As Decimal  
  2.         Dim test2 As Decimal
  3.         Dim test3 As Decimal
  4.         test1 = xtboxNum1.Text.Length
  5.         test2 = xtboxNum2.Text.Length
  6.         test3 = xtboxNum3.Text.Length
  7.         If test1.Equals(0) Or test2.Equals(0) Or test3.Equals(0) Then
  8.                 MessageBox.Show("One or more numbers are not filled in!") 
  9.         End If
  10. End Sub
Hello Lowrider!

Is this all of the code? It sounds like you should be able to stop the program once you click okay button. Does the program get fired with click of a button?

Expand|Select|Wrap|Line Numbers
  1. Unload Me
  2. YourForm.Show
  3.  
should bring you back to the button, if that's the case.

In a bit!

Dököll
Nov 26 '07 #2
Hello Lowrider!

Is this all of the code? It sounds like you should be able to stop the program once you click okay button. Does the program get fired with click of a button?
Expand|Select|Wrap|Line Numbers
  1. Unload Me
  2. YourForm.Show
  3.  
should bring you back to the button, if that's the case.
In a bit!
Dököll

Dököll, all the code is listed below. Yes a button click starts Private Sub xbutGetMax which calls Private Sub TestForNumbers(). I am trying to test each textbox for content and if an empty textbox is found the message box is displayed. At this point the user clicks OK and I want the program to stop, halt, pause, etc. while the user inputs the number in the empty textbox. The user then clicks the button Private Sub xbutGetMax again and the and the procedure begins again. Does this make sense? Can you help?



Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  3.     End Sub
  4.  
  5.        Private Sub TestForNumbers()
  6.         Dim test1 As Integer                 
  7.         Dim test2 As Integer
  8.         Dim test3 As Integer
  9.         test1 = xtboxNum1.Text.Length       
  10.         test2 = xtboxNum2.Text.Length
  11.         test3 = xtboxNum3.Text.Length
  12.         If test1.Equals(0) Or test2.Equals(0) Or test3.Equals(0) Then
  13.             MessageBox.Show("One or more numbers are not filled in!")
  14.             Me.Show()
  15.         End If
  16.     End Sub
  17.  
  18.     Private Function GetMax(ByVal num1 As String, ByVal num2 As String, ByVal num3 As String) As String
  19.  
  20.         Decimal.TryParse(Me.xtboxNum1.Text, num1)
  21.         Decimal.TryParse(Me.xtboxNum2.Text, num2)
  22.         Decimal.TryParse(Me.xtboxNum3.Text, num3)
  23.         Dim arrayNumbers() As Decimal = {num1, num2, num3}
  24.         Dim max As Decimal = arrayNumbers(0)
  25.         For number As Integer = 1 To arrayNumbers.Length - 1  
  26.             If arrayNumbers(number) > max Then
  27.                 max = arrayNumbers(number)
  28.             End If
  29.         Next number
  30.         Return max                                             
  31.     End Function
  32.  
  33.     Private Sub xbutGetMax_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xbutGetMax.Click
  34.  
  35.         Call TestForNumbers()                              
  36.         Dim max1 As Decimal                                
  37.         Dim num1 As String = Me.xtboxNum1.Text
  38.         Dim num2 As String = Me.xtboxNum2.Text
  39.         Dim num3 As String = Me.xtboxNum3.Text
  40.         max1 = GetMax(CDec(num1), CDec(num2), CDec num3))                       
  41.         Me.xlabMax.Text = "The largest number is " & max1 & "" 
  42.  
  43.     End Sub 
Nov 26 '07 #3
Killer42
8,434 Expert 8TB
I'd suggest you change TestForNumbers from a Sub to a Function. Have it return True for OK, and False for not OK. Then your code can say something like...

Expand|Select|Wrap|Line Numbers
  1. If TestForNumbers() Then
  2.   ' Do the GetMax stuff.
  3. End If
Nov 26 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by C. Alexander | last post: by
11 posts views Thread by Robert Bowen | last post: by
9 posts views Thread by Alvin Bruney [MVP] | last post: by
17 posts views Thread by tshad | last post: by
10 posts views Thread by Dennis D. | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.