Back to Devexpress

How to: Wrap Multiple Sections into Regions

coderushforroslyn-401397-getting-started-examples-organize-members-how-to-wrap-multiple-sections-into-regions.md

latest2.8 KB
Original Source

How to: Wrap Multiple Sections into Regions

  • Apr 13, 2022
  • 3 minutes to read

CodeRush can wrap each member section into a region and surround these member sections with the parent region.

csharp
namespace MyApp
{
    public class Example
    {
        #region Private fields, private events, and private methods 
        #region Private fields
        object field1;
        object field2;
        #endregion
        #region Private events
        event EventHandler EventName1;
        event EventHandler EventName2;
        #endregion
        #region Private methods
        void MethodName1() { }
        void MethodName2() { }
        #endregion
        #endregion
    }
}

Follow the steps below to achieve the result shown in the code snippet above:

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

  2. In the “Rule set” section, click New to open the “New Set” dialog.

  3. Rename the suggested rule set name, for example, to “My Rule Set”. Click OK to save the rule set name and close the “New Set” dialog.

  4. Click Add Rule to create a new rule.

  5. Change the suggested rule name to “Private fields” and press Enter to save this change.

  6. In the “Group by” section, specify the following condition for the “Private fields” rule:

  7. Add a rule (see the step 4), name it “Private events”, and specify the following grouping condition:

  8. Add a rule for private methods with the following grouping condition:

  9. Select the “Private fields” rule and click Region to wrap this rule into a region.

  10. Press Enter to apply the suggested region name for private fields.

  11. Repeat steps 9-10 to wrap “Private events” and “Private methods” rules into a region.

  12. Select the “Region: Private methods” item and click Region.

  13. Change the suggested region name to: “Private fields, private events, and private methods”.

  14. Use drag & drop to position “Region: Private events” and “Region: private fields” items inside “Region: Private fields, private events, and private methods”, as shown in the screencast below:

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

  16. Place the caret in a class member in the code snippet below:

  17. Press Ctrl + . or Ctrl + ~ to invoke the Code Actions menu.

  18. Choose Organize Members from the menu and press Enter.

CodeRush groups private fields, private events, and private methods according to the specified rule order and wraps these members into regions.