120 likes | 296 Views
Lecture Set 7. Procedures and E vent Handlers Part B - T he Structure of an Application Event Handlers. Objectives. To understand events and event handlers How are event handlers generated? How are event handlers invoked? Handling multiple events with one event handler
E N D
Lecture Set 7 Procedures and Event Handlers Part B - The Structure of an Application Event Handlers
Objectives • To understand events and event handlers • How are event handlers generated? • How are event handlers invoked? • Handling multiple events with one event handler • There are different types of events • “Wiring” an event to an event handler
Introduction to Control Events • Every form control type (including a form itself) has a list of events associated with it • As we saw in earlier lectures, every event also has a default event that is generated if you double click on the control in the form • But you can generate your own events • We can view the list of events for a control and select the handler(s) needed for our application in the Events List
How to Get to the Events List for a Control • If the properties window for controls is not already docked and visible, then • Right click on the Control (such as a Textbox) • Click on the Properties item in the pull down list and click on the lightening bolt in the menu bar of the properties window (now you can see the entire list of event handlers
Wiring a Handler to an Event • Visual Studio “wires” an event to a handler using the “handles clause” in the handler header • In working with events, pay attention to how events and handlers are named -- • For example, for the validating event for a text box: txtMonthlyInvestment_Validating • We will see more about validation later • Handler names can be changed – but I don’t recommend it • There are “tons” of events for a control • We can add and remove event wiring
Wiring a Handler to an Event(Example - again) // Add this button to the event handler list so that we may use the // .Click even // This is very important, make sure you add each individual event // handler you want to use here this.boardView[row, col].Click += new System.EventHandler(this.Button_Click); // The actual event handler private voidButton_Click(object sender, EventArgs e) { thisButton= (Button)sender; thisButton.BackColor= Color.LightGoldenrodYellow; stringthisID = thisButton.Name.Substring(3, 2); introwID = (int)thisID[0] - (int)'0'; intcolID = (int)thisID[1] - (int)'0'; ((Button)sender).Enabled = false; ((Button)sender).Text = "C"; MessageBox.Show("row and col = " + rowID.ToString() + " " + colID.ToString()); } // end Button Click
Event Handler Templates • We have seen example of these almost since day one in this course • We can generate them from the Designer View of a component or from the Code editor • We may want to generate • the default handler or • some other handler for a given control
The Default Template for a Textbox • This code will be executed when the content of the text box is changed
Code for a Validating Template • There are other event handlers for a textbox • Right click on the box. Go to Properties • Click on the lightening bolt (Events) icon • The validating template is another one of the many event templates for a textbox • This is a highly structured and complicated even • You should know the list of templates exists
Comments on Form Events • Events fire as a form gets focus and loses focus • The Activated event fires each time a form gets focus • The Deactivate event fires each time a form loses focus • The Load event fires when a form is loaded into memory • The FormClosing event fires just before a form is unloaded from memory • After a form has closed, the FormClosed event fires