Windows フォームにコンポーネントを配置する場合、デザイン環境では、 GenerateMember
と Modifiers
の 2 つのプロパティが提供されます。
GenerateMember
プロパティは、Windows フォーム デザイナーがコンポーネントのメンバー変数を生成するタイミングを指定します。
Modifiers
プロパティは、そのメンバー変数に割り当てられたアクセス修飾子です。
GenerateMember
プロパティの値がfalse
されている場合、Modifiers
プロパティの値は無効です。
コンポーネントがフォームのメンバーであるかどうかを指定する
Visual Studio の Windows フォーム デザイナーで、フォームを開きます。
ツールボックスを開き、フォームに 3 つのButton コントロールを配置します。
次の表に従って、各
GenerateMember
コントロールのModifiers
プロパティとButtonプロパティを設定します。ボタン名 メンバーを生成する値 修飾子の値 button1
true
private
button2
true
protected
button3
false
変更なし ソリューションをビルドします。
ソリューション エクスプローラーで、[すべてのファイルを表示] ボタンをクリックします。
Form1 ノードを開き、コード エディターでForm1.Designer.vbまたはForm1.Designer.cs ファイルを開きます。 このファイルには、Windows フォーム デザイナーによって出力されるコードが含まれています。
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
に変更することです。
こちらも参照ください
.NET Desktop feedback