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.

FORMS

1. Pulling Results from a Dialog formA 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 FormFor 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 FormEven 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 FormsMessaging 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.

CONTROLS

1. Linked Control PropertiesHave 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 ArraysProgrammers 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.

1. Access Resource by NameUsing 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.

SERIAL PORT

1. Simple Data ReceivingThe 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 DelegateThe 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 DataReceiving 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.

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 AccessVisual 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.

METHODS

1. ByVal / ByRefUnderstanding the implications of passing parameters by value or by reference is important in ensuring proper use of methods.

2. Using Static VariablesStatic variables can solve many problems that otherwise look difficult, or may require a global variable.

3. Shared Variables and MethodsShared variables or methods provide easy access to processes and values without the need to create an instance of the class.

GRAPHICS

1. Graphics MethodologyThere 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 PickerThe 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.

EXAMPLES

1. Graphics – Scrolling ListThe 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 UpdatingA 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 DisplayAn example that reads the contents of a file and displays the contents in a formatted text box as hexadecimal and text.

5. Simple Text PrintingAn 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 SearchRecursion 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.