Manipulating Forms with the Windows Forms Library

In this third part of a four-part series, you will learn how to use .NET's Windows Forms Library to make forms and controls interact with each other. This article is excerpted from chapter four of the Visual Basic 2005 Cookbook, written by Tim Patrick and John Clark Craig (O'Reilly; ISBN: 0596101775). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
February 22, 2007
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

4.10 Remembering a Form’s Position Between Uses

Problem

You would like the position of a form to be retained between exiting the application (or closing that form) and the next time you access that same form.

Solution

Sample code folder: Chapter 04\RememberFormPosition

Tie the form’sLocationproperty to a member of theMy.Settingsobject. You do this using the form’s application-setting property bindings.

Create a new Windows Forms application. Access the Project Properties window through the Project -> WindowsApplication1 Properties (or similar) menu command. Select the Settings tab in this window, as shown in Figure 4-5.


Figure 4-5.  The Settings tab of the Properties window

In the first row of the Settings grid, set the Name field toMainFormLocation, and selectSystem.Drawing.Pointin the Type field (Figure 4-6). Close the Project Properties window.


Figure 4-6.  The added MainFormLocation property

Back onForm1, expand its(ApplicationSettings)property. One of the subproperties should beLocation. Change its value toMainFormLocation.

The program is ready to use. Run it, and move the form to a conspicuous location. Then exit the program. When you run the program again, the form will be where you moved it.

Discussion

If, when you expand the (ApplicationSettings) property, you don’t see theLocationsubproperty, use the(PropertyBinding)subproperty instead. Click on the “...” button in its value area to display the “Application Settings for ‘Form1’” dialog. Locate theLocationentry in the form’s settings list, and set its value toMainFormLocation, as shown in Figure 4-7.


Figure 4-7.  The Application Settings dialog for Form1 

Any settings added to the Settings tab in the Project Properties window appear as members of theMy.Settingsobject. In this recipe’s case, you get a new property of typeSystem.Drawing.Pointwith the nameMy.Settings.MainFormLocation. You can access this property as needed in your code.

Another way to add a control-linked setting is to skip the trip to the Project Properties’ window’s Settings panel, and add the new setting directly from the control’s list of properties. When you select the(ApplicationSettings)property for the form or control and bring up the Application Settings dialog (Figure 4-7), if you click the drop-down button in the second column for any property, one of the choices that appears is “(new).” Clicking this link brings up the New Application Setting dialog, where you can enter the name and starting value of a new setting. The new property automatically obtains the right data type for the linked field. Figure 4-8 shows this method in action.


Figure 4-8.  Adding a new setting for the form's Location property 

4.11 Attaching a Control to the Edge of a Form

 Problem

You want a specific control, such as a toolbar, to always “stick” to one edge of the form, no matter how it is resized.

Solution

Use the control’s Dock property to permanently affix the control to the selected side or other “dock region.”

Discussion

Dock has six possible values:

None
  
The control performs no docking.

Top
   The control attaches itself to the top of the form’s
   client area and fills the entire width of the client area,
   if the control supports such resizing.

Bottom 
 
The control attaches itself to the bottom of the
    form’s client area and fills the entire width of the 
   client area, if the control supports such resizing.

Left
   The control attaches itself to the left edge of the
   form’s client area and fills the entire height of the
   client area, if the control supports such resizing.

Right 
 
The control attaches itself to the right edge of the
   form’s client area and fills the entire height of the
   client area, if the control supports such resizing.

Fill 
  
The control fills the entire client area of the form, if
    the control supports such resizing.

If multiple controls haveDocksettings other thanNone, they are attached to the form edges according to their z-order settings, starting from the back-most control. To alter the z-order of a control, right-click on the control in the Form Designer and select either “Bring to Front” or “Send to Back” from the shortcut menu. Figures 4-9 and 4-10 show a form with two controls with different z-orders docked to its bottom edge: aMonthCalendarcontrol (notice how it automatically fills the width of the form by adding months) and aStatusStrip control.


Figure 4-9.  The form when the calendar's z-order is in front

Some controls are designed to dock along a specific edge of the form’s client area. The most obvious example is theStatusStrip control, shown in this recipe’s figures, which is designed to dock along the bottom edge of the form. Other controls, such as theCheckBoxcontrol, really aren’t designed for docking. While you can still dock them, they may not look very nice.

Docking also applies to panels and other containers that can include subordinate controls. Figure 4-11 displays aPanelcontrol with an includedComboBox control that is docked along the top edge of the panel.

Attaching a Control to the Edge of a Form


Figure 4-10.  The form when the calendar's z-order is in back


Figure 4-11.  Docking within a container

See Also

Recipe 4.12 discusses the Anchor property, which can be used to attach a control to one, two, three, or four sides of the form. The Dock andAnchorproperties cannot be used at the same time on the same control. The last one you set on that control is the one used.

4.12 Moving or Resizing Controls as a Form Resizes

 Problem

You want a control to move or stretch in proportion to how the form is resized.

Solution

Use the control’s Anchor property to attach it to one or more sides of the form.

Discussion

Setting the Anchor property of a control tells that control to permanently maintain a consistent distance relationship with one or more sides of the form or container. You can anchor a control to any or all of the four sides of a form. By default, controls are anchored to the left and top sides of the form. This means that as the form resizes, the controls remain the same distance from the form’s left and top edges (i.e., they do not appear to move).

The availableAnchor property choices includeLeft,Top,Right, andBottom, and you can use them in any combination. The following list shows the types of combinations you can use with theAnchorproperty:

Anchored to one side

As the form is resized, the center point of the control along the anchored edge is matched to a position on that form edge relative to the changing size of the form. The size of the control does not change. For instance, if a control is anchored to the top of a form and the form is made wider, the control moves to the right in proportion to the size of the form, as shown in Figure 4-12.


Figure 4-12.  The top-anchored control moves when the form is resized

Anchored to two adjacent sides

As the form is resized, the control maintains its distance from both anchor sides. In other words, it seems to be joined to the corner that is shared by the two anchor sides. By default, most controls anchor to the left and top sides of the form and do not appear to move when the right and bottom borders of the form are moved in a resize operation.

Anchored to two opposite sides

The anchor sides of the control remain a fixed distance from the anchor borders. For instance, if a control is anchored on the left and right, the control grows by the same number of pixels as the form is widened (see Figure 4-13). When the unanchored direction is resized, the control is moved to keep the portion of space between the unanchored sides and the control the same, but the control is not resized in that direction.


Figure 4-13.  The left-and-right-anchored control stretches as the form widens

Anchored to three sides

The control resizes between the two anchor sides that are opposite to each other and remains the same distance from the single anchor border, as shown in Figure 4-14.


Figure 4-14.  Anchored to top, left, and right, the edges of the control remain a fixed distance from all but the bottom edge of the form

Anchored to all four sides

The control is continually resized with the form. All its sides stay the same distance from all anchored form borders, as shown in Figure 4-15.

Figure 4-15.  Anchored to top, left, bottom, and right, the proportions of the control change in concert with the form's proportions

Anchoring also applies to panels and other containers that can include subordinate controls.

See Also

See Recipe 4.11 for details on the Dock property, which you can use to attach a control to one side of a form’s client area. The Dock andAnchor properties cannot be used at the same time on the same control. The last one you set on that control is the one used.

4.13 Limiting the Sizing of a Form

Problem

You want the user to be able to resize a form, but within limits.

Solution

Use the MinimumSize and MaximumSizeproperties of the form to limit the user’s adjustments of the form’s size. As with the standardSize property, these two properties encapsulate separate width and height values. Figure 4-16 shows these settings in use in the Properties panel.


Figure 4-16.  MaximumSize and MinimumSize properties in use 

Discussion

These properties do limit the size of the form, whether the user is resizing the form directly or your code sets the Size property. You will usually want to set the form’s FormBorderStyleproperty toSizable, and you must set theMaximizeBox property toFalse(or in some other way hide the maximize box, such as by setting theControlBox property toFalse).

Please check back next week for the conclusion to this article.

blog comments powered by Disqus
VISUAL BASIC.NET ARTICLES

- Basic Form Properties and Modality in VB.NET
- Multiple Document Interfaces in Visual Basic
- Visual Basic for Beginners
- ASP.NET Image to PDF with VB.Net
- MySQL in ASP.NET: Mono using VB.NET
- AsyncFileUpload File Type and File Size Vali...
- Visual Studio: Adding Functionality and Style
- Clocks and Countdowns
- User-defined Functions using Visual Basic Ap...
- Understanding Object Binding in VBA
- Mastering the Message Box
- Testing a Windows Forms Application
- Using Visual Basic.NET Features to Code a Wi...
- Correcting Code in a Windows Forms Applicati...
- Write Readable Code and Comments for Windows...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials