方法: Modifiers プロパティおよび GenerateMember プロパティを使用する

コンポーネントを Windows フォームに配置すると、デザイン環境によって、GenerateMemberModifiers の 2 つのプロパティが提供されます。 GenerateMember プロパティでは、Windows フォーム デザイナーによってコンポーネントのメンバー変数が生成されるタイミングを指定します。 Modifiers プロパティは、そのメンバー変数に割り当てられたアクセス修飾子です。 GenerateMember プロパティの値が false の場合、Modifiers プロパティの値は無効です。

コンポーネントがフォームのメンバーであるかどうかを指定する

  1. Visual Studio の Windows フォーム デザイナーで、フォームを開きます。

  2. ツールボックスを開き、フォームに 3 つの Button コントロールを配置します。

  3. 次の表に従って、各 Button コントロールの GenerateMember プロパティと Modifiers プロパティを設定します。

    ボタン名 GenerateMember の値 Modifiers の値
    button1 true private
    button2 true protected
    button3 false 変更なし
  4. ソリューションをビルドします。

  5. ソリューション エクスプローラーで、 [すべてのファイルを表示] ボタンをクリックします。

  6. Form1 ノードを開き、コード エディターで、Form1.Designer.vb ファイルまたは Form1.Designer.cs ファイルを開きます。 このファイルには、Windows フォーム デザイナーによって出力されるコードが含まれています。

  7. 3 つのボタンの宣言を見つけます。 次のコード例は、GenerateMember プロパティと Modifiers プロパティによって指定された違いを示します。

    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.
    

注意

既定では、Windows フォーム デザイナーによって、private (Visual Basic では Friend) 修飾子が Panel のようなコンテナー コントロールに割り当てられます。 ベースの UserControl または Form にコンテナー コントロールがある場合は、継承されたコントロールやフォームによって新しい子が受け取られることはありません。 解決するには、ベース コンテナー コントロールの修飾子を protected または public に変更します。

関連項目