How to: Use the Modifiers and GenerateMember Properties


When you place a component on a Windows Form, two properties are provided by the design environment: GenerateMember and Modifiers. The GenerateMember property specifies when the Windows Forms Designer generates a member variable for a component. The Modifiers property is the access modifier assigned to that member variable. If the value of the GenerateMember property is false, the value of the Modifiers property has no effect.

System_CAPS_ICON_note.jpg Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Customizing Development Settings in Visual Studio.

To specify whether a component is a member of the form

  1. In the Windows Forms Designer, open your form.

  2. Open the Toolbox, and on the form, place three Button controls.

  3. Set the GenerateMember and Modifiers properties for each Button control according to the following table.

    Button nameGenerateMember valueModifiers value
    button3falseNo change
  4. Build the solution.

  5. In Solution Explorer, click the Show All Files button.

  6. Open the Form1 node, and in the Code Editor,open the Form1.Designer.vb or Form1.Designer.cs file. This file contains the code emitted by the Windows Forms Designer.

  7. Find the declarations for the three buttons. The following code example shows the differences specified by the GenerateMember and Modifiers properties.

            private void InitializeComponent()
                // button3 is declared in a local scope, because 
                // its GenerateMember property is false.
                System.Windows.Forms.Button button3;
                this.button1 = new System.Windows.Forms.Button();
                this.button2 = new System.Windows.Forms.Button();
                button3 = new System.Windows.Forms.Button();

            // The Modifiers property for button1 is "private".
            private Button button1;
            // The Modifiers property for button2 is "protected".
            protected Button button2;
            // button3 is not a member, because 
            // its GenerateMember property is false.

System_CAPS_ICON_note.jpg Note

By default, the Windows Forms Designer assigns the private (Friend in Visual Basic) modifier to container controls like Panel. If your base UserControl or Form has a container control, it will not accept new children in inherited controls and forms. The solution is to change the modifier of the base container control to protected or public.

Windows Forms Visual Inheritance
Walkthrough: Demonstrating Visual Inheritance
How to: Inherit Windows Forms