(Print this page)

Microsoft Visual Basic Power Packs 3.0
Published date: Wednesday, March 26, 2008
On: Moer and Éric Moreau's web site

The latest version (3.0) now includes a DataRepeater control. The previous version was containing the Line and Shape controls, the PrintForm component and the Printer Compatibility Library.

This article will introduce you to the DataRepeater control, the Line and Shape controls and the PrintForm component. You can use the controls and the components of the Power Pack in any of your .Net Framework 2.0 (or better) Windows application.

Installation

The installation is quick and easy. Close your Visual Studio instances and run the 2.7 MB file downloaded from here.

It might also be a good thing for you to read the very short readme file as the team list the known issues there.

Unless you use the Express edition of Visual Studio (see the readme file for more details), the installation should automatically add a new group into your toolbox.

Figure 1: New items added to your toolbox

I had a small problem installing it on a PC that has both VS2005 and VS2008: I can’t get the help about the VB Power Packs in VS2005 but I have no problems in VS2008.

The Line and Shape controls

The Line and Shape controls are a set of 3 graphical controls that enable you to draw lines, ovals, and rectangles on Windows forms.

When you look at the properties of the LineShape control, it is quite simple. The coordinates of the line are provided using 4 properties (X1, Y1, X2, Y2). You can also set the BorderStyle (solid, dot, …) and the BorderWidth. It even supports a Click and a DoubleClick event.

The OvalShape control and RectangleShape control are much more complete providing all the Fill properties (FillColor, FillGradientColor, FillGradientStyle, and FillStyle) that let you display a colorful and/or textured shape. The border can also be set separately. The only thing that you might find missing is a Text property to let you set a caption and use this control as a button! Both of them also support the Click and the DoubleClick events.

The PrintForm component

The PrintForm component lets you quickly and easily prints a form. You can use it whenever you want to be able to take a capture of your form.

One of its most important properties is called PrintAction which is used to set your destination and can get 3 values.

The first value is PrintToPrinter and I hope you can guess where the output will go. The nice thing is that before calling the Print method, you can set a couple of properties like the printer name to let you print to a printer that is not the default one:

'Print to a specific printer
.PrintAction = Printing.PrintAction.PrintToPrinter
.PrinterSettings.PrinterName = "FGL Canon iR5070 PCL"
The second value is PrintToFile. This time, the important property to set is called PrintFileName and must contain the full path and the file name in which you want the output to be saved to. One big deception (at least for me), the only available format is Encapsulated PostScript (*.eps). Here is an example of code:
'Print to a file
.PrintAction = Printing.PrintAction.PrintToFile
.PrintFileName = "test.eps"
The last (but not least) value for the PrintAction property is PrintToPreview. When you set this action, a modal viewer opens with your preview in it (from where you can zoom and print).
'Print Preview
.PrintAction = Printing.PrintAction.PrintToPreview
Once that you have set the destination of your report, you are ready to start the process. The Print method has 2 overloads. The first one has no arguments and prints the currently visible client area of the current form. The second overload let you specify a form and a print option. This argument is an enumeration of 5 values but 3 are really interesting:
  • ClientAreaOnly which is the default value prints the inside of the form.
  • FullWindow prints the complete form including its borders, title bar, and scroll bar (if any).
  • Scrollable prints the full client area (by opposition to only the currently visible part).

Here is the code that starts the print process:

.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.FullWindow)
Any control that is not visible is not rendered on the output. This is useful if you want to hide some controls. You simply need to set them to invisible, use the Print method and set them visible back again.

The DataRepeater control

The DataRepeater control is a scrollable container that repeats a standard Windows Forms controls much like a grid.

If you ever used that control in the VB6 era, you surely remember that you need to create an ActiveX control (.ocx) and that control was repeated. That was sometime a pain!

In the .Net version of the DataRepeater control, you can still do it but you don’t have to. You can simply drag controls from the toolbox into the first row at the top and they will automatically be repeated. Isn’t it nice?

There is a really nice demo on the VB Power Packs web site displaying the weather but as it is often the case with Microsoft demo, they bind it to a data source. The example I am showing you here uses an in-memory dataset (the same you have seen in many other of my examples).

Figure 2: The demo application

For you to reproduce this example, you need to follow a couple of steps.

The first thing to do is to create the GUI. So drop a DataRepeater control from the toolbox to your form and resize it correctly. On the first line of the DataRepeater control, drop the controls you need (in my case, I have a label and 2 textboxes named lblID, txtCode, and txtDescription). Resize the row of the DataRepeater otherwise your row will be very large.

You now need to switch to the code view.

If you look at the Load event of the form of my demo application, you will see 3 main blocks. The first one is a call to a method that creates the dataset. The second block binds the controls of the item template of the DataRepeater control to fields of the dataset using this code:

lblID.DataBindings.Add(New Binding("Text", mdsData.Tables("States"), "ID"))
txtCode.DataBindings.Add(New Binding("Text", mdsData.Tables("States"), "Code"))
txtDescription.DataBindings.Add(New Binding("Text", mdsData.Tables("States"), "Description"))
In plain English, a binding line says something like: bind the Text property of the lblID control to a ID field in the States table.

We are now ready for the last block which is setting some properties of the DataRepeater control. Probably its most important is the DataSource. We have bound controls to fields but we never provided the data to the DataRepeater control. Here is how it is done:

DataRepeater1.DataSource = mdsData.Tables("States")
Of course the control can do a lot more. An event you should explore if you need to do specific process when displaying data is the DrawItem event. The demo application from the VB Power Packs website uses this event to put a picture (rain, sun, cloud, …) for the forecast depending on a value stored in a field.

If you are using this control to display a lot of data, you better spend time exploring the VirtualMode property and how it works. It can really help you improve performance.

The suggestion center

The team that is developing the Power Packs is ready to hear your how they can make your like easier. The channel they have opened for that is the Visual Basic Power Pack Suggestion Center , a web site dedicated to that purpose.

Other Q&A

Are those controls working in a C# application?

The answer is … of course yes. Those controls only have VB in their name because it is the VB Team that created them.

What about the deployment?

You need to deploy 2 additional DLLs: Microsoft.VisualBasic.dll and Microsoft.VisualBasic.PowerPacks.dll.

I can understand for the newly released DataRepeater control but why other controls have not been bundled into Visual Studio 2008?

Actually the PrintForm component has been included into Visual Studio 2008. The other controls were not ready when the cut-off was made. They will probably be included into the next version. The advantage for us developers is that we don’t have to wait too long before having good controls like those. Through the Team forums, we can also suggest new controls and provide input on existing ones.

Conclusion

The more tools we have in our toolbox, the better we are (at least we should be better). Here are just some new controls for you to start using today. Even if “VisualBasic” is in their namespace, C# developers shouldn’t be afraid of using them.


(Print this page)