Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Hungry Minds - Visual C# Blueprint.pdf
Скачиваний:
101
Добавлен:
12.02.2016
Размер:
9.71 Mб
Скачать

C#

CREATE A MODAL FORM

C# places forms in two categories: modal and modeless. The mode status of your form lets you tailor the behavior of your form so when your form

runs it gives you the results you want. A modeless form is the default form type.

A modal form, also known as a dialog box, must be closed before you can continue working with the program. For example, you probably have encountered dialog boxes that will not let you work with any other part of the program until you provide some direction such as clicking the OK or Cancel button. A modeless form lets you move between the form and other windows without requiring the user to give the form direction about what to do first.

C# lets you create your own dialog boxes within the Windows Form Designer by setting the appropriate form properties. When you set the properties you also disable some of them so the user will only be able to close the dialog box until the user presses a button, for example, the OK button.

The Form.Show method tells your form that it is modeless.

When your program runs and the program encounters the Form.Show method, it will execute code that appears after the method.

CREATE A MODAL FORM

Click the Windows Application icon in the Templates pane.

Type a name for the file.

ˇ Click OK.

Á In the Properties window, scroll down to the FormBorderStyle property.

Click the Sizable entry beside the FormBorderStyle property.

° Click to select FixedDialog from the dropdown list.

You should use modal forms when you have a form or dialog box that contains important information or information that you need direction from the user on before the program can proceed. Modeless forms are good for non-important information or for such items as toolboxes that contain buttons for use with other parts of your program.

The Form.ShowDialog method contains the optional owner argument that lets you determine which form in your program is the main form. Then, if you have any child forms that inherit from that main form, the owner argument will let the child forms know that they should take on the modal properties of the main form.

TYPE THIS:

frmAbout MyForm = new frmAbout(); MyForm.ShowDialog( this );

BUILDING FORMS 10

RESULT:

The this keyword in the Form. ShowDialog method argument tells your program that MyForm is the main form.

· Scroll down the Properties window form property list until you reach the ControlBox entry.

Click beside the True value.

Click to select False in the drop-down list.

± Set the Minimize Box and Maximize Box property entries to false.

¡ Run the program by pressing F5.

The dialog box form appears on the screen without any minimize, maximize, or close buttons in the title bar.

Close the form by pressing Alt+F4.

£ Save the program as the filename.

205

C#

LAYOUT A FORM

The Windows Form Designer in C# lets you design forms that will meet the needs of your users. C# provides three different interface styles that your form

can take: the single-document interface (SDI), the multipledocument interface, and the Explorer-style interface.

The single-document interface lets you open only one document at a time. For example, you can have only one copy of Notepad open in your computer at any one time. If you want another Notepad document open you must open a second Notepad window.

The multiple-document interface displays a parent window so you can open many windows within that parent window.

An example of an MDI includes a program like Microsoft Word where you can open the Word window and open up several document windows within it.

The Explorer-style interface splits a window into two different panes or regions on the page. The left-pane contains directory information in a tree or other hierarchy and the right-hand pane contains file information from the selected directory or object in the left-pane. Windows Explorer is the most obvious example of this interface but it can also be used with other windows where you have to navigate through a large number of objects such as data files of a certain type.

LAYOUT A FORM

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Click the Windows

 

Á Click View.

 

Click Toolbox.

 

 

.NET

 

Application icon in the

 

 

 

 

 

 

 

Studio

 

Templates pane.

 

 

 

 

 

 

 

 

 

 

Type a name for the file.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

window

 

 

ˇ Click OK.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

BUILDING FORMS 10

The program you design determines the type of window that you want to create. For example, if you have a program that requires working on more than one piece of data at a time (such as an online form), you may want to let the user work with more than one window. Then you can use the MDI style. A calculator application is better suited to an SDI style because you usually do not have to open up more than one calculator at a time.

The MDE window online help contains more detailed information about creating and designing MDI forms. There are several design aspects to be aware of when you design an MDI form including:

•Creating an MDI parent form

Creating one or more MDI child forms that appear within the parent form

Arranging MDI child forms within a parent form

Determining the active MDI child form

Sending data to the active MDI child form

This is design information that is beyond the scope of this book, but you can search for MDI in online help for more information.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The Toolbox window

 

° Click Windows Forms

 

 

Drag the TreeView object

 

 

 

appears.

until you reach the TreeView

 

to the form.

 

 

icon.

 

 

 

 

 

 

 

 

· Click theTreeView object.

 

 

 

 

 

 

 

 

 

 

 

 

 

The TreeView window appears on the form after the mouse is released.

CONTINUED

207

C#

LAYOUT A FORM

hen you design your form you can add various Wcontrols from the Toolbox. Controls are buttons,

text boxes, checkboxes, radio buttons, and other features that let the user manipulate data in the form and send that data back to the program.

After you determine the organization of your form windows — SDI, MDI, or Explorer-style — you can move on to issues about form design. The Windows Form Designer also lets you change the appearance of the form by letting you set the form size, colors, and other information.

When your Windows form appears, eight small boxes, or handles, appear around the perimeter of the form. You can move your mouse pointer to one of these handles, click and

hold down your left mouse button, and move the form accordingly. The mouse pointer turns from an arrow into a two-sided arrow when you resize it. For example, if you move the bottom center handle you can raise the height of the form. If you have controls, such as buttons, then they will resize automatically when you resize your form.

The dots in the form represent the form grid that acts as a guide for placing your controls. No matter where you place the controls on the form, the button will move the control to the nearest grid point.

LAYOUT A FORM (CONTINUED)

 

 

 

 

The ListView area appears

 

± Click both the TreeView

 

the form.

area and the ListView area

 

and drag to a lower place on

 

the form.

¡ Click the Windows Forms in the Toolbox window

until you reach the MainMenu option.

Click the MainMenu option.

BUILDING FORMS 10

You can change the size of the form as well as the title of the form in code. The argument private void InitializeComponent() in the code contains the form size and name. You can create your own form from scratch by typing the following code block within your form class. Type the following code within the public class

Form:System.Windows.Forms.Form class.

TYPE THIS:

private void InitializeComponent()

{

this.components = new System.ComponentModel.Container() this.Size = new System.Drawing.Size(100,100); this.Text = "MyForm";

}

RESULT:

This code creates a new form with the size of 100 pixels wide and 100 pixels high.

£ Add a MainMenu object to the form.

The MainMenu object appears at the top of the form and the object name appears in a window directly beneath the form.

¢ Name your menu object by clicking the object in the form and typing in a new label.

Scroll up the WinForms list and add a Label object to your form below the MainMenu object.

The label appears. You can change the label text in the Properties window.

§ Save the program as the filename.

209

Соседние файлы в папке c#