1 / 12

Lecture Set 7

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

jenski
Télécharger la présentation

Lecture Set 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture Set 7 Procedures and Event Handlers Part B - The Structure of an Application Event Handlers

  2. 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

  3. 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

  4. 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  

  5. Getting to the Events List - Step 1

  6. Selecting an Event Handler – Step 2

  7. 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 

  8. 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

  9. 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

  10. The Default Template for a Textbox • This code will be executed when the content of the text box is changed

  11. 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

  12. 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

More Related