FunctionX Practical Learning Logo

A Common Event For Various Controls

 

Introduction

If you have various controls that perform the same action, you can write your own event and make it available to those controls. For example, you can write one member function of a form to respond to a possible Click event. Then, when a button is clicked, you can find out what button was clicked and then act accordingly

Creating and Implementing the Common Event

  1. Start Microsoft Visual C++ .Net and create a Windows Form Application named EventSharing
  2. In the last section of the header file of the form, create the following event:
     
    public:
        void AButtonWasClicked(Object* Sender, EventArgs* EArgs)
        {
    	if( Sender->Equals(button1) )
    	    MessageBox::Show("The first button sent the message");
    	else if( Sender == button2 )
    	    MessageBox::Show("The message was sent by the second button");
    	else if( Sender == button3 )
    	    MessageBox::Show("Let the third button handle this message");
    	}
        };
    }
  3. Add three buttons to the form
  4. Click each button and, in the Events section of the Properties window, select AButtonWasClicked
  5. Test the application
 

Home Copyright © 2004 FunctionX, Inc.