Export (0) Print
Expand All
0 out of 4 rated this helpful - Rate this topic

Hooking Up Events Using Intellisense (C#) 

In the Code Editor, IntelliSense can help you hook up methods (event handlers) to event fields.

When you type the += operator after an event field in your .cs file, IntelliSense prompts you with the option to press the TAB key. This inserts a new instance of a delegate that points to the method handling the event.

Button Auto Hook Up
NoteNote

To add an event handler in a J# Windows application, drag the desired control onto the form surface, and then double-click the control to open the Code Editor and generate the event handler. Alternatively, you can add the event in the Properties window, and then add event handler code in the .jsl file. For more information, see Creating Event Handlers in Windows Forms.

If you press TAB, IntelliSense automatically completes the statement for you and displays the event handler reference as selected text in the Code Editor. To complete the automatic event hookup, IntelliSense prompts you to press the TAB key again, to create an empty stub for the event handler.

Generate Event Handler
NoteNote

If your new delegate, created by IntelliSense, references an existing event handler, then IntelliSense conveys this information in the ToolTip. You can then modify this reference, as the text is already selected in the Code Editor. Otherwise, automatic event hookup is complete at this point.

If you press TAB, IntelliSense stubs out a method with the correct signature and places the cursor in the body of your event handler.

NoteNote

Use the Navigate Backward command on the View menu (CTRL+-) to go back to the event hookup statement.

The following task shows how IntelliSense automatically hooks up an event handler named button1_Click to an event field named button1.Click.

To hook up to a C# event

  1. Create a C# Windows application.

  2. Drag a Button control from the Windows Forms tab in the Toolbox onto your form, and then use the View Code command to open Form1.cs in the Code Editor.

  3. In the form constructor, type the event field button1.Click, and then type the += operator. For example:

    this.button1.Click +=
    

    IntelliSense prompts you to press TAB to automatically hook up the event.

  4. Press TAB.

    IntelliSense creates a new instance of the EventHandler delegate with a reference to an event handler named button1_Click.

    this.button1.Click +=new EventHandler(button1_Click);
    

    Because button1_Click does not exist, IntelliSense prompts you to press TAB again so that it can create this method.

  5. Press TAB.

    IntelliSense creates the method button1_Click() and then places the cursor in the body so that you can immediately continue adding code to this event handler.

    private void button1_Click(object sender, EventArgs e)
    {
    
    }
    

Because of semantic differences, Visual J# does not provide the same level of IntelliSense support as C# for hooking up events. For more information about handling events in J#, see Creating Event Handlers in Windows Forms and Defining and Using Events.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.