|
Server : Apache/2.4.62 System : FreeBSD fbsdweb2.web.rcn.net 14.1-RELEASE FreeBSD 14.1-RELEASE releng/14.1-n267679-10e31f0946d8 GENERIC amd64 User : www ( 80) PHP Version : 8.3.8 Disable Function : NONE Directory : /domains/bburke/xInHouseWebPages/ |
Upload File : |
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Visual Basic Tips</title>
<meta name="Microsoft Theme" content="copy-of-blends 011, default">
</head>
<body background="../_themes/copy-of-blends/blegtext.gif" bgcolor="#CCCCCC" text="#000000" link="#0000FF" vlink="#CC3300" alink="#0000FF"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
<p align="center"><font size="7">Visual Basic Tips</font></p>
<!--msthemeseparator--><p align="center"><img src="../_themes/copy-of-blends/blesepa.gif" width="600" height="10"></p>
<div align="center">
<center>
<!--mstheme--></font><table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="200" id="AutoNumber3">
<tr>
<td width="100%"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
<p align="center"><a href="Index.htm">Main Page</a><!--mstheme--></font></td>
</tr>
</table><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
</center>
</div>
<p align="center"><b><font size="4">Tips added 08/31/2004</font></b></p>
<div align="center">
<center>
<!--mstheme--></font><table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="100%" id="AutoNumber4" height="249" bordercolordark="#000000" bordercolorlight="#999999">
<tr>
<td width="50%" align="center" height="22"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
<a href="#A Try...Catch alternative to GoTo...Resume (VB 6/ VB .NET)">A Try...Catch alternative to
GoTo...Resume (VB 6/ VB .NET)</a><!--mstheme--></font></td>
<td width="50%" align="center" height="22"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
<a href="#Put your VB application into deep sleep (VB 6/.NET)">Put your VB
application into deep sleep (VB 6/.NET)</a><!--mstheme--></font></td>
</tr>
<tr>
<td width="50%" align="center" height="22"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
<a href="#Beware of CInt; use CDbl instead (VB 6.0)">Beware of CInt; use CDbl
instead (VB 6.0)</a><!--mstheme--></font></td>
<td width="50%" align="center" height="22"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
<a href="#Restrict TextBox entries using the KeyPress() event (VB 6)">
Restrict TextBox entries using the KeyPress() event (VB 6)</a><!--mstheme--></font></td>
</tr>
<tr>
<td width="50%" align="center" height="42"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
<a href="#Call a function or subroutine with its string name using the Invoke() method (VB .NET)">Call a function or subroutine
with its string name using the Invoke() method (VB .NET)</a><!--mstheme--></font></td>
<td width="50%" align="center" height="42"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
<a href="#The Val() function bug that wont go away (VB 6/.NET)">The Val()
function bug that won't go away (VB 6/.NET)</a><!--mstheme--></font></td>
</tr>
<tr>
<td width="50%" align="center" height="22"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
<a href="#Create a Universal Data Link file as an alternative to hard-coding connection strings (VB 6.0)">
Create a Universal Data Link file as an alternative to hard-coding
connection strings (VB 6.0)</a><!--mstheme--></font></td>
<td width="50%" align="center" height="22"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
<span style="font-face: Courier New">
<a href="#Three ways to lock down a form in Visual Basic (VB 6)">Three
ways to lock down a form in Visual Basic (VB 6)</a></span><!--mstheme--></font></td>
</tr>
<tr>
<td width="50%" align="center" height="22"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
<a href="#Def declarations are now defunct in the new framework (VB 6/VB .NET)">
Def
<type></type>
declarations are now defunct in the new framework (VB 6/VB .NET) </a><!--mstheme--></font></td>
<td width="50%" align="center" height="22"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
<a href="#Understanding the difference between the form Load() and Activate() events (VB 6.0)">
Understanding the difference between the form Load() and Activate() events
(VB 6.0)</a><!--mstheme--></font></td>
</tr>
<tr>
<td width="50%" align="center" height="22"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
<a href="#Easily access the days of the week with the DayofWeek Enumeration (VB .NET)">
Easily access the days of the week with the DayofWeek Enumeration (VB
.NET)</a><!--mstheme--></font></td>
<td width="50%" align="center" height="22"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
<a href="#Use Structure instead of Type to create user-defined types (VB .NET)">
Use Structure instead of Type to create user-defined types (VB .NET)</a><!--mstheme--></font></td>
</tr>
<tr>
<td width="50%" align="center" height="22"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
<a href="#How to ignore textbox keystrokes in VB 6 and VB .NET (VB 6/.NET)">
How to ignore textbox keystrokes in VB 6 and VB .NET (VB 6/.NET)</a><!--mstheme--></font></td>
<td width="50%" align="center" height="22"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
<a href="#Use the current time value to create custom greetings for users (VB .NET)">
Use the current time value to create custom greetings for users (VB .NET)</a><!--mstheme--></font></td>
</tr>
<tr>
<td width="50%" align="center" height="22"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
<a href="#Locate a variable in your project with the Definition menu option (VB IDE/Visual Studio .NET)">
Locate a variable in your project with the Definition menu option (VB
IDE/Visual Studio .NET)</a><!--mstheme--></font></td>
<td width="50%" align="center" height="22"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
<a href="#Use the Show() method to make one form the owner of another (VB 6)">
Use the Show() method to make one form the owner of another (VB 6)</a><!--mstheme--></font></td>
</tr>
<tr>
<td width="50%" align="center" height="22"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
<span style="font-face: Courier New">
<a href="#Option Base is no longer an option (VB 6/VB .NET)">Option Base
is no longer an option (VB 6/VB .NET)</a></span><!--mstheme--></font></td>
<td width="50%" align="center" height="22"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
<a href="#Use the Val() function to strip unwanted characters from the end of numeric strings (VB 6/.NET)">
Use the Val() function to strip unwanted characters from the end of
numeric strings (VB 6/.NET)</a><!--mstheme--></font></td>
</tr>
<tr>
<td width="50%" align="center" height="22"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
<a href="#Publicly welcome some of your favorite objects into a class global Declarations (VB .NET)">
Publicly welcome some of your favorite objects into a class' global
Declarations (VB .NET)</a><!--mstheme--></font></td>
<td width="50%" align="center" height="22"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
<a href="#Write fewer lines of code when adding dynamic controls to a form using the AddRange() method (VB .NET)">
Write fewer lines of code when adding dynamic controls to a form using the
AddRange() method (VB .NET)</a><!--mstheme--></font></td>
</tr>
</table><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">
</center>
</div>
<p> </p>
<p><b><a name="A Try...Catch alternative to GoTo...Resume (VB 6/ VB .NET)">A Try...Catch alternative to GoTo...Resume (VB 6/ VB .NET)</a></b><br>
<br>
Believe it or not, the .NET framework still supports GoTo branches in your VB
.NET code. This means if you're upgrading code that contains a GoTo structure,
like the one below, VS .NET will port it over.<br>
<br>
Dim x As Double<br>
Dim y As Short<br>
On Error GoTo Oops<br>
<br>
x = 32780 : y = x<br>
MsgBox(y)<br>
Exit Sub<br>
<br>
Oops:<br>
MsgBox(Err.Description)<br>
x = 1<br>
Resume <br>
<br>
However, since the majority of these statements are used for error handling, you
should really consider manually converting them to Try...Catch statements. The
next set of code easily replaces the GoTo statement in the previous example.<br>
<br>
Dim x As Double = 32780<br>
Dim y As Short<br>
Dim tryagain As Boolean = True<br>
<br>
Do While tryagain = True<br>
Try<br>
y = x<br>
tryagain = False<br>
Catch Oops As Exception<br>
MsgBox(Oops.Message)<br>
x = 1<br>
End Try<br>
End While<br>
MsgBox(y)<br>
<br>
<br>
In the code, we use a Try...Catch block to catch the Overflow exception. We then
enclosed the Try statement in a Do loop to simulate the Resume statement. In a
real-life scenario, you'd also probably want include a Finally statement to
terminate the<br>
loop after so many tries.<br>
</p>
<p><b><a name="Beware of CInt; use CDbl instead (VB 6.0)">Beware of CInt; use CDbl instead (VB 6.0)</a></b><br>
<br>
If you have any program that converts string values to numbers, you'll want to
steer clear of the CInt function and use CDbl <br>
instead. The problem with CInt is that it only accepts numbers up to 32767. You
might say today, "My number will never get that <br>
large." However, later on down the line, it might. Unfortunately, by that time,
your code may be riddled with hundreds of CInt time bombs waiting to blow up,
displaying the dreaded Overflow error. Using the CDbl function is no different
from using CInt. The syntax is:<br>
<br>
CDBl(numericstring)<br>
<br>
CDbl returns the same value as CInt and it accepts num-bers up to
1.7976931348623158E+308.<br>
</p>
<p><b>
<a name="Call a function or subroutine with its string name using the Invoke() method (VB .NET)">Call a function or subroutine with its string name using the Invoke()
method (VB .NET)</a></b><br>
<br>
VB 6 introduced the CallByName() function, which allows you to call a function
or subroutine using the subroutine or function name stored as a string value.
While this function still works in VB .NET, you do have another option. You can
use the Invoke() method of the MethodInfo class. This class is a member of the
System.Reflection Namespace and it provides access to a method's metadata.
Consider the following subroutine:<br>
<br>
Public Sub CallMe(ByVal arg1 As String, ByVal arg2 As String)<br>
<br>
'do something<br>
<br>
End Sub<br>
<br>
To call this subroutine using the Invoke() method, you can use code similar to
the following:<br>
<br>
Dim SubName As String = "CallMe"<br>
<br>
Dim arguments() As String = {"Hello ", "world."}<br>
<br>
Dim PageType As Type = Me.GetType()<br>
Dim MyMethod As System.Reflection.MethodInfo = _<br>
PageType.GetMethod(SubName)<br>
MyMethod.Invoke(Me, arguments)<br>
<br>
The GetMethod() method obtains information about the function and the Invoke()
method calls it using the argument list. This is<br>
an excellent tool to help you streamline your code if you have several different
functions, but you won't know which one to call<br>
until runtime. For example, if you have a new, edit, and delete routine for
updating database records, instead of using an<br>
If..Then or Case statement to call the right method, just link the name of each
method to the user's requested action.<br>
<br>
<b>
<a name="Create a Universal Data Link file as an alternative to hard-coding connection strings (VB 6.0)">
Create a Universal Data Link file as an alternative to hard-coding connection
strings (VB 6.0)</a></b><br>
<br>
We've probably all had to hard-code connection string information at some point
or another. However, an easy and safer<br>
alternative is to use a Microsoft Universal Data Link file (UDL) to hold
connection string information. A UDL file is simply a<br>
text file that ADO reads to obtain the necessary connection information. And
these files are really easy to create. <br>
<br>
First, open Windows Explorer and select a location for the file. Once you've
opened the folder, position the mouse pointer in the file display pane.
Right-click and select New-Text Document from the pop-up menu. The operating
system creates a text document with the name New Text Document.txt. Rename this
file and change the .txt extension to .udl. The operating system may display a
warning message as a result of the extension change. Click the Yes button to
dismiss the warning and save the file.<br>
<br>
Once you've created the file, double-click on it to display the Data Link
Properties page, which is the standard Windows system<br>
interface for configuring connection strings. From this page, you'll be able to
select a provider, supply a Data Source Name (DSN) or connection string, and
identify authentication information, if necessary. It can also help you build a
connection string using a DSN. After supplying the necessary information, you
should test the connection. If the test is successful, click the OK button to
save the file and close it. To view the contents of the file, right-click on it
in the Explorer window and open it with Notepad. You should see something
similar to the following:<br>
<br>
[oledb]<br>
; Everything after this line is an <br>
;OLE DB initstring<br>
Provider=MSDASQL.1;Persist Security Info=False;<br>
User ID=sa;Data Source=Address_Book<br>
<br>
The contents of your file will, of course, depend on the<br>
provider and DSN you select. To access the file in VB, use code<br>
along the lines of:<br>
<br>
Dim cn As New ADODB.Connection<br>
<br>
With cn<br>
.ConnectionString = "File Name=C:\MyUDL.UDL"<br>
.CursorLocation = adUseClient<br>
.Open<br>
End With<br>
</p>
<p><b>
<a name="Def declarations are now defunct in the new framework (VB 6/VB .NET)">
Def
<type />declarations are now defunct in the new framework (VB 6/VB .NET) </a>
</b></p>
<p>Veterans of VB may remember Def statements as a quick way to declare a range
of junk variables. They include DefBool, DefByte, DefInt, DefLng, DefCur, DefSng,
DefDbl, DefDec, DefDate, DefStr, DefObj, and DefVar. We could use these
statements like so: DefDate A-C, H-I </p>
<p>When we place this statement in a VB 6 program, it treats the variables A, B,
C, H, and I as Date types. Unfortunately, VB .NET doesn't recognize such
statements. It requires that you explicitly declare each variable. </p>
<p><b>
<a name="Easily access the days of the week with the DayofWeek Enumeration (VB .NET)">
Easily access the days of the week with the DayofWeek Enumeration (VB .NET)</a></b><br>
<br>
Working with the days of the week in VB .NET is a simple task. That's
because the Framework has a built-in Enumeration type <br>
for the days of the week called DayOfWeek. To interate this Enum and return all
the week names, you can then call the GetNames method of the Enum type, like so:<br>
<br>
<br>
Dim WeekDay As String<br>
For Each WeekDay In [Enum].GetNames(GetType(DayOfWeek))<br>
MsgBox(WeekDay)<br>
Next<br>
<br>
If you just need to return the day of the week for a specific date, you can use
code similar to the following:<br>
<br>
Dim dt As New DateTime(2004, 6, 20)<br>
MsgBox(dt.DayOfWeek.ToString)<br>
</p>
<p><b>
<a name="How to ignore textbox keystrokes in VB 6 and VB .NET (VB 6/.NET)">How
to ignore textbox keystrokes in VB 6 and VB .NET (VB 6/.NET)</a></b><br>
<br>
Do you need to ignore a textbox keystroke? If so, you can easily accomplish this
task in both VB and VB .NET. In VB, place the<br>
following code in the KeyPress() event of the textbox:<br>
<br>
KeyAscii = 0<br>
<br>
In VB 6, KeyAscii is a parameter of the KeyPress() event and it contains the
Ascii value of the key the user just pressed. Setting the value to 0 in the
KeyPress() event nullifies the entry. </p>
<p><br>
In VB .NET, there isn't a KeyAscii event argument. So, to cancel the current
keystroke, you'll need to use the Handled property of the e event argument, like
so:<br>
<br>
e.Handled = True<br>
<br>
The Handled property tracks which events have been handled and which have not.
VB .NET will only pass on events that haven't<br>
been handled. Hence, when you set the Handled property to True in the KeyPress()
event, you're essentially telling .NET that the event has been handled and to
ignore it.<br>
</p>
<p><b>
<a name="Locate a variable in your project with the Definition menu option (VB IDE/Visual Studio .NET)">
Locate a variable in your project with the Definition menu option (VB IDE/Visual
Studio .NET)</a></b><br>
<br>
Have you ever been debugging an application and came across an unfamiliar
variable? Well, you can easily find out where you<br>
declared the variable by using the Definition menu option. To use this option,
select a variable in your code and right-click on<br>
it. Then, select Definition from the shortcut menu. VB will jump to the area in
your code where you declared the variable. To<br>
return to the line in your code where you selected the variable, right-click in
the declaration area and select Last Position from<br>
the shortcut menu. If VB can't find the variable, it will display an error:
Identifier under cursor is not recognized.<br>
<br>
You can also use this technique in Visual Studio .NET. However, it doesn't offer
the Last Position menu option to help you get<br>
back to the spot you left and it won't display an error if the variable
declaration doesn't exist.<br>
<br>
<span style="font-face: Courier New"><b>
<a name="Option Base is no longer an option (VB 6/VB .NET)">Option Base is no
longer an option (VB 6/VB .NET)</a></b><br>
<br>
Seasoned VB developers may recall the Option Base statement from the VB days of
old. It allowed you to set the default lower bound of an array to 0 or 1, like
so:<br>
<br>
Option Base 1<br>
<br>
In VB .NET, this statement is irrelevant because all arrays are inherently
zero-based. While .NET does provide support for<br>
non-zero-based arrays, the Option keyword isn't a part of the solution. When VB
.NET encounters the Option keyword, it expects to see one of the following
keywords after it: Compare, Explicit, or Strict.</span><span style="FONT-SIZE: 10pt; font-face: Courier New"><br>
</span></p>
<p><b>
<a name="Publicly welcome some of your favorite objects into a class global Declarations (VB .NET)">
Publicly welcome some of your favorite objects into a class' global Declarations
(VB .NET)</a></b><br>
<br>
In VB 6, if you try to publicly declare objects like constants or user-defined
types in class modules, you receive the error<br>
"Constants, fixed-length strings, arrays, user-defined types and Declare
statements not allowed as Public members of object<br>
modules." However, VB .NET lifted those restraints. In the following code
snippet, we crammed four of the five VB 6 no-no's<br>
into a class declarations:<br>
<br>
Public Class Class1<br>
Public Const balloons = "red"<br>
Public myArray() As String = _{"Element", "K", "Journals"}<br>
Public Declare Sub Sleep _ Lib "Kernel32" (ByVal<br>
dwMilliseconds As Integer)<br>
<br>
Public Structure myType<br>
Public sMonth As String<br>
Public sDay As Short<br>
Public sYear as Short<br>
End Structure<br>
End Class<br>
<br>
Fixed-length strings are the only thing you can't publicly declare and that's
because those types of declarations are no<br>
longer supported in VB .NET.<br>
<br>
<b><a name="Put your VB application into deep sleep (VB 6/.NET)">Put your VB
application into deep sleep (VB 6/.NET)</a></b><br>
<br>
There may be occasions when you want an application to wait for a specified
period of time without performing activity or wait loops. The Timer control can
help, but only up to 60 seconds. The Sleep Windows API function lets you put the
application to sleep. Enter the following declaration into a standard module:<br>
<br>
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)<br>
<br>
Then, simply call the function from your code, as in:<br>
<br>
Sleep 5000<br>
<br>
In this case, the 5000 milliseconds put the application to bed for 5 seconds.
You can specify your own period of time, as necessary.<br>
</p>
<p><b><a name="Restrict TextBox entries using the KeyPress() event (VB 6)">
Restrict TextBox entries using the KeyPress() event (VB 6)</a></b><br>
<br>
If you ever need to restrict a TextBox to only numeric or only alphanumeric
entries, you can do so using the KeyPress() event of<br>
the TextBox. This event subroutine has a single integer argument called KeyAscii.
The KeyAscii value identifies the key that the<br>
user just pressed. For example, the values 48-57 represent the numbers 0-9 and
the values 65-90 represent the capital letters A-Z.<br>
<br>
To cancel a key press, you'll want to set the KeyAscii value to 0. Therefore, to
restrict a TextBox to only capital letters, you<br>
could use the following code:<br>
<br>
If KeyAscii < 65 Or KeyAscii > 90 Then _<br>
KeyAscii = 0<br>
<br>
Some values have VB constants that can be used in place of the integer values.
For example, the next set of code accepts only<br>
numbers in the TextBox field:<br>
<br>
If KeyAscii < vbKey0 Or KeyAscii > VbKey9 _<br>
Then KeyAscii = 0<br>
<br>
For a full a list of valid ASCII values and their character equivalents, visit
the MSDN Web site at
<a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbceide/htm/appendix_8.asp">
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbceide/htm/appendix_8.asp</a><br>
</p>
<p><b><a name="The Val() function bug that wont go away (VB 6/.NET)">The Val()
function bug that won't go away (VB 6/.NET)</a></b><br>
<br>
Many programmers use VB's Val() function to convert user inputted strings into
numbers. This useful function returns zero (0) for strings that are meaningless
as numeric input. Unfortunately, Val() has a bug in its routines. Notably,
Val("6%") returns 6, Val("6.%") returns 6, Val("6.0%") returns 6, but
Val("6.1%") generates a Type Mismatch error!<br>
<br>
Believe it or not, this is still an issue in VB .NET. Except now both of the
following lines will fail: </p>
<p><br>
Val("6.0%") <br>
Val("6.1%")<br>
<br>
To avoid this error, try a routine that replaces the offending percentage sign
with some other character, like this:<br>
<br>
TheString = "15.7%"<br>
Mid(TheString, Instr(TheString, "%"),1) = "$"<br>
Debug.Print Val(TheString)<br>
<br>
For .NET developers, the following line of code will do the trick:<br>
<br>
Debug.WriteLine(Val(Replace(TheString, "%", "$")))</p>
<p><span style="font-face: Courier New"><b>
<a name="Three ways to lock down a form in Visual Basic (VB 6)">Three ways to
lock down a form in Visual Basic (VB 6)</a></b><br>
<br>
If you want your VB form to stay just the way you load it, you can easily lock
it into place using the form properties. For example, if you set the form
BorderStyle property to one of the following values, you can stop the user from
resizing the form:<br>
<br>
Value Description<br>
1 Fixed Single<br>
3 Fixed Dialog<br>
4 Fixed ToolWindow<br>
<br>
Also, if you want to keep users from minimizing the form, you can set the
MinButton property to False. Likewise, if you don�t<br>
want them to blow the form up to the full screen size, you can set the MaxButton
property to False. Finally, to lock the form at<br>
a certain position on the window, you can set the Moveable property to False.<br>
</span></p>
<p><b>
<a name="Understanding the difference between the form Load() and Activate() events (VB 6.0)">
Understanding the difference between the form Load() and Activate() events (VB
6.0)</a></b><br>
<br>
The form Load() event, as you probably know, fires as soon as VB encounters the
Form.Show() method or whenever your program executes with a form as the startup
object. The event fires only once during the lifetime of the form. This makes it
the perfect place to initialize variables or perform other setup tasks required
by your application. However, one thing you can't do in this event is use
control.SetFocus to set the focus to a control on your form. This is because,
until the form Activate() event fires, the actual form doesn't exist. The form
Activate() event, on the other hand, fires each time the form loads or you
return to the form from another form in the project. For example, the following
code hides one form to show another:<br>
<br>
Form1.Hide<br>
Form2.Show 1<br>
<br>
When you close Form2 and return to the first using Form1.Show, the Activate()
event will fire again, but the Load() event will not.<br>
</p>
<p><b>
<a name="Use Structure instead of Type to create user-defined types (VB .NET)">
Use Structure instead of Type to create user-defined types (VB .NET)</a></b><br>
<br>
In VB, you create a user-defined type using the following syntax:<br>
<br>
Private Type myNames<br>
sFirst As String<br>
sMiddle As String<br>
sLast As String<br>
End Type<br>
<br>
In VB .NET, you now use the Structure statement to create user-defined types,
like so:<br>
<br>
Public Structure myName<br>
Public sFirst As String<br>
Public sMiddle As String<br>
Public sLast As String<br>
End Structure<br>
<br>
Notice that we preceeded the .NET members with the word Public. That's because,
in .NET, type members can be Public (accessible) or Private (not accessible).
Private members won't show up in the Intellisense when you use the type, and
you'll receive an error if you try to access it.<br>
</p>
<p><b>
<a name="Use the current time value to create custom greetings for users (VB .NET)">
Use the current time value to create custom greetings for users (VB .NET)</a></b><br>
<br>
Is it morning, afternoon, or evening? .NET allows you to easily determine the
answer to this question with its built-in Hour()<br>
function. The Hour() function returns an integer between 0 and 23. So, if the
the return value is greater than 12 then it's<br>
afternoon. Assuming evening starts around 6 p.m., if the function returns 18 or
higher, it's evening. The following code uses the<br>
Hour() function to output a custom greeting:<br>
<br>
Dim ct As Date = Date.Now<br>
Dim greeting As String = "Good Morning"<br>
If CDbl(ct.Hour) >= 12 Then greeting = "Good_ Afternoon"<br>
If CDbl(ct.Hour) >= 18 Then greeting = "Good_ Evening"<br>
Debug.WriteLine(greeting)<br>
</p>
<p><b>
<a name="Use the Show() method to make one form the owner of another (VB 6)">Use
the Show() method to make one form the owner of another (VB 6)</a></b><br>
<br>
The Visual Basic 6 Form contains a property called StartupPosition. You use this
property to instruct VB where on the screen it should initially place the form
it's loading. One of the values you can assign this property is CenterOwner (1).
This value tells VB to load the new form in the center of the form that owns it.
In order to use this property, you must first designate one form as the owner of
another form. You do this using the Show() method. The syntax for this method is
as follows:<br>
<br>
NewForm.Show [ModalVal],[OwnerFormName]<br>
<br>
You'll use the OwnerFormName argument to identify the name of form that will own
the form being opened. For example, if your project contains two forms named
Form1 and Form2, you could designate Form1 as the owner of Form2 when you open
Form2, using code similar to the following:<br>
<br>
Form2.Show 0, Form1<br>
<br>
This code tells VB to show the second form non-modally and make the first form
its owner.<br>
</p>
<p><b>
<a name="Use the Val() function to strip unwanted characters from the end of numeric strings (VB 6/.NET)">
Use the Val() function to strip unwanted characters from the end of numeric
strings (VB 6/.NET)</a></b><br>
<br>
If you have a numeric string with some trailing characters, strip out the
characters in a single call with the Val() function. Val() evaluates a string
function to see if it can convert it into a number. For example, the following
statement would return the string "123456":<br>
<br>
MsgBox(CStr(Val("123456smd")))<br>
<br>
Notice that we specifically stated that the characters must be on the end of the
string. If the characters are at the beginning<br>
of the string (e.g., "smd123456"), then the Val() function will assume that the
string can't be converted to a number and will return 0.<br>
<br>
<b>
<a name="Write fewer lines of code when adding dynamic controls to a form using the AddRange() method (VB .NET)">
Write fewer lines of code when adding dynamic controls to a form using the
AddRange() method (VB .NET)</a></b><br>
<br>
Typically, when you add multiple controls to a form dynamically, you add each
control one by one, like so:<br>
<br>
<br>
Dim button1 As New System.Windows.Forms.Button<br>
Dim button2 As New System.Windows.Forms.Button<br>
Dim button3 As New System.Windows.Forms.Button<br>
<br>
Me.Controls.Add(button1)<br>
Me.Controls.Add(button2)<br>
Me.Controls.Add(button3)<br>
<br>
However, in VB .NET, you can save a few keystrokes by using the AddRange()
method. This method allows you to add an array of controls to the collection
instead of just one control at a time. The syntax for the AddRange() method is
as follows:<br>
<br>
AddRange(ObjectArray())<br>
<br>
where the ObjectArray() argument holds an array of Control objects. When you use
this method, you can add the same three<br>
Button objects to the Controls collection using the following line of code:<br>
<br>
Me.Controls.AddRange(New Control() {button1, button2, button3})<br>
</p>
<!--mstheme--></font></body>
</html>