Thursday, March 8, 2012

Visual Basic Power Pack

Starting in 2005 Microsoft released a Power Pack which gives VB.NET access to many great components that VB6 users where used to such as: Shapes, DataReader, PrintForm, etc. This tutorial gives some examples of these controls and how you can make use of them when developing great Visual Basic .Net applications.

The Microsoft Visual Basic Power Pack 3.0 component adds back to VB (versions 2005 and higher) a set of controls that were available in pre-.NET versions of VB (i.e. VB6) but removed in the first versions of VB.NET (2002 / 2003). Included in the power pack are the basic shape controls (line, oval, and rectangle), a PrintForm component, and the DataRepeater control. The shape controls enable you to enhance your form's layout by simply dropping one of these controls on the form (in earlier versions of .NET you would have to use the System.Drawing class and write code to draw a simple shape on your form). The PrintForm component enables you to easily print an image of your form. The DataRepeater control would be used in a database application to display rows of data in a scrollable container.

The PrintForm component will be demonstrated in the tutorial on forms and the shape controls will be used in a number of upcoming tutorials and sample applications.

To get the power pack, go to this web page (a screen-shot of the page is shown below):

http://msdn.microsoft.com/en-us/vbasic/bb735936.aspx

image

Once you download the power pack from the page shown above, install it as you would any other software. At that point you will then be ready to add the controls to your VB toolbox. The information given below shows how you would incorporate the power pack controls into your VB toolbox using VB 2005 Express Edition. The way to do this using other editions may vary slightly, but the concept is the same.

First, open up VB, right-click in an empty area of the toolbox, and select Choose Items ... from the context menu:

image

The Choose Toolbox Items dialog box will appear. Click the Browse button:

image

A standard Open dialog box will appear. Navigate to Program Files\Microsoft Visual Basic 2005 Power Packs\3.0 and double-click on Microsoft.VisualBasic.PowerPacks.dll.

image

The Open dialog box will close. Back on the Choose Toolbox Items dialog, click on the Namespace column to sort the components by Namespace, and scroll down until you see the Microsoft.VisualBasic.PowerPacks items. Check the items using the checkboxes on the left and click OK.

image

At this point the controls will be added to the toolbox:

image

You can then add the controls to your form as you would any other control:

image

Shape Control Bug

There is a bug, acknowledged by Microsoft, with the shape controls. The bug has to do with the Name property. If you are using the shape controls purely as GUI elements and will NOT be referring to the control in code, then this issue is of no concern. However, if you will be referring to the control in code (as is done in the Tic-Tac-Toe application, for example), then you need to be aware of the bug and how to fix it.

In the screen above, the shape controls were named myLine, MyRec, and myOval. However, the form designer may "miss" a control or two, and internally retain the default name (like "OvalShape1") even though you see the name you gave the control (myOval in this case) on the property sheet. If you refer to one of the misnamed controls in your code, a run-time error will most likely occur. To ensure the designer "got them all right", you can click the "Show All Files" button on the Solution Explorer, and then open the ".Designer.vb" file of your form.  As you can see in the screen-shot below, "myLine" and "MyRec" are fine, but "myOval" is wrong – the designer did not set the Name property to my new name. The remedy here is, on the circled line, change "OvalShape1" to "myOval".

image

After you have made any necessary corrections, you may want to toggle "Show All Files" back off as a safeguard against inadvertently modifying the designer files and other files that you would normally not edit. (Making the correction shown here is an "exception to the rule" – you do not normally want to modify the form's Designer.vb file.)

This article was originally written by The VB Programmer.


View the original article here

No comments:

Post a Comment