Microsoft KB Archive/816185

= How to handle events in Visual C++ .NET or in Visual C++ 2005 =

Article ID: 816185

Article Last Modified on 5/12/2007

-

APPLIES TO


 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft .NET Framework 1.1
 * Microsoft Visual C++ 2005 Express Edition

-





For a Microsoft Visual C# .NET version of this article, see 322685.



For a Microsoft Visual Basic .NET version of this article, see 319823.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System::Windows::Forms
 * System::ComponentModel
 * System::Collections

IN THIS TASK

 * INTRODUCTION
 * Create the Microsoft Visual C++ .NET or Microsoft Visual C++ 2005 project
 * Connect events through the Microsoft Visual Studio .NET or Microsoft Visual Studio 2005 IDE
 * Connect events programmatically
 * REFERENCES



INTRODUCTION
This step-by-step article describes how to connect an event of a control in Microsoft Visual C++ .NET or Microsoft Visual C++ 2005 to the code that will run when the event occurs.

Back to the top



Create the Microsoft Visual C++ .NET or Microsoft Visual C++ 2005 project

 * 1) Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005.
 * 2) On the File menu, point to New, and then click Project.
 * 3) Click Visual C++ Projects under Project Types, and then click Windows Forms Application (.NET) under Templates.

Note In Visual Studio 2005, click Visual C++ under Project Types, and then click Windows Forms Application under Templates.
 * 1) In the Name box, type Q816185.
 * 2) In the Location box, type C:\Test, and then click OK.
 * 3) Add a Button control and a ComboBox control to the form. By default, the button1 control and the comboBox1 control are added to the form.

Back to the top

Connect events through the Microsoft Visual Studio .NET IDE
 Double-click button1. The Code window opens, and the code for the default event of the button control is added. The Click event is the default event of the Button control.

Note By default, if you double-click an object on a form in Design view, the code for the function is generated and is connected to the default event automatically. Click the Form1.h [Design] tab to return to Design view. Double-click button1 again. Notice that you return to the Click event that was created in step 1. Return to Design view. Double-click the comboBox1 control. This action opens the Code window, and then it adds the code for the default event of the ComboBox control. Notice that the SelectedIndexChanged event that is connected to the comboBox1 control is a different event than the one that is created for the button1 control. SelectedIndexChanged is the default event for the ComboBox control. To add events other than the default event, follow these steps: <ol style="list-style-type: lower-alpha;"> Return to Design view. To do this, click the Form1.h[Design] tab.</li> Click the button1 control.</li> In the Properties window, click Events.</li> To add the MouseHover event for the button1 control, double-click MouseHover in the list.</li></ol>

Notes <ul> You can use the Events view of the Properties window to code any one of the events for a form or for controls. You must first select the object on the form and then select the event for that object in the Event view of the Properties window.</li> To connect the event to a procedure with the default name, double-click the event. To connect to a function that has a name that is other than the default, type a name for the event function, or select a function that already exists and that matches the signature that is required for that event from the list.</li></ul> </li> To locate any one of the events that are already written, follow these steps: <ol style="list-style-type: lower-alpha;"> Return to the Code window. To do this, click the Form1.h tab.</li> Click the first item in the left list. The first item in the list is the name of your form.

Notice that the right list lists all the events that are coded for any object that is on that form.</li> Click an event in the right list to position the cursor in that event.</li></ol> </li></ol>

Back to the top

Connect events programmatically
Note This section demonstrates how to make a method handle an event. This section does not demonstrate how to connect events dynamically. <ol>  Add the following code to the Form class to add a DropDown event for the ComboBox control: private: System::Void comboBox1_DropDown(System::Object * sender, System::EventArgs *  e)             { } Note The event handler must specify the correct arguments for the event that is being handled. In this case, the correct arguments are &quot;System::Object * sender, System::EventArgs * e&quot;. For more information about the arguments for each specific event, see the Visual Studio .NET Help documentation for the event. </li> After the event handler function is created, you must hook the function to the event. To do this, follow these steps: <ol style="list-style-type: lower-alpha;">  In the InitializeComponent function, locate the following comments: //            // comboBox1 // </li>  Add the following code this->comboBox1->DropDown += new System::EventHandler(this, comboBox1_DropDown); after the following line of code. This code hooks the SelectItemChanged event: this->comboBox1->SelectedIndexChanged += new System::EventHandler(this, comboBox1_SelectedIndexChanged); Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile this code sample. To do this, follow these steps: <ol> Click Project, and then click ProjectName Properties.

Note ProjectName represents the name of the project.</li> <li>Expand Configuration Properties, and then click General.</li> <li>Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting on the right pane, click Apply, and then click OK.</li></ol>

For more information about the common language runtime support compiler options, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

Note The name of the function can be anything. You link the function to the event by adding the EventHandler delegate. The name of the function is independent of the process of linking the event to the function. However, if you change the name of the handler, you must modify this code to reflect the new name. If you do not modify the code, the event is not handled because a handler no longer exists. </li> <li>One function can handle multiple events. These events can be for the same control or for different controls. The function that handles the DropDown event for the ComboBox control can also handle the Validated event for the Button control.

To do this, follow these steps: <ol> <li> In the InitializeComponent function, locate the following comments: //            // button1 // </li> <li> After the following line of code this->button1->Click += new System::EventHandler(this, button1_Click); paste the following code: this->button1->Validated += new System::EventHandler(this, comboBox1_DropDown) </li></ol> </li></ol> </li></ol>

Back to the top

<div class="references_section">