Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TextBox Entry

Status
Not open for further replies.

dataman86

Technical User
Oct 11, 2008
104
US
I have a form where users will be entering numbers in a textbox. I want an error message to appear when the user tabs to the next textbox and they have left the previous textbox blank. I want the error message to say MessageBox.Show ("Must Enter a Number with a decimal")

The two TextBoxes are Debit1.Text and Credit1.Text

I need the message box to appear when the user tabs to Credit1.TextBox

Is there a way to do this?

Dataman86
 
So, every time the user is in one textbox and wants to get to the other one to enter data, you're going to hit them with a message box? You may want to rethink your design, such as only having a single textbox with a Credit/Debit set of radio buttons. Take a look at the Validating event as it should address your specific situation. You may also want to look into replacing the MessageBox pop-up with an ErrorProvider control.
 
Dave,

The user has to enter a number with decimals in the Debit textbox and the Credit textbox. In accounting entries the debits always equal the credits so you can just have one textbox debit/credit entry at least not in my scenario.

I want the user to not just leave the textbox blank and tab over to the Credit Textbox and then enter something. My VB application requires a entry in the textbox. If there is nothing for the debit side, the user must enter 0.00 and not leave the area blank. All I want to do is give the user an error message when they tab to the credit textbox that they can't leave the textbox empty and 0.00 must be entered here if there is nothing.
 
Try adding the following code to your form. Be sure you have some means of exiting the form like the btnExit shown below. Set its CausesValidation property to False. Drag an ErrorProvider onto your form as a replacement for displaying the error in a MessageBox.

Code:
    Private Sub CreditAndDebit_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Credit1.Validating, Debit1.Validating
        Dim am As Decimal

        Dim tb As TextBox = CType(sender, TextBox)
        If (tb.Text Like "*.##" AndAlso Decimal.TryParse(tb.Text, am)) Then
            ' Do nothing
        Else
            ErrorProvider1.SetError(tb, "Must enter a number with a decimal")
            e.Cancel = True
        End If

    End Sub

    Private Sub CreditAndDebit_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Credit1.Validated, Debit1.Validated
        ErrorProvider1.SetError(sender, String.Empty)
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = False 'Ignore validation errors
    End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top