4. Common Methods

The Move Method

If a control supports Left, Top, Width, and Height properties, it also supports the Move method, through which you can change some or all four properties in a single operation. The following example changes three properties: Left, Top, and Width.

'Double a form's width, and move it to the upper left corner of 
'the screen.
'Syntax is: Move Left, Top, Width, Height.
Form1.Move 0, 0, Form1.Width * 2

The Refresh Method

The Refresh method causes the control to be redrawn. You normally don't need to explicitly call this method because Visual Basic automatically refreshes the control's appearance when it has a chance (usually when no user code is running and Visual Basic is in an idle state). But you can explicitly invoke this method when you modify a control's property and you want the user interface to be immediately updated:

For n = 1000 To 1 Step -1
    Label1.Caption = CStr(i)
    Label1.Refresh' Update the label immediately.
Next

The SetFocus Method

The SetFocus method moves the input focus on the specified control. You need to call this method only if you want to modify the default Tab order sequence that you implicitly create at design time by setting the TabIndex property of the controls on the form.
The control whose TabIndex property is set to 0 receives the focus when the form loads.
A potential problem with the SetFocus method is that it fails and raises a run-time error if the control is currently disabled or invisible. For this reason, avoid using this method in the Form_Load event (when all controls aren't yet visible) and you should either ensure that the control is ready to receive the focus or protect the method with an On Error statement. Here's the code for the former approach:

'Move the focus to Text1.
If Text1.Visible And Text1.Enabled Then
    Text1.SetFocus
End If

And here's the code for the other possible approach, using the On Error statement:

'Move the focus to Text1.
On Error Resume Next
Text1.SetFocus

No comments:

Post a Comment