String Functions, Assign Arrays, Add Controls Dynamically

Visual Basic 6.0 introduced several improvements to the core language including string functions that allow you to convert an array to a string (Join), a string to an array (Split), reverse a string (StrReverse), Replace portions of the string, and search a string starting from the end (InStrRev) instead of the beginning. It also lets you to assign arrays to one another, pass them as the return value of a function and Filter out certain elements.

The Validate event lets you validate a control without having to do it in the LostFocus event then set focus back to that control. The CausesValidation property determines if the Validate event should be triggered or not. In this example the textbox's Validate event issues a message when the text property is empty. Clicking any button causes the validation to occur. Except when you click Quit. That is because its CausesValidation property is set False.

Other Visual Basic 6.0 features are the ability to add and remove controls dynamically at runtime and the Call By Name feature. Call By Name lets you access properties and methods of a control by specifying a string containing the name of the property or method, rather than by coding the name explicitly. My Dynamically Add Controls and Create a Scrollable Form page offers another approach to dynamically adding controls.

String Functions, Array Assignment, Dynamically Add Controls,  Call By Name Examples
Download Source Code

Applies To

The features illustrated here first became available in Visual Basic 6.0.

Dynamically Adding and Removing a Control

To add a control dynamically, create an object of the desired type:

  '
  ' Define an object variable of type Command button.
  ' The Withevents statement allows the button object
  ' to respond to events occurring on the new button.
  '
  Private WithEvents btnObj As CommandButton

Add it by setting its properties and adding it to the controls collection:

   Private Sub Form_Load()
     '
     ' Dynamically add a new button to the form. The
     ' item to add, a button in this case, must be a
     ' member of the VB object as displayed in the
     ' Object Browser. The second parameter is the
     ' the new control's name.
     '
     Set btnObj = Controls.Add("VB.CommandButton", "btnObj")
     With btnObj
        .Visible = True
        .Caption = "New Button"
        .Top = 720
        .Left = 120
        .Width = 2175
     End With
   End Sub

Deleted it by removing from the controls collection:

   Private Sub cmdRemove_Click()
      '
      ' Remove the new button and hide the Remove button.
      '  
      Form1.Controls.Remove ("btnObj")
cmdRemove.Visible = False End Sub

Instructions
  • Get the source code and look at the form. Notice there is no button named "New Button" as shown above. Run the program and see that it is added dynamically when the form loads.
  • Hit "Remove New Button" to delete the button.
  • While the textbox is empty, click a button. You get a message telling you to enter a value. Notice that focus remains on the textbox. Now click "Quit" to see that the Quit button does not Cause Validation.
  • Play with the remaining buttons while stepping through the code to best see the effects of the string and array features.

About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2016 TheScarms
Goto top of page