Click here to Skip to main content
15,880,405 members
Articles / Programming Languages / Visual Basic 6
Tip/Trick

VB6 - Quickly Number Format All TextBoxes in a Form

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
13 Mar 2019CPOL2 min read 21.4K   2   5
Automatically clear/format the text box content with few lines of code. The idea sounds good, isn't it? It can be done with a foreach loop and form's control collection.

Introduction

This is a VB6 tutorial which helps beginners to update their VB6 skills.

Background

All of us know that Visual Basic 6 is the simplest way to develop Windows application for desktop computer. In this special tip, we are going to learn a code poem which automatically clears the text box content with few lines of code. The idea sounds good, isn't it?

Using the Code

Firstly, let's make a Sub to clear the fields. I have two methods, the first one lets you clear selected text box which utilizes the paramArray argument and the second will clear the entire field in a form.

Open your Visual Basic 6.0 and enter the following sub routine.

Sub ClearBox for Selected Boxes

VB.NET
Sub ClearBox (ParamArray boxes() As Variant)
Dim ct As Variant
For Each ct In boxes
 If TypeOf ct Is TextBox Then
    ct.Text = ""
 End If
Next
End Sub

This sub will clear the passed textbox controls on the form. The sub utilizes the paramArray feature, so that the program can accept any number of arguments/controls. Remember that paramArray object should be a type of variant.

Clear All the Boxes

The second method will clear all the boxes on the form. Let's take a look at the second code.

VB.NET
Sub ClearBox_Auto(f As  Form)
 Dim ob As Object
 Dim ct As Control

 For Each ob In Form1.Controls
  Set ct = ob
  If TypeOf ct Is TextBox Then
    ct.Text = ""
  End If
 Next
 End Sub

Form's controls property is returning the object collection and we collect each of then to 'ob' and then assign to a control variable and then can do the similar type of operations done in Clear Box sub.

Auto Number Format 'Sub'

Finally, I have a special format box which utilises Forms controls collection to gather controls appeared in the User Interface and apply specific number format to all the number field as follows:

VB.NET
Sub AutoNumberFormat(f As  Form)
 Dim ob As Object
 Dim ct As Control

 For Each ob In Form1.Controls
  Set ct = ob
  If TypeOf ct Is TextBox Then
     If IsNumeric(ct.Text) Then
        ct.Text = Format(ct, "0.00")
     End If
  End If
 Next

End Sub

Call the Box Function

Let's invoke the subroutines on the click event of command button:

VB.NET
'Clear selected boxes
Call ClearBox(TextBox1, TextBox2, TextBox3)

'Clear All the boxes
Call ClearBox_Auto(Me)

'Format numbered fields
Call AutoNumberFormat(Me)

Note

The code is working on Microsoft Word VBA and Visual Studio 6.0 alike. For using on Word VBA, please replace the Form (in argument) with UserForm.

Points of Interest

I found this tricky code, when I was trying to format Text Boxes in our project, we have more than 10 forms and each has many boxes and it becomes full of repetitive codes and I simply re-write it with this idea and it saves my time and space.

History

I will keep looking for code poems and many ideas are rolling out of my head. Keep kooling on my blog Code Poetry for more.

License

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


Written By
Software Developer Shersoftware
India India
I am VB6/.Net/SQL developer who is interested in Microsoft Technology. I am eagerly looking for futuristic ERP projects, and love to write code poetry

Comments and Discussions

 
GeneralVB6 object array Pin
FaizRahmathulla30-Nov-15 6:48
FaizRahmathulla30-Nov-15 6:48 
GeneralRe: VB6 object array Pin
Member 1217340130-Nov-15 15:28
professionalMember 1217340130-Nov-15 15:28 
QuestionIs VB6 still popular today? Pin
leiyangge29-Nov-15 14:28
leiyangge29-Nov-15 14:28 
AnswerRe: Is VB6 still popular today? Pin
FaizRahmathulla30-Nov-15 6:17
FaizRahmathulla30-Nov-15 6:17 
GeneralRe: Is VB6 still popular today? Pin
Member 1217340130-Nov-15 15:24
professionalMember 1217340130-Nov-15 15:24 

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.