Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Manning - Windows Forms Programming With CSharp.pdf
Скачиваний:
72
Добавлен:
24.05.2014
Размер:
14.98 Mб
Скачать

 

 

 

 

 

 

ADD CONTROLS TO THE CAPTIONDLG FORM

 

 

 

 

 

 

 

 

 

 

 

 

 

Action

 

 

 

Result

 

 

 

 

 

 

 

 

 

3

 

Before adding any controls, lock

 

 

 

the toolbox open.

 

 

 

 

 

 

How-to

 

 

 

 

 

 

Open the Toolbox window and click

 

 

 

the push-pin graphic at the top

 

 

 

right of this window.

 

 

 

 

 

 

 

 

 

 

 

 

 

Note: The order of controls in your toolbox may dif-

 

 

 

 

 

 

 

 

 

fer from those shown here. You can sort this list

 

 

 

 

 

 

 

 

 

alphabetically by right-clicking on the Windows

 

 

 

 

 

 

 

 

 

Forms title and selecting the Sort Items Alphabeti-

 

 

 

 

 

 

 

 

 

cally option.

 

 

 

 

 

 

 

 

 

4

 

Add an OK button to the base of

 

 

 

the form.

 

 

 

 

 

 

How-to

 

 

 

 

 

 

Drag a Button control from the

 

 

 

toolbox onto the form, and assign

 

 

 

its properties as indicated.

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Property

 

 

Value

 

 

 

 

(Name)

 

 

btnOK

 

 

 

 

DialogResult

 

 

OK

 

 

 

 

Text

 

 

&OK

 

 

 

Note: We discuss the meaning of

 

 

 

the DialogResult property later

 

 

 

in the chapter.

 

 

 

 

 

 

 

 

 

 

 

 

 

5

 

Add a Cancel button to the form,

 

 

 

and position the two buttons as

 

 

 

shown in the graphic.

 

 

 

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Property

 

Value

 

 

 

 

(Name)

 

btnCancel

 

 

 

 

DialogResult

 

Cancel

 

 

 

 

Text

 

&Cancel

 

 

 

 

 

 

 

 

 

 

Note: There is a bit of black magic involved in posi-

 

 

 

 

 

 

 

 

 

tioning controls. The Format menu in Visual Studio

 

 

 

 

 

 

 

 

 

.NET provides commands for positioning and align-

 

 

 

 

 

 

 

 

 

ing controls. These appear on the Layout toolbar,

 

 

 

 

 

 

 

 

 

and you can experiment with these while creating

 

 

 

 

 

 

 

 

 

this form.

 

 

 

 

 

 

 

 

 

 

MODAL DIALOG BOXES

243

ADD CONTROLS TO THE CAPTIONDLG FORM (continued)

 

 

 

 

 

Action

 

 

 

Result

 

 

 

 

 

 

 

 

 

 

6

Create the Image and Caption

 

 

labels on the form.

 

 

 

 

 

How-to

 

 

 

 

 

 

 

Drag two Label controls onto the

 

 

form, and resize and position them

 

 

as in the graphic.

 

 

 

 

 

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Label

 

Property

Value

 

 

 

 

Image

 

Text

Image:

 

 

 

 

 

 

TextAlign

MiddleRight

Note: For the Visual C++ programmers among us,

 

 

 

Caption

 

Text

Caption:

 

 

 

 

the Label class is similar to the CStatic class

 

 

 

 

 

TextAlign

MiddleRight

found in the MFC library.

 

 

 

 

 

 

 

 

 

 

 

7

Create a lblImage label on the

 

 

form.

 

 

 

 

 

 

 

 

Note: We could also use a read-

 

 

 

only TextBox control here. Labels

 

 

 

and text boxes are discussed in

 

 

 

detail in chapter 9.

 

 

 

 

 

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Property

 

Value

 

 

 

 

(Name)

 

lblImage

 

 

 

 

BorderStyle

 

Fixed3D

 

 

 

 

Text

 

image file name

 

 

 

 

TextAlign

 

MiddleLeft

 

 

 

 

 

 

 

 

 

 

 

 

8

Create a text box to hold the image

 

 

caption.

 

 

 

 

 

 

 

How-to

 

 

 

 

 

 

 

Drag a TextBox control onto the

 

 

form.

 

 

 

 

 

 

 

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Property

 

Value

 

 

 

 

(Name)

 

txtCaption

 

 

 

 

Text

 

image caption

 

 

 

 

 

 

 

 

 

 

 

Note: Again for Visual C++ programmers, the

 

 

 

 

 

 

 

 

 

 

TextBox class is similar to the CEdit class found

 

 

 

 

 

 

 

 

 

 

in the MFC library.

 

 

 

 

 

 

 

 

 

 

 

244

CHAPTER 8 DIALOG BOXES

ADD CONTROLS TO THE CAPTIONDLG FORM (continued)

Action

Result

9Set the tab order for the controls on the form.

How-to

a.Click the top-level View menu.

b.Select the Tab Order item.

c.Click the controls in the desired order, starting with number 0, as shown in the graphic.

d.Press the Esc key to save the new tab order.

Well, that took a while. Placing controls on a form is not a difficult task, but it can take some time. This is another reason sketching out your interface up front is a good idea before you spend too much time in the designer window.

As you might expect, the code generated in the CaptionDlg.cs source file is quite similar to what we have seen for our MainForm class in previous chapters. As a quick recap, here is a summary of the code you will find in this file.

The CaptionDlg class is derived from the Form class.

public class CaptionDlg : System.Windows.Forms.Form

{

Each control is created as a private member of the class. The organization used by Visual Studio can be a bit confusing, so I have used the power of cut and paste to rearrange these code excerpts to be a bit more logical. If you recall, the

components member is required by Visual Studio to manage certain controls on the form.

private System.Windows.Forms.Button btnOK; private System.Windows.Forms.Button btnCancel; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label lblImage; private System.Windows.Forms.TextBox txtCaption;

///<summary>

///Required designer variable.

///</summary>

private System.ComponentModel.Container components = null;

The controls are initialized in the InitializeComponent method, which is called from the CaptionDlg constructor.

#region Windows Form Designer generated code

/// <summary>

MODAL DIALOG BOXES

245

///Required method for Designer support - do not modify

///the contents of this method with the code editor.

///</summary>

private void InitializeComponent()

{

Inside the InitializeComponent method, the controls are first created using the new keyword.

this.btnOK = new System.Windows.Forms.Button (); this.btnCancel = new System.Windows.Forms.Button (); this.label1 = new System.Windows.Forms.Label (); this.label2 = new System.Windows.Forms.Label (); this.lblImage = new System.Windows.Forms.Label (); this.txtCaption = new System.Windows.Forms.TextBox ();

Next the nondefault properties are set for each control. This section is quite long, so the following code is only an excerpt of this portion of the file:

this.SuspendLayout();

//

//btnOK

this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK; this.btnOK.Location = new System.Drawing.Point(82, 88);

this.btnOK.Name = "btnOK"; this.btnOK.TabIndex = 0;

this.btnOK.Text = "&OK";

//btnCancel

//

this.btnCancel.DialogResult

= System.Windows.Forms.DialogResult.Cancel; this.btnCancel.Location = new System.Drawing.Point(187, 88); this.btnCancel.Name = "btnCancel";

this.btnCancel.TabIndex = 1; this.btnCancel.Text = "&Cancel";

//

// label1

//

this.label1.Location = new System.Drawing.Point(32, 8); this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(48, 23); this.label1.TabIndex = 2;

. . .

Finally, the Form itself is initialized, and the controls are added to the form using the Form.Controls property.

//

// CaptionDlg

//

246

CHAPTER 8 DIALOG BOXES