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".
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)
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
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