'Requires a form with txtTheRealTextBox on it
Private Sub Command1_Click()
Dim varTextBox As TextBox
Set varTextBox = txtTheRealTextbox
varTextBox.Text = "I changed a variable!!!"
Set varTextBox = Nothing 'reclaim the
memory
End Sub
Frequently we step through a collection with a special variant of the for loop called for each:
Private Sub Command1_Click()
Dim theControl As Control
For Each theControl In Form1.Controls
MsgBox (theControl.Name)
theControl.BackColor = QBColor(1)
Next
End Sub
As you can see, this code would first generate a generic control variable
We then will step through the controls collection of the form
Each time through the loop, the current control will be in theControl
The number of iterations will be automatically calculated
We will then msgBox out the name of the control
Finally, we will manipulate the color of the control, if that is appropriate
Here you can see why it is nice to have references.
As another example, this code replicates Java's auto-resize behavior
Option Explicit
Dim XSize As Integer
Dim YSize As Integer
Dim XRatio As Single
Dim YRatio As Single
'resizer
'Andy Harris, 1/29/98
'Demos use of controls as objects and collections
'Note that this will work on pretty much ANY form!!!
Private Sub Form_Load()
'set up initial values for Sizes
XSize = theForm.Width
YSize = theForm.Height
End Sub
Private Sub Form_Resize()
'whenever the form is resized...
'Get new ratio values
XRatio = theForm.Width / XSize
YRatio = theForm.Height / YSize
'reset the values for X and Y sizes
XSize = theForm.Width
YSize = theForm.Height
Dim thing As Control
'step through each element in the built - in controls
'collection
For Each thing In Controls
'We'll get an error for controls without width
and height
'(menus, timer, yada yada)
On Error Resume Next
'Use the ratios to find new values for size
and placement
'of each control
thing.Width = thing.Width * XRatio
thing.Left = thing.Left * XRatio
thing.Height = thing.Height * YRatio
thing.Top = thing.Top * YRatio
Next
End Sub
Dim People As New Collection
Private SearchType As String
Private Sub cmdGo_Click()
If SearchType = "INDEX" Then
MsgBox (People(scrIndex.Value))
ElseIf SearchType = "KEY" Then
MsgBox (People(lstKey.Text))
End If
End Sub
Private Sub Form_Load()
'general initialization
Dim person As String
SearchType = "INDEX"
'add stuff to the collection
'Note use of the with structure to simplify syntax
With People
person = "Bill"
.Add person, "President"
person = "Hillary"
.Add person, "Wife"
person = "Chelsea"
.Add person, "Daughter"
person = "Monica"
.Add person, "That Woman"
person = "Socks"
.Add person, "Cat"
End With
'Initialize the scroll bar
scrIndex.Max = People.Count
'note that the min on a collection is 1,
'but I'll leave this min to zero to test that assumption
'Initialize the list box
lstKey.AddItem ("President")
lstKey.AddItem ("Wife")
lstKey.AddItem ("Daughter")
lstKey.AddItem ("That Woman")
lstKey.AddItem ("Cat")
End Sub
Private Sub optIndex_Click()
If optIndex.Value = True Then
scrIndex.Enabled = True
lstKey.Enabled = False
SearchType = "INDEX"
End If
End Sub
Private Sub optKey_Click()
If optKey.Value = True Then
scrIndex.Enabled = False
lstKey.Enabled = True
SearchType = "KEY"
End If
End Sub
Private Sub scrIndex_Change()
optIndex.Caption = "Index (" + Str$(scrIndex.Value) + ")"
End Sub
'Speller
'Demonstrates Use of Object variables
'Calling Word from VB
'expects cmdSpell, txtSpell
'Word must be installed and NOT CURRENTLY RUNNING
Private Sub cmdSpell_Click()
Dim wordApp As Application
'try to open a new version of word
Set wordApp = CreateObject("Word.Application")
'Check to see if this works!!
'see if wordApp exists. Nothing is a special null
object
'Note also that 'is' is a keyword for comparing objects
If wordApp Is Nothing Then
MsgBox "couldn't start word!!"
End
End If
'Add a new document
wordApp.Documents.Add
'Activate the document
wordApp.ActiveDocument.Activate
'copy text from vb to word
wordApp.ActiveDocument.Range.InsertAfter txtSpell.Text
'run the spell checker
wordApp.ActiveDocument.CheckSpelling
'copy back to VB
txtSpell.Text = wordApp.Selection.Text
'Shut down word
wordApp.Quit (True)
End Sub
Private Sub cmdClone_Click()
Static counter
counter = counter + 1
Load cmdDolly(counter)
cmdDolly(counter).Left = counter * cmdDolly(0).Width
cmdDolly(counter).Caption = counter
cmdDolly(counter).Visible = True
End Sub