次の方法で共有


方法: 修飾子と GenerateMember プロパティを使用する

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

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

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

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

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

    ボタン名 メンバーを生成する値 修飾子の値
    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などのコンテナー コントロールにFriend (Visual Basic のPanel) 修飾子を割り当てます。 基本 UserControl または Form にコンテナー コントロールがある場合、継承されたコントロールやフォームでは新しい子を受け入れません。 解決策は、基本コンテナー コントロールの修飾子を protected または publicに変更することです。

こちらも参照ください