Visual Basic: How Do I?
I frequently get numerous student questions starting with the words "How do I do _____?" The following list of
items is a compilation of those general questions along with their answers, arranged in no particular order.
Use your browser's search facilities to find the answers to your most pressing Visual Basic questions. Enjoy!
- display line numbers in source code - from the main .Net window, select Tools
Options expand Text Editor expand All Languages
select General, and in the Display section, check Line Numbers.
- set/reset development environment settings - from the Menu Bar, select the Tools menu
Import and Export Settings Reset All Settings No, then
select General Development Settings.
- set font size/style in code window - from the Menu Bar, select the Tools menu
Options Environment Fonts and Colors, then select the
font size and/or style you wish, and Apply.
- set tab order - from the form view window, select View menu Tab Order,
then once the numbers appear, click the number on the object you wish to set. Once set, select
View Tab Order to exit. You can also set tab order using the Tab Index property
for each object. Note tab order is only effective for tab-able objects.
- set the default accept button for a form - with the form selected, in the Properties window, find
the AcceptButton property, click on the drop down arrow, and select the event procedure you wish to use when
the user pushes the Enter key.
- set the default escape button for a form - with the form selected, in the Properties window, find
the CancelButton property, click on the drop down arrow, and select the event procedure you wish to use when
the user pushes the Esc key.
- set focus for an object - a) start-up focus is set via the Tab Order where the object with the lowest Tab
Order get initial focus; b) run-time focus is set in an event procedure by calling the objects .Focus() method.
For example, btnExit.Focus(). Note focus is only effective for focus-able objects.
- re-size Label objects - select the Label object you wish to re-size, then set the AutoSize property
to False, then re-size to your heart's content.
- set object color - select the object you wish to set the color of, then find the specific
color property of the object you wish to set (e.g. BackColor), left-click on the drop-down, select the Custom tab, then click
on the color of your object. Note you can also enter a 3 digit RGB value in the format of x, y, z, where the values for
x, y, z range from 0 - 255 (e.g. 75, 0, 130).
- make Labels look like TextBoxes - size Label as above, then set the Labels BackColor property to
White (usually) and set the BorderStyle to Fixed3D.
- split code across a line - the character to split code across a line (called the line continuation
character) is the underscore (_). The _ character must be used within whitespace, and not in the middle of a
word.
- join 2 strings - the character to join strings (called the concatenation operator) is the ampersand (&).
The syntax is "str1" & "str2", where the result of this is "str1str2".
- set Option Strict / Explicit globally - within .Net, select Tools Options
expand Projects and Solutions VB Defaults and select On
for both Option Explicit and Strict. Note these settings will take effect in each new solution created from this
point forward.
- center a form - in the view designer window, select the form, in the properties window, find the
StartPosition property, click the dropdown and click on CenterScreen.
- display a single string on 2 or more lines - use the VB built-in constant vbCrLf, which stands for Visual
Basic Carriage Return/LineFeed. For example, strValue = "this text goes on one line" & vbCrLf & "and this text
goes on another".
- call an event procedure from another procedure - using the name of the object tied to the event
procedure you wish to call, you use the PerformClick method. Thus, if you had an button object called btnReset, and
you wished to call its Click event, you would simply enter btnReset.PerformClick ().
- fix the "given assembly name or codebase was invalid" error - from the Project menu in the menu bar,
select properties for current project (the bottom selection), select the Debug tab,
and un-check the "Enable the Visual Studion hosting process" box.
- fix the "Sub Main not found" error - from the Project menu in the menu bar,
select properties for current project (the bottom selection), select the Startup Object dropdown, and select either
Sub Main or the selection other than Module1. To avoid this problem in the future, reanme the Module1 project by
right clicking on the Module1.vb name in the solution explorer, and select Rename to rename the project.
- nicely format a string - use the .ToString method. See some examples here.
©2007, Mark A. Thomas. All Rights Reserved.