Professional VB 2005 with .NET 3.0 (Programmer to Programmer)

If you have already used Windows Forms 1.0 or 1.1, much of the material in this chapter will be familiar to you. To help you quickly focus on the new capabilities in Windows Forms 2.0, here is a summary of changes and additions.

Changes in Existing Controls

Changes to existing controls in Windows Forms 2.0 are minor, but are helpful additions of functionality. Some changes apply to all controls because they are part of the base Control class in Windows Forms 2.0. Other changes apply only to specific controls.

Smart Tags

Many controls in Windows Forms 2.0 display a small triangle or glyph toward the top, right side of the control when highlighted in the visual designer. This is a new feature called a smart tag. If you click this glyph, you get a pop-up dialog with common operations for the control. Many of the features that you access through the property box can also be accessed through the smart tag. In general, this chapter uses the Properties window for manipulation of properties, but you should be aware of the smart tag shortcut for commonly used features.

AutoCompletion in Text Boxes and Combo Boxes

Text boxes and combo boxes get new properties for autocompletion of text entries. This capability could be added manually or with third-party controls in previous versions, but is now built in. The AutoCompleteMode property controls how autocompletion works in the control, while the AutoCompleteSource and AutoCompleteCustomSource properties tell the control where to get entries for autocompletion.

An example of autocompletion in action is shown later, in the section entitled “Advanced Capabilities for Data Entry.”

New Properties for All Controls

The base Control class, which is a base class for all Windows Forms controls, has several new properties in Windows Forms 2.0. Because all controls inherit from this class, all Windows Forms controls gain these new properties and the new functionality that goes along with them.

Two of the new properties, Padding and Margin, are most useful when used in conjunction with some new controls, TableLayoutPanel and FlowLayoutPanel. Those two properties are discussed later in the chapter. The other new properties are discussed here. They include MaximumSize, MinimumSize, and UseWaitCursor.

MaximumSize and MinimumSize Properties

The MaximumSize and MinimumSize properties specify the maximum and minimum height and width of a control. Forms had these properties in Windows Forms 1.0 and 1.1, but now all controls have them.

If the maximum height and width are both set to the default value of 0, then there is no maximum. Similarly, if the minimum height and width are set to zero, then there is no minimum. The form or control can be any size.

If these properties are set to anything else, the settings become limits on the size of the control. For example, if the MaximumSize height and width are both set to 100, then the control cannot be bigger than 100×100 pixels. The visual designer will not make the control any larger on the form design surface. Attempting to set the height or width of the control in code at runtime to a value greater than 100 will cause it to be set to 100 instead.

The MaximumSize and MinimumSize properties can be reset at runtime to enable sizing of the controls outside the limits imposed at design time. However, the properties have a return type of Size, so resetting either property requires creating a Size structure. For example, you can reset the MinimumSize property for a button named Button1 with the following line of code:

Button1.MinimumSize = New Size(20, 20)

This sets the new minimum width and height to 20 pixels.

The Size structure has members for Height and Width. Those can be used to fetch the current minimum or maximum sizes for either height or width. For example, to find the current minimum height for Button1, the following line of code would be used:

Dim n As Integer = Button1.MinimumSize.Height

UseWaitCursor Property

Windows Forms interfaces can make use of threading or asynchronous requests to allow tasks to execute in the background. When a control is waiting for some asynchronous request to finish, it is helpful to indicate that to the user by changing the mouse cursor when the mouse is inside the control. Normally, the cursor used is the familiar hourglass, which is called the WaitCursor in Windows Forms.

For any control, setting the UseWaitCursor property to True causes the cursor to change to the hourglass (or whatever is being used for the WaitCursor) while the mouse is positioned inside the control. This allows a control to visually indicate that it is waiting for something. The typical usage is to set UseWaitCursor to True when an asynchronous process is begun and then set it back to False when the process is finished and the control is ready for normal operation again.

New Controls

Windows Forms 2.0 includes a number of new controls. Some are brand-new controls that offer completely new functionality. Others are replacements for existing controls, offering additional functionality.

WebBrowser Control

Even smart client applications often need to display HTML or browse websites. Windows Forms 1.0 or 1.1 did not include a true Windows Forms control for browsing. The legacy ActiveX browsing control built into Windows could be used via interoperability, but this had drawbacks for deployment and versioning.

The legacy ActiveX control is still the ultimate foundation for browsing capability, but Windows Forms 2.0 includes an intelligent Windows Forms wrapper that makes it much easier to use and deploy the control.

MaskedTextbox control

Windows Forms 1.0 offered replacements for almost all of the controls available in Visual Basic 6, but one notable exception was the MaskedEdit control. In Windows Forms 1.0 and 1.1, masked edit capabilities were available only through third-party controls or by doing your own custom development.

That omission has now been rectified. The MaskedTextbox control resembles the old MaskedEdit control in functionality. It allows a mask for input and a variety of useful properties to control user interaction with the control. More information on this control is available in the section “Advanced Capabilities for Data Entry.”

TableLayoutPanel and FlowLayoutPanel Controls

Browser-based user interfaces are good at dynamically arranging controls at runtime, because browser windows can be different sizes for different users. Forms-based interfaces have traditionally lacked such capabilities. Dynamic positioning can be done in forms, but it requires writing a lot of sizing and positioning logic.

Two new controls in Windows Forms 2.0 mimic layout capabilities in a browser, offering better options for dynamic positioning of controls: FlowLayoutPanel and the TableLayoutPanel. Both are containers than can automatically reposition controls that are placed in them, based on the current space available in the container.

An example illustrating usage of both controls is included in the section “Dynamic Sizing and Positioning of Controls.”

Replacements for Older Windows Forms Controls

The Toolbar, MainMenu, ContextMenu, and StatusBar controls in Windows Forms 1.0 and 1.1 offered basic functionality, and these controls are still available in Windows Forms 2.0, but in most cases you won’t want to use these controls because there are new replacements with significantly enhanced capabilities. Because the old versions are still available, the new versions have different names. The table that follows summarizes these replacements:

Open table as spreadsheet

Old Control

New Control

Most Important New Capabilities

Toolbar

ToolStrip

Allows many new types of controls on the toolbar.

Supports rafting, which enables the toolbar to be detached by the user and float over the application. Allows users to add or remove buttons or other toolb ar elements.

Includes new cosmetics, allowing toolbars to look like those in Office 2003.

MainMenu

MenuStrip

Both new menu controls inherit from ToolStrip, which allows new cosmetics and more flexible placement.

ContextMenu

ContextMenuStrip

 

StatusBar

StatusStrip

Inherits from ToolStrip, which allows new cosmetics and makes it easier to embed other controls in a status bar.

Splitter

SplitContainer

Less difficult to set up

The old versions no longer show up by default in the Toolbox. If you want to use them in new projects, you must add them to the Toolbox by right-clicking on the Windows Forms Toolbox tab, selecting Choose Items, and placing a check mark on the older control that you want added to the Toolbox. However, you’ll probably only want to use the older controls for compatibility with older projects, using the improved versions for new development.

These controls are covered in more detail, including examples, in the sections “Toolbars and the New ToolStrip Control,” “Menus,” and “Dynamic Sizing and Positioning of Controls.”

Default Instances of Forms

In VB6 and earlier, a form named Form1 could be shown by merely including the following line:

Form1.Show

This capability was not available in Visual Basic 2002 and 2003. Instead, a form was treated like any other class, and had to be instantiated before use. Typical code to show a form in Windows Forms 1.0 and 1.1 looked like this:

Dim f As New Form1() f.Show

This technique is still recommended because it fits object-oriented conventions. However, the first form returns to Visual Basic 2005, with the minor change of parentheses at the end of the call:

Form1.Show()

Showing a form without instancing it, as in the first form above, is referred to as using the default instance of the form. That default instance is available from anywhere in a project containing a form. There is only one default instance, and any reference to it will bring up the same underlying instance of the form.

Another way to get to the default instance of a form is through the new My namespace. The following line has exactly the same effect of showing the default instance of a form:

My.Forms.Form1.Show()

Категории