Skip to main content
All docs
V22.2

How to: Group Members by Member Name

  • 4 minutes to read

Organize Members allows you to group members (properties, methods, etc.) by the member name. This example shows how to create, configure, and apply rules for Form_Load, Form events, and Button click event handlers.

Add Event Handlers Rules to a Rule Set

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

    organize-members-page

  2. Click Add Rule to add a rule for the Form_Load event handler.

    click-add-rule

    CodeRush creates a new rule and adds it to the end of the rule list.

  3. Change the suggested rule name to “Form_Load” and press Enter to apply the change.

    specify-form-load-rule-name

  4. Change the “Form_Load” rule’s position, as shown in the screencast below, so that the “Form_Load” event handler follows constructors in a class. To change the rule’s position, drag the “Form_Load” rule in the rule list and drop it when you reach the desired position.

    change-form-load-rule-position

    When you run Organize Members, CodeRush changes the type’s member order to correspond to the order of the rules in the rule set.

    Note

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

  5. Repeat the previous steps to create and position “Form events” and “Button click event handlers” rules in the rule set.

    event-handlers-rule-position

Customize Rules

  1. Select the Form_Load rule.

    select-form-load-rule

  2. In the “Group by” section, click the “+” button to add a new condition to the “And” group.

    click-plus-button-to-add-condition

    CodeRush adds the “Node kind”.

    added-node-kind

  3. Click the new “Node kind” item and choose Name from the list.

    choose-name-for-node-kind

    The And group looks as follows:

    name-equals-condition

  4. Specify the following condition:

    Set the Name to Regex. For this, click “Equals” and choose “Regex” from the list.

    change-equals-to-regex

    Type the “\w*Form\w*_Load\w*” regular expression in the “enter a value” text box and press Enter to apply the change.

    The “\w*Form\w*_Load\w*” regular expression allows Organize Members to group event handlers whose name contain the “*Form*_Load*” mask.

    Note

    Organize Members also allows you to use Equals, Contains, and Starts with string operations to group members.

    Set the Node kind to Method. For this, click “Member” and choose the “Method” item from the list.

    change-member-kind-to-method

    The condition for the “Form_Load” event handler looks as follows:

    form-load-group-condition

  5. Click Apply to save changes.

  6. Select the “Form events” rule. Add a new condition to the “And” group and set the first “Node kind” item to Name, as described in the previous steps.

    form-events-and-group

  7. Specify the following condition for the And group: set the Name to Regex, type the “\w*Form\w*_\w*” regular expression in the “enter a value” text box, and press Enter. Set the Node kind to Method.

    form-events-regex

  8. Select the “Button click event handlers” rule. Add a new condition to the “And” group and set the first “Node kind” item to Name, as described above.

    select-button-click-event-rule

  9. Specify the following criteria: set Name to Regex, type the “\w*Button\w*_click\w*” regular expression in the text box, and press Enter. Set the Node kind to Method.

    button-click-handler-regex

  10. Click Apply and OK to save the changes and close the Organize Members options page.

Run Organize Members

This section shows how to apply Form_Load, Form events, and Button click event handlers rules.

In the following code, place the caret anywhere in the class body:

using System;
using System.Windows.Forms;

namespace WinFormsApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {

        }
    }
}

Press Ctrl+. or Ctrl+~ to invoke the Code Actions Menu. Select Organize Members and press Enter.

group-events-rules

CodeRush groups event handlers according to the specified rule’s order.

See Also