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
Open the Editor | All Languages | Organize Members options page to configure the Organize Members.
Click Add Rule to add a rule for the Form_Load event handler.
CodeRush creates a new rule and adds it to the end of the rule list.
Change the suggested rule name to “Form_Load” and press Enter to apply the change.
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.
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.
Repeat the previous steps to create and position “Form events” and “Button click event handlers” rules in the rule set.
Customize Rules
Select the Form_Load rule.
In the “Group by” section, click the “+” button to add a new condition to the “And” group.
CodeRush adds the “Node kind”.
Click the new “Node kind” item and choose Name from the list.
The And group looks as follows:
Specify the following condition:
Set the Name to Regex. For this, click “Equals” and choose “Regex” from the list.
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.
The condition for the “Form_Load” event handler looks as follows:
Click Apply to save changes.
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.
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.
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.
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.
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.
CodeRush groups event handlers according to the specified rule’s order.