How to Check if a Textbox Is Empty in VB

Save

Visual Basic is an event-driven programming language and integrated development environment developed and deployed by Microsoft Corporation. Visual Basic originated from BASIC. The language conforms to Microsoft's Component Object Model (COM) software development standards. The language makes heavy use of reusable code, referred to as active X controls and objects. Using the VB programming environment, users can easily drag and drop controls into their VB programs and have access to their events, properties and attributes. Placing a textbox control into your VB program and checking to see if it's empty or not takes but a few simple steps.

  • Open Visual Basic.NET.

  • Select "Windows Application" in the "New Project" window and give your program a name. Click "OK." A default visual basic form1 should now be created and displayed on your screen.

  • Click on "Button" from the toolbar and draw it onto the form.

  • Click on "Textbox" from the toolbar and draw it onto the form.

  • Double-click on the "Button" object. That will open the code area for the button.

  • Enter the following code in between the Button1's click event sub procedure.

    Type: If TextBox1.Text="" then MsgBox("This text box is empty") Else Msgbox("This text box is not empty").

    Your code should look like the following:

    Public Class Form1

    Private Sub Button1Click(ByVal sender As System.Object,

    ByVal e As System.EventArgs) _

    Handles Button1.Click

    If TextBox1.Text = "" Then

     MsgBox ("the text box is empty")

    Else

    MsgBox ("the text box is not empty")

    End If

    End Sub

    End Class

    The preceding code Textbox1.text="" checks the value in the textbox by using an if-else statement to evaluate it for a value.

  • Press "F5" on your keyboard to run the program.

  • Enter some information in the textbox and press the button. You should get a pop-up message box with the words "The Text Box Is Not Empty" in the middle of it.

  • Erase the information in the text box and press the button. You should get a pop-up message box with the words "The Text Box Is Empty" in the middle of it.

References

Promoted By Zergnet

Comments

Resources

You May Also Like

Related Searches

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!