ListBox.ObjectCollection.Add(Object) メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
ListBoxの項目の一覧に項目を追加します。
public:
int Add(System::Object ^ item);
public int Add(object item);
member this.Add : obj -> int
Public Function Add (item As Object) As Integer
パラメーター
- item
- Object
コレクションに追加する項目を表すオブジェクト。
戻り値
コレクション内の項目の 0 から始まるインデックス。 BeginUpdate() が呼び出された場合は -1。
例外
新しい項目をリストに追加するための空き領域が不足しています。
item は nullです。
例
次のコード例では、複数の項目を列に表示し、コントロールの一覧で複数の項目を選択できる ListBox コントロールを作成する方法を示します。 この例のコードでは、ListBox.ObjectCollection クラスのAdd メソッドを使用して 50 個の項目をListBoxに追加し、SetSelected メソッドを使用してリストから 3 つの項目を選択します。 次に、 ListBox.SelectedObjectCollection コレクションの値 ( SelectedItems プロパティを使用) と ListBox.SelectedIndexCollection ( SelectedIndices プロパティを使用して) を表示します。 この例では、コードを配置し、 Formから呼び出す必要があります。
void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
// Create an instance of the ListBox.
ListBox^ listBox1 = gcnew ListBox;
// Set the size and location of the ListBox.
listBox1->Size = System::Drawing::Size( 200, 100 );
listBox1->Location = System::Drawing::Point( 10, 10 );
// Add the ListBox to the form.
this->Controls->Add( listBox1 );
// Set the ListBox to display items in multiple columns.
listBox1->MultiColumn = true;
// Set the selection mode to multiple and extended.
listBox1->SelectionMode = SelectionMode::MultiExtended;
// Shutdown the painting of the ListBox as items are added.
listBox1->BeginUpdate();
// Loop through and add 50 items to the ListBox.
for ( int x = 1; x <= 50; x++ )
{
listBox1->Items->Add( String::Format( "Item {0}", x ) );
}
listBox1->EndUpdate();
// Select three items from the ListBox.
listBox1->SetSelected( 1, true );
listBox1->SetSelected( 3, true );
listBox1->SetSelected( 5, true );
#if defined(DEBUG)
// Display the second selected item in the ListBox to the console.
System::Diagnostics::Debug::WriteLine( listBox1->SelectedItems[ 1 ] );
// Display the index of the first selected item in the ListBox.
System::Diagnostics::Debug::WriteLine( listBox1->SelectedIndices[ 0 ] );
#endif
}
private void button1_Click(object sender, System.EventArgs e)
{
// Create an instance of the ListBox.
ListBox listBox1 = new ListBox();
// Set the size and location of the ListBox.
listBox1.Size = new System.Drawing.Size(200, 100);
listBox1.Location = new System.Drawing.Point(10,10);
// Add the ListBox to the form.
this.Controls.Add(listBox1);
// Set the ListBox to display items in multiple columns.
listBox1.MultiColumn = true;
// Set the selection mode to multiple and extended.
listBox1.SelectionMode = SelectionMode.MultiExtended;
// Shutdown the painting of the ListBox as items are added.
listBox1.BeginUpdate();
// Loop through and add 50 items to the ListBox.
for (int x = 1; x <= 50; x++)
{
listBox1.Items.Add("Item " + x.ToString());
}
// Allow the ListBox to repaint and display the new items.
listBox1.EndUpdate();
// Select three items from the ListBox.
listBox1.SetSelected(1, true);
listBox1.SetSelected(3, true);
listBox1.SetSelected(5, true);
// Display the second selected item in the ListBox to the console.
System.Diagnostics.Debug.WriteLine(listBox1.SelectedItems[1].ToString());
// Display the index of the first selected item in the ListBox.
System.Diagnostics.Debug.WriteLine(listBox1.SelectedIndices[0].ToString());
}
Private Sub button1_Click(sender As Object, e As System.EventArgs)
' Create an instance of the ListBox.
Dim listBox1 As New ListBox()
' Set the size and location of the ListBox.
listBox1.Size = New System.Drawing.Size(200, 100)
listBox1.Location = New System.Drawing.Point(10, 10)
' Add the ListBox to the form.
Me.Controls.Add(listBox1)
' Set the ListBox to display items in multiple columns.
listBox1.MultiColumn = True
' Set the selection mode to multiple and extended.
listBox1.SelectionMode = SelectionMode.MultiExtended
' Shutdown the painting of the ListBox as items are added.
listBox1.BeginUpdate()
' Loop through and add 50 items to the ListBox.
Dim x As Integer
For x = 1 To 50
listBox1.Items.Add("Item " & x.ToString())
Next x
' Allow the ListBox to repaint and display the new items.
listBox1.EndUpdate()
' Select three items from the ListBox.
listBox1.SetSelected(1, True)
listBox1.SetSelected(3, True)
listBox1.SetSelected(5, True)
' Display the second selected item in the ListBox to the console.
System.Diagnostics.Debug.WriteLine(listBox1.SelectedItems(1).ToString())
' Display the index of the first selected item in the ListBox.
System.Diagnostics.Debug.WriteLine(listBox1.SelectedIndices(0).ToString())
End Sub
注釈
ListBoxのSortedプロパティがtrueに設定されている場合、項目はアルファベット順にリストに挿入されます。 それ以外の場合、項目はリストの末尾に挿入されます。 リスト ボックスに特定の位置に項目を挿入するには、 Insert メソッドを使用します。 1 回の操作でリスト ボックスに項目のセットを追加するには、 AddRange メソッドを使用します。
Addメソッドを使用して多数の項目をリストに追加する場合は、BeginUpdateメソッドとEndUpdateメソッドを使用して、すべての項目がリストに追加されるまで、ListBoxがリストに追加されるたびに再描画されないようにします。
ListBoxに項目を追加する場合は、最初に項目を並べ替えてから新しい項目を追加する方が効率的です。
オブジェクトがコレクションに追加されると、ListBoxは最初に、ListControl クラスのDisplayMember プロパティに、項目のテキストを取得するときに参照するように指定されたオブジェクトのメンバーの名前があるかどうかを確認します。 DisplayMember プロパティにメンバーが指定されていない場合、ListBoxはオブジェクトのToString メソッドを呼び出して、リストに表示するテキストを取得します。