By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,057 Members | 1,346 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,057 IT Pros & Developers. It's quick & easy.

Flow control does not stop after clicking OK in message box

P: 2
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
Share this Question
Share on Google+
3 Replies


Dököll
Expert 100+
P: 2,364
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

P: 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

Expert 5K+
P: 8,434
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.