How to handle events in Visual C++ .NET or in Visual C++ 2005 (816185)



The information in this article applies to:

  • Microsoft Visual C++ .NET (2003)
  • 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

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.
  4. In the Name box, type Q816185.
  5. In the Location box, type C:\Test, and then click OK.
  6. 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

  1. 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.
  2. Click the Form1.h [Design] tab to return to Design view.
  3. Double-click button1 again. Notice that you return to the Click event that was created in step 1.
  4. Return to Design view.
  5. 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.
  6. To add events other than the default event, follow these steps:
    1. Return to Design view. To do this, click the Form1.h[Design] tab.
    2. Click the button1 control.
    3. In the Properties window, click Events.
    4. To add the MouseHover event for the button1 control, double-click MouseHover in the list.
    Notes
    • 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.
    • 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.
  7. To locate any one of the events that are already written, follow these steps:
    1. Return to the Code window. To do this, click the Form1.h tab.
    2. 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.
    3. Click an event in the right list to position the cursor in that event.
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.
  1. 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 "System::Object * sender, System::EventArgs * e". For more information about the arguments for each specific event, see the Visual Studio .NET Help documentation for the event.
  2. After the event handler function is created, you must hook the function to the event. To do this, follow these steps:
    1. In the InitializeComponent function, locate the following comments:
                  // 
                  // comboBox1
                  // 
      
    2. 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:
      1. Click Project, and then click ProjectName Properties.

        Note ProjectName represents the name of the project.
      2. Expand Configuration Properties, and then click General.
      3. 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.
      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.
    3. 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:
      1. In the InitializeComponent function, locate the following comments:
                    // 
                    // button1
                    // 
        
      2. 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)
        
Back to the top

REFERENCES

For more information about how to handle events, visit the following Microsoft Developer Network (MSDN) Web site:back to the top

Modification Type:MajorLast Reviewed:1/16/2006
Keywords:kbWindowsForms kbForms kbCtrl kbButton kbComboBox kbEvent kbHOWTOmaster KB816185 kbAudDeveloper kbAudITPRO