Animating a Label

The final example in this chapter demonstrates how to animate a Label control. The UserForm shown in Figure 14-21 is an interactive random number generator.

Figure 14-21: Generating a random number.


Figure 14-21: Generating a random number.

Two TextBox controls hold the lower and upper values for the random number. A Label control initially displays four question marks, but the text is animated to show random numbers when the user clicks the Start button. The Start button changes to a Stop button, and clicking it again stops the animation and displays the random number. Figure 14-22 shows the dialog box displaying a random number between 1 and 10,000.

1 1 Ju ndom Hi untie r Ge riBiu [or (¿¿J 1


jivtmeifsnaon mjnber bt j 1 a-4 | LOt»

Start ~

Figure 14-22: A random number has been chosen.

Figure 14-22: A random number has been chosen.

The code that's attached to the button is as follows:

Dim Stopped As Boolean

Private Sub StartStopButton_Click() Dim Low As Double, Hi As Double

If StartStopButton.Caption = "Start" Then ' validate low and hi values

If Not IsNumeric(TextBox1.Text) Then

MsgBox "Non-numeric starting value.", vbInformation With TextBox1

.SelStart = 0 .SelLength = Len(.Text) .SetFocus End With Exit Sub End If

If Not IsNumeric(TextBox2.Text) Then

MsgBox "Non-numeric ending value.", vblnformation With TextBox2

.SelStart = 0 .SelLength = Len(.Text) .SetFocus End With Exit Sub End If

' Make sure they aren't in the wrong order

Low = Application.Min(Val(TextBox1.Text) , Val(TextBox2.Text)) Hi = Application.Max(Val(TextBox1.Text) , Val(TextBox2.Text))

' Adjust font size, if necessary

Select Case Application.Max(Len(TextBox1.Text), Len(TextBox2.Text)) Case Is < 5: Label1.Font.Size = 72 Case 5: Label1.Font.Size = 60 Case 6: Label1.Font.Size = 48 Case Else: Label1.Font.Size = 36 End Select

StartStopButton.Caption = "Stop" Stopped = False Randomize Do Until Stopped

Label1.Caption = Int((Hi - Low + 1) * Rnd + Low) DoEvents ' Causes the animation

Loop Else

Stopped = True

StartStopButton.Caption = "Start" End If

End Sub

Because the button serves two purposes (starting and stopping), the procedure uses a public variable, Stopped, to keep track of the state. The first part of the procedure consists of two If-Then structures to validate the contents of the TextBox controls. Two more statements ensure that the low value is in fact less than the high value. The next section adjusts the Label control's font size, based on the maximum value. The Do Until loop is responsible for generating and displaying the random numbers. Notice the DoEvents statement. This statement causes Excel to "yield" to the operating system. Without the statement, the Label control would not display each random number as it is generated. In other words, the DoEvents statement is what makes the animation possible.

The UserForm also contains a CommandButton that serves as a Cancel button. This control is positioned off the UserForm so it's not visible. This CommandButton has its Cancel property set to True, so pressing Esc is equivalent to clicking the button. It's click event handler procedure simply sets the Stopped variable to True and unloads the UserForm:

Private Sub CancelButton_Click() Stopped = True Unload Me End Sub

CD- This example, named random number generator .xlsm, is available on the ROM companion CD-ROM.


0 0


  • Laura
    Is it possible animate label text?
    7 years ago

Post a comment