Control.ControlCollection.AddRange メソッド
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)


controls 配列に格納されている Control オブジェクトが、コレクションの末尾に追加されます。
Add メソッドを使用して個別の Control を手動でコレクションに追加するのではなく、AddRange メソッドを使用して Control オブジェクトのグループをすばやくコレクションに追加できます。
以前追加した Control を削除するには、Remove、RemoveAt、Clear の各メソッドを使用します。
継承時の注意 派生クラスで AddRange をオーバーライドする場合は、基本クラスの AddRange メソッドを呼び出して、確実にコントロールがコレクションに追加されるようにしてください。
2 つの Control オブジェクトを派生クラス Panel の Control.ControlCollection に追加するコード例を次に示します。この例では、Form 上に Panel コントロールと Button コントロールが作成されている必要があります。ボタンがクリックされると、2 つの RadioButton コントロールがパネルの Control.ControlCollection に追加されます。
' Create two RadioButtons to add to the Panel. Dim RadioAddButton As RadioButton = New RadioButton() Dim RadioAddRangeButton As RadioButton = New RadioButton() ' Add controls to the Panel using the AddRange method. Private Sub AddRangeButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles AddRangeButton.Click ' Set the Text the RadioButtons will display. RadioAddButton.Text = "RadioAddButton" RadioAddRangeButton.Text = "RadioAddRangeButton" ' Set the appropriate location of RadioAddRangeButton. RadioAddRangeButton.Location = New System.Drawing.Point( _ RadioAddButton.Location.X, _ RadioAddButton.Location.Y + RadioAddButton.Height) ' Add the controls to the Panel. Panel1.Controls.AddRange(New Control() {RadioAddButton, RadioAddRangeButton}) End Sub
// Create two RadioButtons to add to the Panel. private RadioButton radioAddButton = new RadioButton(); private RadioButton radioRemoveButton = new RadioButton(); // Add controls to the Panel using the AddRange method. private void addRangeButton_Click(object sender, System.EventArgs e) { // Set the Text the RadioButtons will display. radioAddButton.Text = "radioAddButton"; radioRemoveButton.Text = "radioRemoveButton"; // Set the appropriate location of radioRemoveButton. radioRemoveButton.Location = new System.Drawing.Point( radioAddButton.Location.X, radioAddButton.Location.Y + radioAddButton.Height); //Add the controls to the Panel. panel1.Controls.AddRange(new Control[]{radioAddButton, radioRemoveButton}); }
// Create two RadioButtons to add to the Panel. private: RadioButton^ radioAddButton; RadioButton^ radioRemoveButton; // Add controls to the Panel using the AddRange method. void addRangeButton_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ ) { radioAddButton = gcnew RadioButton; radioRemoveButton = gcnew RadioButton; // Set the Text the RadioButtons will display. radioAddButton->Text = "radioAddButton"; radioRemoveButton->Text = "radioRemoveButton"; // Set the appropriate location of radioRemoveButton. radioRemoveButton->Location = System::Drawing::Point( radioAddButton->Location.X, radioAddButton->Location.Y + radioAddButton->Height ); //Add the controls to the Panel. array<Control^>^controlArray = {radioAddButton,radioRemoveButton}; panel1->Controls->AddRange( controlArray ); }
// Create two RadioButtons to add to the Panel. private RadioButton radioAddButton = new RadioButton(); private RadioButton radioRemoveButton = new RadioButton(); // Add controls to the Panel using the AddRange method. private void addRangeButton_Click(Object sender, System.EventArgs e) { // Set the Text the RadioButtons will display. radioAddButton.set_Text("radioAddButton"); radioRemoveButton.set_Text("radioRemoveButton"); // Set the appropriate location of radioRemoveButton. radioRemoveButton.set_Location(new System.Drawing.Point(radioAddButton. get_Location().get_X(), radioAddButton.get_Location().get_Y() + radioAddButton.get_Height())); //Add the controls to the Panel. panel1.get_Controls().AddRange(new Control[] { radioAddButton, radioRemoveButton }); } //addRangeButton_Click

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- Control.ControlCollection.AddRange メソッドのページへのリンク