Click here to Skip to main content
15,892,059 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Emulating HTML Text boxes Value Attribute in VB.NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
2 Jan 2016CPOL1 min read 9.3K   2   2
Emulating HTML Text boxes Value attribute in VB.NET text boxes

Introduction

If you want to avoid using labels to identify what goes in text boxes, or need extra room in your form, this would be useful.

The TEXT properties of text boxes in the form are automatically filled, given the NAME properties follow certain criteria and I'll give an example: txtCity, the NAME property for a text box indicates a city must be typed in, it must start at the 4th position. Once you run the program, and if you use the BACK key all the way while inside any text box, and delete any text, upon leaving, (LEAVE event is triggered), the field is "auto completed", so you always know what goes in the text box. It's up to you to provide any extra validation.

Using the Code

I created a form, Form1, in the load event, I centered the form, set its Border Style, its size and call the FillTextBox sub.

The sub then sets the objects and line containing ct.Text = Microsoft.VisualBasic.Right(ct.Name, Len(ct.Name) - 3) assigns to all text boxes' TEXT properties the contents of the NAME properties, extracting starting at the 4th position to the right, thus filling them up with what is to be shown in those text boxes.

In my form, I created 5 text boxes and named (Name property) them: txtName, txtLast, txtAddress, txtCity, txtZipCode, so when you run the program, the text boxes will show: Name, Last, Address, City, ZipCode.

The 2nd part of the FillTextBox sub then sets the LEAVE event of the text boxes and the txtLeave sub is invoked.

Once the txtLeave sub is called, if the text field is empty upon leaving it, then it is automatically "auto completed".

VB.NET
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.CenterToScreen()
        Me.FormBorderStyle = FormBorderStyle.FixedToolWindow
        Me.Size = New Size(373, 271)
        FillTextBox(Me)
    End Sub

    '
    Private Sub FillTextBox(f As Form)
        'FILLING UP TEXT BOXES TEXT PROPERTIES PROGRAMMATICALLY
        Dim ob As Object : Dim ct As Control
        For Each ob In f.Controls
            ct = ob
            ct.Text = Microsoft.VisualBasic.Right(ct.Name, Len(ct.Name) - 3)
        Next

        ' EVENT HANDLER FOR THE LEAVE EVENT
        Dim obTextBoxes = Me.Controls.OfType(Of TextBox)()
        For Each txt In obTextBoxes
            AddHandler txt.Leave, AddressOf txtLeave
        Next
        '
    End Sub

    '
    Private Sub txtLeave(sender As Object, e As EventArgs)
        Dim txt = DirectCast(sender, TextBox)
        If Len(txt.Text) = 0 Then
            txt.Text = Microsoft.VisualBasic.Right(txt.Name, Len(txt.Name) - 3)
        End If
    End Sub
End Class

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member doesn't quite have enough reputation to be able to display their biography and homepage.

Comments and Discussions

 
BugEmulating HTML Text boxes Value attribute in VB.NET Pin
Mauricio Orejuela Bohorquez3-Jan-16 16:09
Mauricio Orejuela Bohorquez3-Jan-16 16:09 
QuestionEmulating HTML Text boxes Value attribute in VB.NET Pin
Mauricio Orejuela Bohorquez3-Jan-16 8:30
Mauricio Orejuela Bohorquez3-Jan-16 8:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.