Visual Basic Tips #5


----------------------------------------------

TipWorld - http://www.tipworld.com
The Internet's #1 Source for Computer Tips, News, and Gossip

Proudly presents:
Visual Basic

----------------------------------------------


*1. CODING IN COLOR                 
        
When adding code to a module, VB uses color to display syntax 
errors. A commented statement (or REM statement) is green, and 
keywords are blue. And as you might expect, lines that contain 
errors are red. As a result, you can find most errors with a 
quick glance. Everything else--objects, properties, functions, 
and methods-- is black. 

If your code is correct, VB converts your statements to proper 
case, regardless of how you enter them. So, if VB converts your 
code to proper case, you know VB was able to interpret what you 
entered and that the syntax is correct. (Your code may still 
contain logic errors.) So, entering your code in lowercase is 
another way to verify proper syntax at a glance.


*2. CHANGING EDITING COLORS                 
        
In our last tip, we reviewed the different colors VB uses in the 
module window. If you don't like the default colors--say you want 
keywords to be purple instead of blue--you can change them. 
Simply choose Options from the Tools menu and choose the Editor 
Format tab. 

The Code Color list displays the different components that VB 
can display in different colors. Select the item you want to 
modify and then choose the Foreground, Background, or Indicator 
color as appropriate. 

You might have noticed that you can also change the font type 
and size. Doing so gives you a great deal of flexibility when 
deciding how you want to display the different pieces of your 
code. We recommend that you retain the Courier New font, unless 
you have a specific reason for wanting to change your font. 
Courier New accommodates quite well the indenting conventions 
most developers use, and many other fonts don't. 


*3. CHECKING FOR THE APPLICATION                 
        
It's easy to have more than one copy of an application 
running--the user simply clicks the icon to open the application, 
forgets the application is already running, and clicks the icon 
again. You can avoid this problem by including a simple routine 
in the application's main sub. For instance, the following sub 

Public Sub Main() 
If App.PrevInstance Then 
  BringWindowToTop frmMain.hwnd 
Else 
  Load frmMain 
End If 
End Sub 

checks to see if the application is already running before it 
launches the application's main form. If the App.PrevInstance 
property is True, the sub simply gives the application focus by 
bringing its window to the top of all the current objects. If 
the App.PrevInstance property is False, the sub loads the 
main form.


*4. MOVE VERSUS TOP/LEFT                 
        
Many of us use a control's Top and Left properties to move that 
control. For instance, the following statements will move a 
button named Command to the top-left corner of the current form: 

Command.Top=0 
Command.Left=0 

However, the following code actually performs faster: 

Command.Move 0, 0 

When working with just a few controls you won't notice a 
difference. However, if you're moving several controls, you'll 
notice an appreciable improvement.


*5. SELECTING TEXT                 
        
A common task is to select (highlight) the text in a control. VB 
does this automatically all the time. If you need to select a 
control's text with code, use the following code: 

Screen.ActiveControl.SelStart = 0 
Screen.ActiveControl.SelLength = Len(Screen.ActiveControl.Text) 

This simple code resets the current selection and then reselects 
the entire entry. You can attach this code to an event or you 
can create a sub and call it--either is appropriate.


*6. ABOUT THE CLIP PROPERTY                 
        
A form's Clip property is set to True by default. This means any 
Graphics methods in the Paint event will repaint the entire form. 
When this property is set to False, VB repaints only the newly 
exposed areas. When forms are simple, a True setting has little 
effect on performance. However, a True setting can slow down a 
complex form. So, if your forms don't require the True setting, 
then you should set it to False for quicker load and repaint time.


*7. UPDATING THE TABINDEX ORDER                 
        
A control's TabIndex property determines the tab order. For 
instance, the control with a TabIndex value of 0 is the first 
control to receive focus. When you press Tab, VB will give focus 
to the control whose TabIndex value is 1, and so on. Initially, 
this value is relative to the order in which you add the 
controls. This means that the first control you add to the form 
has a TaxIndex value of 0. The next control will receive a value 
of 1, and so on.  

It's common to move controls around during the design stage, so 
you'll probably need to update this property after you've 
completed the form. A quick and easy way to reset each property 
is to select the object that should be last in the order. Then, 
select the TabIndex property and enter the value 0. Now, select 
the next-to-the-last object, and repeat the process. Continue 
like this until you reach the control that you want to be first 
in the order. At this point, all of the controls should be in 
order. That's because VB resets the higher TabIndex settings 
when you enter a lower value.


*8. NAVIGATING THE VB EDITOR                 
        
Most of us prefer the keyboard to the mouse, since grabbing the 
mouse breaks our data entry momentum. This is true even when 
we're entering code. So, we thought you'd like a few keyboard 
shortcuts for moving about in the VB Editor. 

F7--Jump to the Code window 
F4--Jump to the Properties window 
Ctrl+R--Jump to the Project window 
Ctrl+G--Jump to the Immediate window 
Alt+F11--Toggle between application and VB Editor


*9. NO NEED FOR NUMERIC COUNTER IN FOR...EACH LOOP                 
        
If you're using the For...Each loop as you do the For 
loop--meaning, you're using a numeric counter--stop. You're 
defeating the purpose of the new structure. You see, For Each 
will keep count of the objects you're looping through for you. 
For example, a typical For loop uses a numeric counter in the 
form 

For i = 0 To Forms.Count - 1 
  Forms(i).BackColor = 0 
Next i 

Instead, use the form 

Dim frm as Form 
For Each frm In Forms 
   frm.BackColor = 0 
Next frm 

The For...Each construct is faster and easier to debug.


*10. USING FORMAT                 
        
Rounding and displaying leading zeros can both be a pain in VB. 
However, the Format() function provides a quick way to resolve 
both issues in many cases. For instance, to display leading 
zeros, use the form 

Format(1234,"00000") 

to display a value with five digits--and to display leading zeros as needed. Our example would return the string 01234. 

To round decimal places, use the form 

Format(1.234,".##") 

which will round the value to two decimal places--1.23. 

You can even combine the two in the form 

Format(1234.567,"00000.##") 

This example would return the string 01234.57. Just remember, 
the Format() function returns strings, not values. But if you're 
using the results for display purposes, it's the perfect solution.
