If you want to know whether an object is of a special type, write the following code:
If TypeOf obj Is ListItem Then ...
If you want to get the name of an object type, use:
MsgBox "The item is a " & TypeName(obj)
Control long loopsWhen the program is stuck in a long loop, you can't click any command buttons or keys. This is very difficult for users to control. Maybe the user no longer wants to execute the loop and exits by clicking the Cancel button? How to solve this problem? The answer is: use the DoEvents command! That is, execute the DoEvents command at the beginning of the loop, like the following code:
Dim i As Long
For i = 1 To 1000000
DoEvents
...
Next i
In fact, the DoEvents command does not need to be placed on line 1, it can be placed anywhere in the loop. By doing this, other parts of the program, such as a command button that ends the loop, will have another chance to accept click events.
Make long loops run fasterI have found that, in general, it is better to use the API function GetInputState to check the value of the program's input queue. I don't know why the If statement makes the code run faster, but it actually works just fine.
Therefore, the following line of code can be
DoEvents
Replace with
If GetInputState() Then DoEvents
Give it a try!
In fact, the reason for this is: the DoEvents statement allows any application to perform related events, not just your own program. After adding the GetInputState judgment, you can only accept event actions from your own program!
Make Select Case easierLet's look at the following example: building a string from the user clicking on different combinations of tags:
lbl(0) = "Hello"
lbl(1) = "I comment: m "
lbl(2) = "Happy"
lbl(3) = "Sad"
...
sub lbl_click (index as integer)
string$ = string$+lbl(index).caption
Then, you have to write a series of select case statements to analyze this string...
select case string$
case "Hello I comment: m Happy"
do something
case "Hello I comment: m"
this would be an error...
case else
do something else
end select
As you can see, this would be very complex, especially if there are many tags and legal checks for merge operations are required.
To solve this problem, I created an array with the same length as the number of tags:
arraylbl(n) as integer
The starting value of the array is 1, and then the value of each item is equal to the previous value multiplied by 2 (value*2), just like this: 1 2 4 8 16 32 64... In this way, the combination of each pair of labels will Another unique value"
lbl(0) + lbl(1) = 1 + 2 = 3
lbl(0) + lbl(1) + lbl(4) = 1 + 2 + 16 = 19
Now, when a label is clicked, its corresponding lblarray() value is added to the variable. This way, based on a few numbers, you can simply write a select case statement:
select case IsLegal
case 3
Note: the string was valid
call goodstuff
case 19
Note: the string was valid
call goodstuff
caseelse
Note: invalid
call nogood
end select
Haha, sometimes, digitizing characters can have unexpectedly good effects!