Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture Set 7 Procedures and Event Handlers Part B - The Structure of an Application Event Handlers."— Presentation transcript:

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

2 Slide 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 Slide 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 Slide 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 Slide 5 Getting to the Events List - Step 1

6 Slide 6 Selecting an Event Handler – Step 2

7 Slide 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 Slide 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 void Button_Click(object sender, EventArgs e) { thisButton = (Button)sender; thisButton.BackColor = Color.LightGoldenrodYellow; string thisID = thisButton.Name.Substring(3, 2); int rowID = (int)thisID[0] - (int)'0'; int colID = (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 Slide 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 Slide 10 The Default Template for a Textbox This code will be executed when the content of the text box is changed

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


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

Similar presentations


Ads by Google