Danh mục

Understanding GUI Events

Số trang: 3      Loại file: pdf      Dung lượng: 10.02 KB      Lượt xem: 2      Lượt tải: 0    
Hoai.2512

Hỗ trợ phí lưu trữ khi tải xuống: 2,000 VND Tải xuống file đầy đủ (3 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

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
Nội dung trích xuất từ tài liệu:
Understanding GUI Events Understanding GUI EventsAs mentioned earlier, the .NET Framework classes and controls used for building GUIsemploy events extensively. Youll see and use GUI events on many occasions in thesecond half of this book. For example, the Button class derives from the Control class,inheriting a public event called Click of type EventHandler. Lets see this in code. TheEventHandler delegate expects two parameters; a reference to the object that caused theevent to be raised, and an EventArgs object that contains additional information about theevent:namespace System{ public delegate void EventHandler(object sender, EventArgs args) ; public class EventArgs { ... }}namespace System.Windows.Forms{ public class Control : { public event EventHandler Click; ... } public class Button : Control { ... }}The Button class automatically raises the Click event when you click the button on-screen (how this actually happens is beyond the scope of this book). This arrangementmakes it easy to create a delegate for a chosen method and attach that delegate to therequired event. The following example shows a Windows form that contains a buttoncalled okay, a method called okay_Click, and the code to connect the Click event in theokay button to the okay_Click method:class Example : System.Windows.Forms.Form{ private System.Windows.Forms.Button okay; ... public Example() { this.okay = new System.Windows.Forms.Button(); this.okay.Click += new System.EventHandler(this.okay_Click); ... } private void okay_Click(object sender, System.EventsArgs args) { // Your code to handle the Click event }}When you use the Designer View in Visual Studio 2005, the IDE generates the code thatsubscribes methods to events automatically. All you have to do is write the logic in theevent handling method.NOTEIt is possible to add a method to an event without creating an instance of a delegate. Youcould replace the following statement:this.okay.Click += new System.EventHandler (this.okay_Click);with this:this.okay.Click += this.okay_Click;However, the Windows Forms designer in Visual Studio 2005 always generates the firstversion.The events that the GUI classes generate always follow the same pattern. The events areof a delegate type whose signature has a void return type and two arguments. The firstargument is always the sender of the event and the second argument is always anEventArgs argument (or a class derived from EventArgs).The sender argument allows you to reuse a single method for multiple events. Thedelegated method can examine the sender argument and respond accordingly. Forexample, you can use the same method to subscribe to the Click event fo two buttons(you add the same method to two different events). When the event is raised, the code inthe method can examine the sender argument to ascertain which button was clicked.

Tài liệu được xem nhiều: