Archive for June, 2012
Welcome to the VBDotNet Blog.
This blog covers a miscellany of Visual Basic .Net topics. The topics have been selected based on actual experience of problems encountered by Visual Basic .Net beginner and intermediate programmers.
Select a Category and topic from the list below, or make a selection using the tabs above.
1. Pulling Results from a Dialog form
A very common problem with forms is getting the result of a dialog form. Dialogs are used to ask a question or to get a response. If it’s a simple question with a Yes/No or OK answer, then the standard dialog form is quick and easy to create, and simple to use. But if the user must enter some value or select from an option, getting the information from the form requires extra code.
2. Passing Data to a New Form
For a Windows Forms application, creating a new form is as easy as Project / Add Windows Form / Windows Form. But what if you need to set this form up with some data that exists in the main form? How do you populate the controls and variables in the new form with the existing values from the main form?
3. Where did I come from?
Passing values between forms is one of the trickiest bits of a multi form application for new VB .Net programmers, but that’s partly because there are so many ways to handle it. When a form needs to know where it came from, there is a property of the form that can provide just that information, if it’s set up properly.
4. Pushing Results Back to the Main Form
Even if you identify the form that opened a subsidiary form, it’s not always easy to update controls on the form with new values. This snippet shows how it is done.
5. Messaging Between Forms
Messaging between forms is the most robust way of passing data back and forth between forms. It allows you to create forms that can be re-used with a minimum of issues about how you connect data on different forms.
1. Linked Control Properties
Have you ever wanted a control property, such as text on a label, to automatically update whenever a similar property on another control changes? With VB.Net you can do it the hard way – in code – or you can do it the easy way, in the designer.
2. Control Arrays
Programmers familiar with Visual Basic 6 are dismayed to find that the .Net designer doesn’t allow them to create arrays of controls – how can they get by without them? Well, VB .Net does support control arrays, and they are just as easy to use as they were in VB6.
3. Subclassing / Inheritance
Enhancing existing controls with new features and functions is easy with .Net.
4. Drag and Drop
Providing your users with Drag and Drop options will make your applications more user friendly.
1. Method Attributes
Adding attributes to your custom control methods provides full integration with Visual Studio features.
1. Access Resource by Name
Using the name of the resource is an effective way of getting to your resources, but the procedure to use is not obvious. Tis example shows how it’s done.
1. Simple Data Receiving
The serial port can be used for simple text receiving with just a few lines of code. This example shows how to implement a simple GPS data receiver.
2. Receiving Text with a Delegate
The serial port has some special requirements in .Net. This snippet shows how to implement a serial input text receiving routine using a delegate to enable updating of GUI objects.
3. Binary Data
Receiving binary data at the serial port is slightly different than receving text, because you must treat it as an array of bytes. This example shows how to do that, together with a simple way to display the data without a cross-thread call.
FILES AND FOLDERS
1. File Locations
Using the standard folders provided by the .Net framework can eliminate user access issues.
2. Reading / Writing text files
.Net makes it easy to read and write text files. All the hard work is done in the background, and the necessary processes are exposed as methods in the My.Computer.FileSystem namespace.
3. Random Access
Visual Basic .Net allows random access to files using the StreamReader class. The file pointer can be set as required, and data can be read or written as a stream of bytes or as a nominated variable type.
1. ByVal / ByRef
Understanding the implications of passing parameters by value or by reference is important in ensuring proper use of methods.
2. Using Static Variables
Static variables can solve many problems that otherwise look difficult, or may require a global variable.
3. Shared Variables and Methods
Shared variables or methods provide easy access to processes and values without the need to create an instance of the class.
1. Graphics Methodology
There are many ways to handle graphics tasks in VB .Net, but it’s not always obvious which approach is best for a particular task. Some different approaches are discussed here.
2. Color Picker
The GetPixel function returns the color of a pixel. This example shows how to use this function by getting the color of the pixel at the current cursor position – a Color Picker function. It also shows how to examine the image as painted on the screen.
1. Graphics – Scrolling List
The Scrolling List example shows how to draw formatted text to a bitmap and then scroll that bitmap vertically in a panel, a pixel at a time.
2. Graphics – Screen Updating
A clock is used to show how to draw the image background separately from the image components that need to be updated periodically. This process reduces the load on the system when the display is updated frequently.
3. User Input, Structures, Lists
The Grade Point Average example demonstrates the use of a simple structure for data management, user input validation, lists and listboxes, and a simple calculation.
4. Binary File Display
An example that reads the contents of a file and displays the contents in a formatted text box as hexadecimal and text.
5. Simple Text Printing
An example that shows how to create a component to simplify the printing process for any application that needs to print plain text.
6. Recursive Folder Search
Recursion is a simple and effective technique for searching in a heirarchy. This example shows how to access the folder heirarchy to list all files and folders. It can be used as a pattern for any similar recursive procedure.
7. Command-line Arguments
VB .Net provides easy access to the command that was used to start an application.
1. Object References
Do you know what your variables are really referring to?
2. ByVal and ByRef
Using ByVal and ByRef properly is a critical part of managing your variables.
3. Strings Are A Reference Type
It doesn’t always seem so, but the String type really is a reference type.
4. Option Strict
Setting Option Strict to On in your projects can save a lot of time and trouble in tracking down strange errors.
Not really a trap, but a method that is subject to a lot of misunderstanding, and which requires caution in its use.