How to Create a Form at Runtime in VB6
Visual Basic 6 (VB6) can be used to develop many different applications for Microsoft Windows-based computing platforms. Creating a new form for the application user at the time that the application begins to run, or "runtime," is a good practice exercise for novice VB6 programmers who need to learn to present their users with application options at the time the application launches on the user's desktop.
Instructions
-
-
1
Create a list of global variables for the list to follow, for example:
Option Explicit
Private allowNumericOnly As Boolean
Private frm As Form
Private lblDisplay As Button
Private WithEvents cmdOK As CommandButton
Private WithEvents cmdCancel As CommandButton
Private WithEvents textInput As TextBox
-
2
Create a procedure for the form that will determine how the form appears to the user and what, if any text and captions will appear. Use the following example of code to set this up for your form:
Private Sub GenerateRuntimeForm()
Dim ctrl As Control
Set frm = New Form 1
Set cmdOK = Nothing
Set cmdCancel = Nothing
Set textInput = Nothing
Set lblDisplay = Nothing
For Each ctrl In frm
ctrl.Visible = False
Next
-
-
3
Set the different commands for the buttons, using the following code as a basis for your project:
Set cmdOK =
frm.Controls.Add("VB.CommandButton", "cmdOK")
Set cmdCancel =
frm.Controls.Add("VB.CommandButton", "cmdCancel")
Set txtInput =
frm.Controls.Add("VB.TextBox", "txtInput")
-
4
Complete the form code by adding the following display conditions and ending the subroutine with "End Sub" as follows:
cmdOK.Visible = True
cmdCancel.Visible = True
lblDisplay.Visible = True
txtIput.Visible = True
form.sow vbModal
End Sub
-
1