1. Trang chủ
  2. » Công Nghệ Thông Tin

Developing a Simple Windows Application phần 2

7 305 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Developing a simple Windows application phần 2
Định dạng
Số trang 7
Dung lượng 50,2 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

protected internal Member accessible only within the class, a derived class, or class in the same program or assembly.. protected Member accessible only within the class or derived class

Trang 1

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call //

}

/// <summary>

/// Clean up any resources being used

/// </summary>

protected override void Dispose(bool disposing)

{

if(disposing)

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose(disposing);

}

#region Windows Form Designer generated code

/// <summary>

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

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

/// </summary>

private void InitializeComponent()

{

this.myLabel = new System.Windows.Forms.Label();

this.myButton = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// myLabel

//

this.myLabel.Location = new System.Drawing.Point(8, 8);

this.myLabel.Name = "myLabel";

this.myLabel.Size = new System.Drawing.Size(288, 184);

this.myLabel.TabIndex = 0;

this.myLabel.Text = "label1";

Trang 2

//

// myButton

//

this.myButton.Location = new System.Drawing.Point(120, 200);

this.myButton.Name = "myButton";

this.myButton.Size = new System.Drawing.Size(72, 24);

this.myButton.TabIndex = 1;

this.myButton.Text = "Press Me!";

this.myButton.Click += new System.EventHandler(this.myButton_Click); //

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(304, 237);

this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.myButton,

this.myLabel});

this.Name = "Form1";

this.Text = "My Form";

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// The main entry point for the application

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void myButton_Click(object sender, System.EventArgs e)

{

myLabel.Text =

"Is this a dagger which I see before me,\n" +

"The handle toward my hand? Come, let me clutch thee.\n" +

"I have thee not, and yet I see thee still.\n" +

"Art thou not, fatal vision, sensible\n" +

"To feeling as to sight? or art thou but\n" +

"A dagger of the mind, a false creation,\n" +

"Proceeding from the heat-oppressed brain?";

Trang 3

}

}

}

As you can see, the Form1 class is derived from the System.Windows.Forms.Form class The Form class represents a Windows form

Note The System.Windows.Forms namespace contains the various classes for creating

Windows applications Most of the classes in this namespace are derived from the

System.Windows.Forms.Control class; this class provides the basic functionality for

the controls you can place on a form

The Form1 class declares two private objects named myLabel and myButton, which are the label and button controls you added to your form earlier Because the myLabel and myButton objects are private, this means that they are accessible only in the Form1 class

Access modifiers enable you to specify the degree to which a class member is available

outside the class You can also use an access modifier to specify the degree to which the class itself is available

Table 6.1 shows the access modifiers in decreasing order of availability: public is the most accessible, and private the least

Table 6.1: ACCESS MODIFIERS

ACCESS

MODIFIER

ACCESSIBILITY

public Member accessible without restriction

protected internal Member accessible only within the class, a derived class, or class in

the same program (or assembly)

internal Member accessible only within the class or class in the same

program (or assembly)

protected Member accessible only within the class or derived classes

private Member accessible only within the class This is the default

The Form1 class constructor calls the InitializeComponent() method This method adds myLabel and myButton to the form and sets the properties for those objects These

properties include the Location (the position in the form), Name, Size, TabIndex (the

Trang 4

order in which the control is accessed using the Tab key), and Text For example, the following code sets the properties of myLabel:

this.myLabel.Location = new System.Drawing.Point(8, 8);

this.myLabel.Name = "myLabel";

this.myLabel.Size = new System.Drawing.Size(288, 184);

this.myLabel.TabIndex = 0;

this.myLabel.Text = "label1";

You'll notice that the InitializeComponent() method is within #region and #endregion

preprocessor directives These directives enclose an area of code that may be hidden in

VS NET's code editor, leaving only the text that immediately follows #region visible Figure 6.5 shows how the hidden code appears in VS NET

Figure 6.5: Hiding code in VS NET using the #region directive

To view hidden code, all you have to do is to click the plus icon to the left of the code Figure 6.6 shows the code within the #region and #endregion directives

Trang 5

Figure 6.6: Viewing hidden code in VS NET

The Main() method runs the form by calling the Application.Run() method The

Application class is static and provides a number of methods you can use in your

Windows programs Because this class is static, you don't create an instance of this class, and its members are always available within your form When the Run() method is called, your form waits for events from the mouse and keyboard One example of an event is the clicking of the button in your form

The myButton_Click() method is the method you modified earlier that sets the Text

property of myLabel to a string containing the quote from Macbeth When myButton is

clicked, the myButton_Click() method is called and the text in myLabel is changed; you saw this when you ran your form earlier

In the next section, you'll learn about the VS NET Solution Explorer

Working with the Solution Explorer

You can use the VS NET Solution Explorer to view the items in your project, such as the namespace for your project Of course, a project may contain more than one namespace

To view the Solution Explorer, you select View ➣ Solution Explorer

Tip You can also view the Solution Explorer by pressing Ctrl+Alt+L on your keyboard You can use Solution Explorer to view the following items in a project's namespace:

References References include other namespaces and classes to which your form's

code refers You can employ the using statement to reference other namespaces and classes

Icon File An icon file has the extension ico You use an icon file to set the image

displayed in Windows Explorer for your application

Trang 6

Assembly File An assembly file contains the metadata for your application's

assembly An assembly is collection of code for your application

Code Files A code file is a program source file, such as the code for a form You

saw an example of this in the earlier "Examining the Code behind the Form" section

Figure 6.7 shows the Solution Explorer for this example

Figure 6.7: The Solution Explorer

As you can see from Figure 6.7, you can expand or collapse the items shown in the Solution Explorer by clicking the plus or minus icon, respectively You can also display the properties for an item in Solution Explorer: When you have the Properties window displayed, selecting an item in Solution Explorer will also display the properties for that item For example, in Figure 6.7, the properties for the MyWindowsApplication project are displayed; you can see that the project file is MyWindowsApplication.csproj

In the next section, you'll learn about the VS NET Class View

Working with the Class View

You use the VS NET Class View to examine the classes, methods, and objects in your project To see the Class View, you select View ➣ Class View

Tip You can also see the Class View by pressing Ctrl+Shift+C on your keyboard

Figure 6.8 shows the Class View for the example

Trang 7

Figure 6.8: The Class View

As you can see from Figure 6.8, you can view the classes, methods, and objects for the example You can also view the properties for a selected item in the Properties window For example, Figure 6.8 also shows the properties for the Form1 class

Next, you'll be introduced to the other types of Windows controls

Ngày đăng: 20/10/2013, 10:15