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.

Specify whether a component is a member of the form

  1. In Visual Studio, 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 name GenerateMember value Modifiers value
    button1 true private
    button2 true protected
    button3 false No 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();
    
    Private Sub InitializeComponent()
    
        ' button3 is declared in a local scope, because 
        ' its GenerateMember property is false.
        Dim button3 As System.Windows.Forms.Button
        Me.button1 = New System.Windows.Forms.Button()
        Me.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.
    
     ' The Modifiers property for button1 is "Private".
     Private button1 As Button
    
     ' The Modifiers property for button2 is "Protected".
     Protected button2 As Button
    
    ' button3 is not a member, because 
    ' its GenerateMember property is false.
    

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.

See also