Skip to main content
A newer version of this page is available. .

How to: Group Event Handlers and Wrap them into a Region

  • 3 minutes to read

Organize Members can group event handlers and wrap them into a region according to the event handlers rule. This example shows how to create, configure, and apply the event handlers rule.

Add an Event Handlers Rule to a Rule Set

  • Open the Editor | All Languages | Organize Members options page to configure the Organize Member.

    OrganizeMembersPage

  • Click Add Rule to add a rule for event handlers.

    OrganizeMembersPage

  • Type a rule name in the corresponding text box.

    OrganizeMembersPage

  • Click Move Up to change the rule's position in the rule set.
    When you run Organize Members, CodeRush changes the type's member order to correspond to the order of the rules in the rule set. Change the Event handlers rule's position, as shown in the screenshot below, if you want event handlers to follow explicit interface implementations in a type.

    OrganizeMembersPage

NOTE

Organize Members applies rules according to the specified order in the rule set.

Customize the Rule

Configure Grouping

This section describes how to specify a condition which is used to group event handlers.

  • Focus the Node kind item.

  • Switch Kind to IsEventHandler.

  • Click And to create the And group.

  • Expand the new And group to see the result.

When you run Organize Members, CodeRush analyzes all type members and checks the IsEventHandler condition to create an event handlers group.

Configure Event Handlers Wrapping into a Region

  • Select Event handlers in the rule list and click Region.

    ClickRegionButton

  • Type "Event handlers" in the "Region name" text box.

    RegionTextField

  • Click Apply to save changes or click OK to save changes and close the CodeRush configuration menu.

Run Organize Members

This section shows how to apply the event handlers rule.

In the following code, place the caret to any place in the class body.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApp4 {
    public partial class Form1 : Form {

        public Form1() {
            InitializeComponent();

            Button button = CreateButton();
            this.Controls.Add(button);
        }

        private Button CreateButton() {
            var myButton = new Button();
            myButton.Text = "Click me!";
            myButton.Click += myButton_Click;
            myButton.MouseEnter += MyButton_MouseEnter;
            myButton.MouseLeave += MyButton_MouseLeave;

            return myButton;
        }

        private void myButton_Click(object sender, EventArgs e) {
            MessageBox.Show("Button clicked!");
        }

        private void MyButton_MouseEnter(object sender, EventArgs e) {
            var myButton = sender as Button;
            myButton.ForeColor = Color.Red;
        }

        private void MyButton_MouseLeave(object sender, EventArgs e) {
            var myButton = sender as Button;
            myButton.ForeColor = Color.Yellow;
        }
    }
}

Press Ctrl+. or Ctrl+~ to invoke the Code Actions Menu and select Organize Members.

CodeRush groups event handlers according to the specified rule and wraps these handlers into a region.