Flylib.com

Books Software

 
 
 

Recipe 4.8. Making a Form the Top-Most Form


Recipe 4.8. Making a Form the Top-Most Form

Problem

You want a specific form to appear on top of all other forms in your application, no matter which form is selected.

Solution

If you wish to have a Toolbox-type form that is accessible at the same time as other forms but always remains on top, set the form's TopMost property to TRue .

Discussion

If you also want to disable access to all other forms, open the important form of the moment using its ShowDialog() method:

Form1.ShowDialog()

No other forms already displayed by the application will be available until the ShowDialog() form closes .



Recipe 4.9. Indicating the Accept and Cancel Buttons on a Form

Problem

On a form, you want to have the Enter key trigger a specific button (such as an "OK" button) and have the Escape key trigger another button (such as a "Cancel" button).

Solution

Use the form's AcceptButton and CancelButton properties to assign the appropriate buttons. In the Visual Studio Form Designer, setting these form properties to the names of buttons on the form will enable the keyboard shortcuts for those buttons.

Discussion

Setting a button to be a form's CancelButton object has the side effect of changing that button's DialogResult property to Cancel .

Even if you set an accept button, the Enter key doesn't always trigger it. For instance, if another button on the form has the focus, that button, and not the form's accept button, is triggered when the user presses the Enter key.



Recipe 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's Location property to a member of the My.Settings object. 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 to MainFormLocation , and select System.Drawing.Point in the Type field (Figure 4-6). Close the Project Properties window.

Figure 4-6. The added MainFormLocation property

Back on Form1 , expand its (ApplicationSettings) property. One of the subproperties should be Location . Change its value to MainFormLocation .

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 the Location subproperty, use the (PropertyBinding) subproperty instead. Click on the "…" button in its value area to display the "Application Settings for 'Form1'" dialog. Locate the Location enTRy in the form's settings list, and set its value to MainFormLocation , 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 the My.Settings object. In this recipe's case, you get a new property of type System.Drawing.Point with the name My.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