|
A For Each loop that iterates in reverse order
|
Total Hit (11864) |
The For Each loop always iterates orderly on all the elements of an array or a collection (more precisely, on all the elements of a class that implements the IEnumerable interface). What happens if you need to iterate in reverse order, though? If you have an array or an ArrayList you can always refe
....Read More |
Rating
|
|
|
A For Each statement that visits items in random order
|
Total Hit (2784) |
At times you may need to process elements of an array, a collection, or a dictionary object in random order, for example when sampling a population or (more often) when writing a game, such as a card game. Here's a class that you can use in a For Each statement to randomly process all the elements o
....Read More |
Rating
|
|
|
Building arrays on the fly
|
Total Hit (2949) |
VB.NET supports the creation of arrays on-the-fly, by using the New operator. For example, look at the following code, that uses GDI methods to display few connected lines
«Code LangId=2»
' Get the Graphics object from the form.
Dim gr As Graphics = Me.CreateGraphics
' Draw three straight line
....Read More |
Rating
|
|
|
Create zero-elements arrays
|
Total Hit (2640) |
The .NET framework lets you create two types of "empty" arrays: unitialized arrays and arrays that are initialized with zero elements. Uninitialized arrays are actually array variables that are set to Nothing, whereas zero-element arrays are non-Nothing variables that point to arrays with zero eleme
....Read More |
Rating
|
|
|
Store bits and small integers efficiently in a BitVector32
|
Total Hit (3419) |
The System.Collections.Specialized.BitVector32 structure can be used to store up to 32 boolean values, or a set of small integers that can take up to 32 consecutive bits. The BitVector32 is similar to the BitArray class, in that it can hold boolean values that take only one bit each, yet it is more
....Read More |
Rating
|
|
|
Store large Boolean arrays in a BitArray object
|
Total Hit (5940) |
.NET Boolean values require 4 bytes each, as opposed to the 2 byte taken under previous VB versions. When creating very large Boolean arrays this extra memory impacts negatively on the application's performance. The .NET Framework offers a specialized class, the System.Collection.BitArray class, whi
....Read More |
Rating
|
|
|
Understanding passing arrays by reference
|
Total Hit (3622) |
.NET arrays are object types, thus an array variable is actually a pointer to the object where data is actually stored. For this reason, when you pass an array to a procedure the pointer is passed and the called procedure is always able to modify the elements of the array, regardless of whether the
....Read More |
Rating
|
|
|
Add a file to the list of recent documents
|
Total Hit (3000) |
To add a file to the "Recents" folder, you just need a single API function: SHAddToRecentDocs. This function takes two parameters. The first is SHARD_PATH if you want to refer to the file with its path string, while is SHARD_PIDL if you use its identifier. The second is the path string of the file,
....Read More |
Rating
|
|
|
Checking if a Floppy Drive is ready using FileSystemObject library
|
Total Hit (3509) |
The Microsoft Scripting Runtime Library offers a FileSystemObject and a Drive object that allow you to check if a Floppy drive is ready. Before writing the code you have to add this library to your project using the References dialog window. If the library isn't contained in the list, you can downlo
....Read More |
Rating
|
|
|
Copy a directory tree
|
Total Hit (3033) |
Creating a VB routine that copies a whole directory tree can be a time-consuming job, but fortunately you don't have to do it. In fact, you can quickly create an entire folder, including all its subfolders, using the CopyFolder method of the FileSystemObject object, that is exposed by the Microsoft
....Read More |
Rating
|
|
|
Counting characters in a file
|
Total Hit (3804) |
Ever needed to count how many characters of a given type are in a file? For instance, how many alphabetical characters, or digits, or spaces? You might read each line of the file using Line Input, but you will probably to load the whole file in memory using one single operation:
«Code LangId=1»
....Read More |
Rating
|
|
|
Delete a folder and all its subfolders
|
Total Hit (3420) |
The RmDir command can delete a directory only if it doesn't contain files or sub-directories. If the directory you want to delete does contain other files or, worse, subdirectories it seems that you are forced to use a recursive routine that does the job.
A simpler solution is offered by the Dele
....Read More |
Rating
|
|
|
Get the canonical name of a file
|
Total Hit (3501) |
In many cases you may need the canonical (or absolute) name of a file, for example when you need to compare two relative file names (relative to the current directory or drive, that is) and decide whether they point to the same or different files.
You can obtain the canonical path of a file usin
....Read More |
Rating
|
|
|
|
Load a text file in one operation
|
Total Hit (2904) |
The fastest way to read a text file is using the Input$ function, as shown in this reusable procedure:
«Code LangId=1»
Function FileText (filename$) As String
Dim handle As Integer
handle = FreeFile
Open filename$ For Input As #handle
FileText = Input$(LOF(handle), handle)
....Read More |
Rating
|
|
|
Retrieve information on all available drives
|
Total Hit (3133) |
You can retrieve information about all the available drives using calls to Windows API, if you like the hard way of doing things. A much simpler solution is offered by the Microsoft Scripting Runtime library, that exposes a Drive object that lets you get all those info by querying a property:
....Read More |
Rating
|
|
|
Search a file in a directory tree using the Imagehlp DLL
|
Total Hit (3749) |
You can search a file in all the subdirectories of a given drive in VB using a recursive routine based on the Dir$ function, the FileSystemObject component, or the FindFirstFile/FindNextFile API functions. There is a fourth way you might want to try out, based on the SearchTreeForFile function embed
....Read More |
Rating
|
|
|
Using the CreateDirectory and CreateDirectoryEx API functions
|
Total Hit (6983) |
The VB's MkDir function creates a directory in a specified path. If the directory already exists, MkDir raises error 75 (Path/file access error); yet, it raises the same error code if you attempt to create the directory on a read-only drive. Even worse, in Windows NT/2K/XP workstations, if you try t
....Read More |
Rating
|
|
|
Create a Touch utility
|
Total Hit (3284) |
The SetCreationTime, SetLastWriteTime, and SetLastAccessTime methods of the System.IO.Directory class let you modify the date attributes of a file or directory:
«Code LangId=2»
' Change the access date- and time of all files in C:\DOCS.
For Each fname In Directory.GetFiles("c:\docs")
File.
....Read More |
Rating
|
|
|
Create all the subdirectories for a new folder
|
Total Hit (3840) |
The System.IO.Directory class exposes a CreateDirectory method that, as its name suggests, can create a subdirectory. However, this method is even more powerful than it appears at a first glance, in that it can even create all the intermediate subdirectories, if necessary. In other words, the follow
....Read More |
Rating
|
|
|
Create directory paths
|
Total Hit (3889) |
Visual Basic 6's MkDir command, as well as the DOS MD command, can create only one subdirectory and fail if any subdirectory on the specified path doesn't exist:
«Code LangId=2»
' This VB6 statement fails if C:\MyApp doesn't exist
MKDIR "c:\MyApp\MyDir"
«/Code»
Conversely, the Directory.Crea
....Read More |
Rating
|
|
|
Display a directory tree
|
Total Hit (4039) |
Thanks to the GetDirectories and GetFiles methods of the System.IO.Directory class, you need very little code to iterate over all the directories and files of a directory tree. For example, the following code snippet prints the structure of a directory tree and (optionally) the name of files in each
....Read More |
Rating
|
|
|
Filter file names on their names or attributes
|
Total Hit (2849) |
The GetFiles and GetDirectories methods of the System.IO.Directory class can take an argument containing wildcards, to filter the result:
«Code LangId=2»
' Display all the *.txt files in C:\DOCS.
Dim fname As String
For Each fname In Directory.GetFiles("c:\docs", "*.txt")
Console.WriteLin
....Read More |
Rating
|
|
|
Reading text files
|
Total Hit (3975) |
In Visual Basic .NET you read a text file by first opening a StreamReader on it, and then iterating over all its lines until its Peek method returns -1:
«Code LangId=2»
Dim sr As New StreamReader("c:\autoexec.bat")
' Display all the text lines in the file.
Do Until sr.Peek = -1
' The Re
....Read More |
Rating
|
|
|
Retrieve file version information
|
Total Hit (3716) |
The System.Diagnostics.FileVersionInfo class lets you easily read file version information without having to call Windows API functions (as you have to do under previous VB versions). This class has a static GetVersionInfo method that returns a FileVersionInfo object related to a given file. Once yo
....Read More |
Rating
|
|
|
Add comments to End If and Loop statements
|
Total Hit (2429) |
Here's a little programming tip that will save you hours of headaches later.
Most of us already indent our Ifs, Selects, Do...Loops, etc., and that is good. But suppose you have some fairly complex code with several levels of indentation. Example:
«Code LangId=1»
If A= 0 and B=1 then
'
....Read More |
Rating
|
|
|
Convert a VB6 project back to VB5
|
Total Hit (2406) |
If you load a VB6 project file in the VB5 environment you get a warning, even though the project is correctly loaded. The cause of the warning is a VB6 attribute "Retained" (that is, the "Retained in memory" option in the Project Properties dialog box) that isn't recognized by VB5. If you want to ma
....Read More |
Rating
|
|
|
Copying menu controls from one form to another
|
Total Hit (2755) |
If you're building a project with multiple similar forms, it's nice to be able to cut-and-paste controls from one form to another. However, in VB6, you can't do this for menu controls - unless you get tricky. The trick is to make an end-run around the VB IDE. Warning: don't try this unless you have
....Read More |
Rating
|
|
|
Fast Copy of Properties between Controls
|
Total Hit (2409) |
You probably already know that if you select multiple controls on a form you can use the Property Window to change one of their common property in one single operation. However, here is a trick that is likely to be ignored by most programmer (included me, until recently).
If you want to copy a n
....Read More |
Rating
|
|
|
Logging under a different name makes your add-ins disappear
|
Total Hit (2206) |
After you install Visual Basic and log on to the machine as a different user (with or without administrator privileges), you cannot see any Add-Ins in the Add-In dialog in Visual Basic.
I ran across this problem when I logged in and installed VB6 on NT Workstation under my User, for a developer
....Read More |
Rating
|
|