sharing msgbox code

Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2008
Posts: 25
Reputation: tyserman5674 is an unknown quantity at this point 
Solved Threads: 0
tyserman5674 tyserman5674 is offline Offline
Light Poster

sharing msgbox code

 
0
  #1
Oct 30th, 2008
Hi All,
I have 3 text boxes that have user input to preform calculations. What I would like to do is if the user inputs improper values to share the error code messages with out having to define the messages in each one of the text boxes using the same code. I have looked into Public Share but am not sure that is the way to go, having a hard time figuring it out anyway.
Here is the code for one of the text boxes:
  1. 'text box for user input of the loan term and verifies the input values
  2. Private Sub txtLoanTerm_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLoanTerm.TextChanged
  3.  
  4. If Me.txtLoanTerm.Text <> "" Then
  5. If IsNumeric(txtLoanTerm.Text) Then 'checks input for numeric value
  6. If txtLoanTerm.Text > 0 Then 'checks input to make sure number entered is more than zero
  7. Else 'show error if conditions are not met
  8. MessageBox.Show("Input Error Please Enter a Numeric value Greater Than Zero")
  9. Me.txtLoanTerm.Text = ""
  10. Me.txtLoanTerm.Focus()
  11. End If
  12. Else 'show error if conditions are not met
  13. MessageBox.Show("Input Error Please enter a valid numeric value")
  14. Me.txtLoanTerm.Text = ""
  15. Me.txtLoanTerm.Focus()
  16. End If
  17. End If
  18.  
  19. End Sub
As it is now I use this code 3 times, just change the name for the text box.
Can anyone help me out on this please?
Thanks,
Ken
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 115
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: sharing msgbox code

 
0
  #2
Oct 30th, 2008
Use the same handler to handle all three text boxes:

  1. Private Sub ValidateNumbers(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged
  2. Dim ThisBox As TextBox
  3.  
  4. ThisBox = CType(sender, TextBox)
  5. If ThisBox.Text <> "" Then
  6. If IsNumeric(ThisBox.Text) Then 'checks input for numeric value
  7. If CDbl(ThisBox.Text) <= 0 Then 'checks input to make sure number entered is more than zero
  8. 'show error if conditions are not met
  9. MessageBox.Show("Input Error Please Enter a Numeric value Greater Than Zero")
  10. ThisBox.Text = ""
  11. ThisBox.Focus()
  12. End If
  13. Else
  14. 'show error if conditions are not met
  15. MessageBox.Show("Input Error Please enter a valid numeric value")
  16. ThisBox.Text = ""
  17. ThisBox.Focus()
  18. End If
  19. End If
  20.  
  21. End Sub
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 300
Reputation: jireh is an unknown quantity at this point 
Solved Threads: 43
jireh's Avatar
jireh jireh is offline Offline
Posting Whiz

Re: sharing msgbox code

 
0
  #3
Oct 30th, 2008
actually I don't understand what you mean...well but if you mean to show their different error messages in one messagbos only, maybe this would help.

  1. Dim ErrMessage As String
  2.  
  3. If Lastname.Text ="" Then
  4. ErrMessage = ErrMessage & "Lastname must have a value." & NewLine
  5. End If
  6.  
  7. If Firstname.Text ="" Then
  8. ErrMessage = ErrMessage & "Firstname must have a value." & NewLine
  9. End If
  10.  
  11. MesagBox.Show(ErrMessage)
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 25
Reputation: tyserman5674 is an unknown quantity at this point 
Solved Threads: 0
tyserman5674 tyserman5674 is offline Offline
Light Poster

Re: sharing msgbox code

 
0
  #4
Oct 30th, 2008
Teme64,
Thank you very much for your help, it works great. I just couldn't figure out how to accomplish it. I searched the web and so forth, no luck on that either.
Thanks Again,
Ken

jireh,
Thank you for your reply, I have have to play with that as well.
Ken
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 25
Reputation: tyserman5674 is an unknown quantity at this point 
Solved Threads: 0
tyserman5674 tyserman5674 is offline Offline
Light Poster

Re: sharing msgbox code

 
0
  #5
Oct 30th, 2008
Teme64,
Sorry to bother you again but for the last couple of hours I have been trying to add in to check the text boxes for no input at all. I have tried adding it to your code so I don't have to repeat this one as well, but it won't work for me either. Here it is:
  1. ElseIf ThisBox.Text.Trim = "" Then
  2. MessageBox.Show("Please press continue on the next screen" _
  3. & " and enter the missing information")
  4. ThisBox.Focus() 'Set focus to field
  5. End If
At the present time I have it here and it does work but I have to repeat it 3 times again.
  1. Dim strInterestRate As String 'string from interest rate text box
  2. strInterestRate = Me.txtInterestRate.Text
  3. 'checks to make sure the Interest Rate box is not left empyt
  4. If Me.txtInterestRate.Text.Trim = "" Then
  5. MessageBox.Show("Please press continue on the next screen" _
  6. & " and enter the 'Interest Rate'")
  7. Me.txtInterestRate.Focus() 'Set focus to field
  8. End If
Can you tell me what I am doing wrong?
Thanks,
Ken
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 115
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: sharing msgbox code

 
0
  #6
Oct 31st, 2008
Hi! If your question is solved, please mark the thread as solved. Thank you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 25
Reputation: tyserman5674 is an unknown quantity at this point 
Solved Threads: 0
tyserman5674 tyserman5674 is offline Offline
Light Poster

Re: sharing msgbox code

 
0
  #7
Oct 31st, 2008
Teme64,
I had asked you, or anyone one more question in regards to this post. If you can try and reply to my last post I would really appreciate it. If not I will close this thread tomorrow.
Thanks,
Ken
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 115
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: sharing msgbox code

 
0
  #8
Nov 1st, 2008
Ok. I don't think you're doing anything wrong. Since the code handles all three boxes and threats all the same, there's no easy way to tell which is the "current" text box.

Let's modify:
  1. Private Sub ValidateNumbers(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged
  2. Dim ThisBox As TextBox
  3. Dim Msg As String
  4. ThisBox = CType(sender, TextBox)
  5. If ThisBox.Text <> "" Then
  6. If IsNumeric(ThisBox.Text) Then 'checks input for numeric value
  7. If CDbl(ThisBox.Text) > 0 Then 'checks input to make sure number entered is more than zero
  8. Else 'show error if conditions are not met
  9. MessageBox.Show("Input Error Please Enter a Numeric value Greater Than Zero")
  10. ThisBox.Text = ""
  11. ThisBox.Focus()
  12. End If
  13. Else 'show error if conditions are not met
  14. MessageBox.Show("Input Error Please enter a valid numeric value")
  15. ThisBox.Text = ""
  16. ThisBox.Focus()
  17. End If
  18. Else ' HERE THE BOX IS EMPTY
  19. ' "Generic" solution
  20. MessageBox.Show("Please press continue on the next screen" _
  21. & " and enter the missing data")
  22. ThisBox.Focus()
  23. ' 2nd solution
  24. ' Since you want to identify exact box:
  25. Msg = ""
  26. If TextBox1.Text.Trim = "" Then
  27. Msg = "Interest rate"
  28. End If
  29. If TextBox2.Text.Trim = "" Then
  30. Msg = "Loan term"
  31. End If
  32. If TextBox3.Text.Trim = "" Then
  33. Msg = "This item"
  34. End If
  35. MessageBox.Show("Please press continue on the next screen" _
  36. & " and enter the '" & Msg & "'")
  37. ThisBox.Focus()
  38. End If
  39. End Sub
Now there's a two ways to do it. You may prefer second solution since you want to give user the exact information of what is missing.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the VB.NET Forum


Views: 810 | Replies: 7
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC