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

C#

ADD AN EVENT-HANDLING METHOD

C# lets you bind an event and a method in the form of an event handler. When your program invokes an event, then the event handler calls the method

associated with that event.

Event handlers are used with Windows forms in C# because they are well-suited for the events, such as a button click and the methods that follow, such as a window opening.

The event handler code contains two parameters for handling the event. The sender parameter references the argument that sent the event. The event object parameter sends an object specific to the handled event.

When you create an event handler, the calling event will produce a different object parameter type. There are some object parameter types with some built-in events in Visual Studio .NET such as mouse events.

These parameters help determine other information that is pertinent to a Windows form or any other graphical user interface that you want to program. For example, you may need information about where the mouse pointer is, where windows are on the screen, or where data is when you drag-and-drop.

ADD AN EVENT HANDLING METHOD

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Windows

 

A blank form appears in

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

icon in the

 

the parent window.

 

 

 

 

 

 

 

pane.

 

Á Access the Toolbox by

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

name for the file.

 

pressing Ctrl+Alt+X.

 

 

 

 

 

 

 

 

 

 

 

.

 

 

 

 

The toolbox window

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

appears with the Windows

 

 

 

 

 

 

 

 

 

 

 

Forms tools open.

Form1.cs

Click the Button entry.

° Click and drag the outline of the button in the form.

PROGRAMMING METHODS AND EVENTS 6

You can create an event-handling method within code. Event-handling methods are always private and no matter what event-handling method you want to add, such as a mouse button click, the method arguments remain the same.

TYPE THIS:

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

{

button1.Click += new EventHandler(button1_Click);

}

RESULT:

When you run your program and the form appears, the form will click when you press down with the left mouse button.

Properties

Click

 

· Click the Events button in

 

Click the field to the right

 

 

 

 

 

 

The event handler skeleton

 

 

 

the Properties window.

of the Click1 entry.

code appears so you can type

 

 

 

 

Type the event handler

in the event handler.

 

 

 

 

 

 

 

 

 

name.

 

 

 

± Save the program as the filename.

135

C#

VIEW INFORMATION ABOUT ARRAYS

An array is a programming staple used in many different languages; arrays act as containers for elements in the same data type. For example, an

array can contain a group of integers. C# treats arrays as objects that the program accesses through a reference variable.

You enter arrays using two square brackets ([]) after the array type and then enter the array identifier. C# indexes arrays starting with zero. For example, if you create an array that has ten elements in it, the array identifies the elements in the array from 0 through 9.

C# supports three different types of arrays: singledimensional arrays, multidimensional (or rectangular) arrays, and array-of-arrays (jagged arrays).

A single-dimensional array is the simplest type. You can use single-dimensional arrays for storing simple lists like your friends’ names or a set of numbers.

A multidimensional or rectangular array lets you store data information by x and y types much as you do when you store data in a spreadsheet column and row.

An array-of-arrays or jagged array lets you nest an array within one or more arrays so an element in one array can access elements in its partner arrays.

This chapter takes you through the different arrays and how to use each array type properly.

VIEW INFORMATION ABOUT ARRAYS

Properties

Click Start Programs

The Start page appears.

 

¤ Click Help.

Click Index.

 

Microsoft Visual Studio .NET

 

 

 

 

7.0 Microsoft Visual

 

 

 

 

Studio .NET 7.0.

 

 

 

 

USING ARRAYS 7

Several array declaration differences exist between C#, C/C++, and Java. The differences are more pronounced between C# and C/C++. The differences (and similarities) include:

Declaring an array is the same in Java as it is in C#; you activate an array by including the new operator.

You cannot place the bracket after the identifier as you can in C or C++. If you are an experienced C or C++ programmer, take care to ensure that your brackets appear after the type.

The array is not part of its type as it is in C and C++. This feature lets you assign as many objects of a type, such as byte to an array no matter how long the array is.

When you initialize an array, you include the array elements without entering the new int [] argument as you do in Java.

Index

The Index window appears.

Note: Close the Properties window by clicking at the right side of the Properties window title bar.

Type arrays in the Look for field.

ˇ Click to select Visual C# in the Filtered by dropdown menu.

Á Click C# under the arrays topic list.

The Arrays Tutorial appears in the parent window.

137

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