| | |
sharing msgbox code
Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 25
Reputation:
Solved Threads: 0
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:
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
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)
'text box for user input of the loan term and verifies the input values Private Sub txtLoanTerm_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLoanTerm.TextChanged If Me.txtLoanTerm.Text <> "" Then If IsNumeric(txtLoanTerm.Text) Then 'checks input for numeric value If txtLoanTerm.Text > 0 Then 'checks input to make sure number entered is more than zero Else 'show error if conditions are not met MessageBox.Show("Input Error Please Enter a Numeric value Greater Than Zero") Me.txtLoanTerm.Text = "" Me.txtLoanTerm.Focus() End If Else 'show error if conditions are not met MessageBox.Show("Input Error Please enter a valid numeric value") Me.txtLoanTerm.Text = "" Me.txtLoanTerm.Focus() End If End If End Sub
Can anyone help me out on this please?
Thanks,
Ken
Use the same handler to handle all three text boxes:
VB.NET Syntax (Toggle Plain Text)
Private Sub ValidateNumbers(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged Dim ThisBox As TextBox ThisBox = CType(sender, TextBox) If ThisBox.Text <> "" Then If IsNumeric(ThisBox.Text) Then 'checks input for numeric value If CDbl(ThisBox.Text) <= 0 Then 'checks input to make sure number entered is more than zero 'show error if conditions are not met MessageBox.Show("Input Error Please Enter a Numeric value Greater Than Zero") ThisBox.Text = "" ThisBox.Focus() End If Else 'show error if conditions are not met MessageBox.Show("Input Error Please enter a valid numeric value") ThisBox.Text = "" ThisBox.Focus() End If End If End Sub
Teme64 @ Windows Developer Blog
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)
Dim ErrMessage As String If Lastname.Text ="" Then ErrMessage = ErrMessage & "Lastname must have a value." & NewLine End If If Firstname.Text ="" Then ErrMessage = ErrMessage & "Firstname must have a value." & NewLine End If MesagBox.Show(ErrMessage)
A conclusion is the place where you got tired of thinking. http://www.martin2k.co.uk/forums/index.php?showforum=4
http://www.a1vbcode.com/a1vbcode/vbforums/Forum3-1.aspx
http://www.developerfusion.co.uk/for...orum&ForumID=4
•
•
Join Date: Sep 2008
Posts: 25
Reputation:
Solved Threads: 0
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:
At the present time I have it here and it does work but I have to repeat it 3 times again.
Can you tell me what I am doing wrong?
Thanks,
Ken
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)
ElseIf ThisBox.Text.Trim = "" Then MessageBox.Show("Please press continue on the next screen" _ & " and enter the missing information") ThisBox.Focus() 'Set focus to field End If
VB.NET Syntax (Toggle Plain Text)
Dim strInterestRate As String 'string from interest rate text box strInterestRate = Me.txtInterestRate.Text 'checks to make sure the Interest Rate box is not left empyt If Me.txtInterestRate.Text.Trim = "" Then MessageBox.Show("Please press continue on the next screen" _ & " and enter the 'Interest Rate'") Me.txtInterestRate.Focus() 'Set focus to field End If
Thanks,
Ken
Hi! If your question is solved, please mark the thread as solved. Thank you!
Teme64 @ Windows Developer Blog
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:
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.
Let's modify:
VB.NET Syntax (Toggle Plain Text)
Private Sub ValidateNumbers(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged Dim ThisBox As TextBox Dim Msg As String ThisBox = CType(sender, TextBox) If ThisBox.Text <> "" Then If IsNumeric(ThisBox.Text) Then 'checks input for numeric value If CDbl(ThisBox.Text) > 0 Then 'checks input to make sure number entered is more than zero Else 'show error if conditions are not met MessageBox.Show("Input Error Please Enter a Numeric value Greater Than Zero") ThisBox.Text = "" ThisBox.Focus() End If Else 'show error if conditions are not met MessageBox.Show("Input Error Please enter a valid numeric value") ThisBox.Text = "" ThisBox.Focus() End If Else ' HERE THE BOX IS EMPTY ' "Generic" solution MessageBox.Show("Please press continue on the next screen" _ & " and enter the missing data") ThisBox.Focus() ' 2nd solution ' Since you want to identify exact box: Msg = "" If TextBox1.Text.Trim = "" Then Msg = "Interest rate" End If If TextBox2.Text.Trim = "" Then Msg = "Loan term" End If If TextBox3.Text.Trim = "" Then Msg = "This item" End If MessageBox.Show("Please press continue on the next screen" _ & " and enter the '" & Msg & "'") ThisBox.Focus() End If End Sub
Teme64 @ Windows Developer Blog
![]() |
Similar Threads
- Drive List Box Help Please for Error 68 (Visual Basic 4 / 5 / 6)
Other Threads in the VB.NET Forum
- Previous Thread: Problem in clearing old data from datagrid iew in VB.Net
- Next Thread: Problem casting a returned structure
Views: 808 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
.net .net2008 2008 access add advanced application array basic beginner browser button buttons center class click client code combo convert cpu cuesent data database datagrid datagridview datetimepicker design designer dissertation dissertations dissertationtopic eclipse employees excel exists forms function html images lib listview map mobile module msaccess mysql net number open page pan pdf picturebox picturebox2 port position print printing printpreview problem refresh regex reuse richtextbox right-to-left save search serial settings socket sorting sqldatbase sqlserver storedprocedure studio textbox timer timespan transparency txttoxmlconverter usercontol vb vb.net vb2008 vba vbnet vista visual visualbasic visualbasic.net visualstudio.net visualstudio2008 web webbrowser winsock wpf wrapingcode xml year





