Visual Basic 2008 Code Examples Pdf

The MultipleForms Example – Forms in Visual basic 2008 It’s time to write an application that puts together the most important topics discussed in this section. The MultipleForms example consists of a main form, an auxiliary form, and a dialog box.

Visual Basic 6 is a third-generation event-driven programming language first released by Microsoft in 1991. In VB 6, there is no limit of what applications you could create, the sky is the limit. You can develop educational apps, financial apps, games, multimedia apps, animations, database applications and more.

  1. Computer Fundamentals and Information Technology (Series 1 – With Simple Visual Basic 2008 Jumpstart. Download Free PDF. Download PDF Package. Premium PDF Package. Download Full PDF Package.
  2. Visual Studio 2008 overview.pptx. Date Published: 5/14/2008. File Size: 6.2 MB. Visual Studio 2008; System Requirements Supported Operating Systems: Windows Server 2003, Windows Server 2003 Itanium-based editions, Windows Server 2003 R2 (32-Bit x86), Windows Server 2003 R2 Datacenter Edition (32-Bit x86), Windows Server 2003 R2 Datacenter x64.

The team at vbtutor.net has created many sample codes, please browse them on the sidebar. You are welcome to use the sample codes as a reference for your assignments and projects. However, the usage of the sample codes for commercial purposes without prior consent from the webmaster is strictly prohibited.


Visual Basic Sample Codes E-Book is written by our webmaster, Dr.Liew. It comprises 258 pages of captivating contents and 48 fascinating Sample Codes.Perfect source of reference for your VB projects. Check it out.

About Us

The Tutor and webmaster of Vbtutor.net, Dr.Liew Voon Kiong , holds a Bachelor's Degree in Mathematics, a Master's Degree in Management and a Doctoral Degree in Business Administration. He obtained the DBA degree from the University of South Australia.

He has been involved in programming for more than years. He created the popular online Visual Basic Tutorial in 1996 and since then the web site has attracted millions of visitors .It is the top-ranked Visual Basic tutorial website in many search engines including Google. Besides that, he has also written a few Visual Basic related books. One of the books, Visual Basic 6 Made Easy was published by Creativespace.com, an Amazon.com publisher.


The Slot Machine created using VB6



Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy

Using the My object, you can write some text to a file via a single statement. The WriteAllText method accepts as arguments a path and the string to be written to the file (as well as a third optional argument that determines whether the text will be appended to the file or will replace the current contents), writes some text to the file (the contents of a TextBox control in the following sample), and then closes the file:

If the specified file does not exist, the write method creates it. To write binary data to a file, use the WriteAllBytes method, whose syntax is almost identical, but the second argument is an array of bytes instead of a string.

By the way, because My is not a class, you can’t import it to a file and shorten the statements that access its members; you have to fully qualify the member names. You can still use the With statement, as shown here:

Visual Basic 2008 Code Examples Pdf

To read back the data saved with the WriteAllText and WriteAllBytes methods, use the ReadAllText and ReadAllBytes methods, respectively. The ReadAllText method accepts as an argument the path of a file and returns its contents as a string. ReadAllBytes accepts the same argument, but returns the file’s contents as an array of bytes. This is all you need to know in order to save data to disk files between sessions with the My object. The following code segment saves the contents of the TextBox1 control to a user-specified file, clears the control, reads the text from the same file, and populates the TextBox1 control:

As you can see, it takes two statements to send the data to the file and read it back. All other statements set up the Open and Save As dialog boxes.

Here’s another example of using the FileSystem object. To delete a folder, call the DeleteDirectory method of the My.Computer.FileSystem component, which accepts three arguments: the name of the folder to be deleted, a constant that specifies whether the DeleteDirectory method should delete the contents of the specified folder if the folder isn’t empty, and another constant that determines whether the folder will be deleted permanently or moved to the Recycle Bin. This constant is a member of the FileIO.RecycleOption enumeration: DeletePermanently (to remove the file permanently from the file system) and SendToRecycleBin (moves the file to the Recycle Bin). To delete a file, use the DeleteFile method, which has the same syntax. (The first argument is the path of a file, not a folder.)

Another interesting member of the FileSystem object is the SpecialDirectories property, which allows you to access the special folders on the target computer (folders such as My Documents, the Desktop, the Program Files folder, and so on). Just enter the name of the SpecialDirectories property followed by a period to see the names of the special folders in the IntelliSense box. To find out the application’s current folder, call the CurrentDirectory method. The RenameDirectory and RenameFile methods allow you to rename folders and files, respectively. Both methods accept as arguments the original folder name or filename and the new name, and perform the operation. They do not return a value to indicate whether the operation was successful, but they throw an exception if the operation fails.

The CopyFile and CopyDirectory methods copy a single file and an entire folder, respectively. They accept as arguments the path of the file or folder to be copied, the destination path, and an argument that determines which dialog boxes will be displayed during the copying operation. The value of this argument is a member of the FileIO.UIOption enumeration: AllDialogs (shows the progress dialog box and any error dialog boxes) and OnlyErrorDialogs (shows only error dialog boxes). The following code segment copies a fairly large folder. It’s interesting to see how it displays the usual file copy animation and prompts users every time it can’t copy a folder (because the user doesn’t have adequate privileges or because a file is locked, and so on).

Please do change the destination drive (E: in the preceding sample code segment); you may not have an E: drive, or you may overwrite a working installation of Visual Studio 2008. Notice that I used the GetName method of the FileSystem component to extract the last part of the path and then combine it with the new drive name. The last argument of the CopyDirectory method, which is a member of the UICancelOption enumeration: DoNothing or ThrowException, determines how the method reacts when the user clicks the Cancel button on the copy animation. I used the ThrowException member and embedded the entire statement in an exception handler. If you click the Cancel button while the folder’s files are being copied, the following message will appear:

Cancelling a copy operation doesn’t reset the destination folder. You must insert some additional code to remove the files that have been copied to the destination folder, or notify the user that some files have copied already and they’re not automatically removed.

To manipulate folders, use the CreateDirectory and DirectoryExists methods, which accept as an argument the path of a folder. To find out whether a specific file exists, call the FileExists method, passing the file’s path as the argument.

To retrieve information about drives, folders, and files, use the GetDriveInfo, GetDirectoryInfo, and GetFileInfo methods, respectively. These methods accept as an argument the name of the drive or the path to a folder/file, respectively, and return the relevant information as an object. Drive properties are described with the IO.DriveInfo class, folder properties are described with the IO.DirectoryInfo class, and file properties with the IO.FileInfo class. These objects are part of the Framework’s IO namespace and they provide properties such as a directory’s path and attributes, a file’s path, size, creation and last modification date, and so on. The three objects are described in detail later in this chapter, in the discussion of the IO namespace. To find out the properties of the C: drive on your system, execute a statement such as the following:

This statement produced the following output on my system:

To retrieve information about all drives in your system, call the Drives method, which returns a read-only collection of DriveInfo objects. If you want to search a folder for specific files, use the FindInFiles method, which is quite flexible. The FindInFiles method goes through all files in a specified folder and selects files by a wildcard specification, or by a string in their contents. The method has two overloaded forms; their syntax is the following:

and

Both methods return the list of matching files as a read-only collection of strings. The dir argument is the folder to be searched, and the containsText argument is the string we want to locate in the files. The ignoreCase argument is a True/False value that determines whether the search is case-sensitive, and the SearchOption argument is a member of the FileIO.SearchOption enumeration and specifies whether the method will search in the specified folder or will include the subfolders as well: SearchAllSubdirectories, SearchTopLevelOnly. The second overloaded form of the method accepts an additional argument, which is an array of strings with the patterns to be matched (for example, *.txt, Sales*.doc, *.xls, and so on). The following statements locate all text, .doc, and .xml files in the Program Files folder that contain the string Visual Basic. The search is case-insensitive and includes the all subfolders under Program Files.

Visual Basic Codes Pdf

A Simpler Method of Saving Data to Files

The Framework provides an attractive alternative to writing data to files: the serialization mechanism. You can create collections of objects and persist them to a file via a few simple statements. Actually, it’s much simpler to create a collection of customer/product/sales data and persist it as a whole, than to write code to write every field to a file (let’s not forget the code for reading the data back into the application). Serialization is a major component of .NET, and it’s discussed in detail in Chapter, “XML and Object Serialization.”

Visual Basic Source Code Examples

This concludes the overview of the file-related methods of the FileSystem component. This component doesn’t expose many members, and their syntax is quite simple. You can experiment with the methods and properties of the FileSystem component to get a better idea of the type of operations you can perform with it. In the remainder of this chapter, you’ll find a detailed discussion of the IO namespace.

Visual Basic 2008 Pdf

Related lessons: