Understanding GUI Events

Hiểu biết về sự kiện GUI Như đã đề cập trước đó, các NET Framework. Lớp học và điều khiển được sử dụng cho GUIs xây dựng sử dụng rộng rãi các sự kiện. Bạn sẽ nhìn thấy và sử dụng GUI sự kiện nhiều lần trong nửa sau của cuốn sách này. Ví dụ, lớp Button có nguồn gốc từ lớp Control | Understanding GUI Events As mentioned earlier the .NET Framework classes and controls used for building GUIs employ events extensively. You ll see and use GUI events on many occasions in the second half of this book. For example the Button class derives from the Control class inheriting a public event called Click of type EventHandler. Let s see this in code. The EventHandler delegate expects two parameters a reference to the object that caused the event to be raised and an EventArgs object that contains additional information about the event namespace System public delegate void EventHandler object sender EventArgs args public class EventArgs . namespace public class Control public event EventHandler Click . public class Button Control . The Button class automatically raises the Click event when you click the button onscreen how this actually happens is beyond the scope of this book . This arrangement makes it easy to create a delegate for a chosen method and attach that delegate to the required event. The following example shows a Windows form that contains a button called okay a method called okay_Click and the code to connect the Click event in the okay button to the okay_Click method class Example private okay . public Example new new . private void okay_Click object sender args Your code to handle the Click event When you use the Designer View in Visual Studio 2005 the IDE generates the code that subscribes methods to events automatically. All you have to do is write the logic in the event handling method. NOTE It is possible to add a method to an event without creating an instance of a delegate. You could replace the following statement new with this However the Windows Forms designer in Visual .

Bấm vào đây để xem trước nội dung
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.