943,667 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 994
  • VB.NET RSS
Oct 30th, 2008
0

sharing msgbox code

Expand Post »
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:
VB.NET Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
tyserman5674 is offline Offline
25 posts
since Sep 2008
Oct 30th, 2008
0

Re: sharing msgbox code

Use the same handler to handle all three text boxes:

VB.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Oct 30th, 2008
0

Re: sharing msgbox code

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.

VB.NET Syntax (Toggle Plain Text)
  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)
Reputation Points: 11
Solved Threads: 49
Posting Whiz
jireh is offline Offline
316 posts
since Jul 2007
Oct 30th, 2008
0

Re: sharing msgbox code

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
Reputation Points: 10
Solved Threads: 0
Light Poster
tyserman5674 is offline Offline
25 posts
since Sep 2008
Oct 30th, 2008
0

Re: sharing msgbox code

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:
VB.NET Syntax (Toggle Plain Text)
  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.
VB.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
tyserman5674 is offline Offline
25 posts
since Sep 2008
Oct 31st, 2008
0

Re: sharing msgbox code

Hi! If your question is solved, please mark the thread as solved. Thank you!
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Oct 31st, 2008
0

Re: sharing msgbox code

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
Reputation Points: 10
Solved Threads: 0
Light Poster
tyserman5674 is offline Offline
25 posts
since Sep 2008
Nov 1st, 2008
0

Re: sharing msgbox code

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:
VB.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Problem in clearing old data from datagrid iew in VB.Net
Next Thread in VB.NET Forum Timeline: Problem casting a returned structure





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC