ListBox クラス
アセンブリ: System.Web (system.web.dll 内)

<ValidationPropertyAttribute("SelectedItem")> _ Public Class ListBox Inherits ListControl Implements IPostBackDataHandler
[ValidationPropertyAttribute("SelectedItem")] public class ListBox : ListControl, IPostBackDataHandler
[ValidationPropertyAttribute(L"SelectedItem")] public ref class ListBox : public ListControl, IPostBackDataHandler

ListBox コントロールを使用して、単一または複数の項目を選択できるリスト コントロールを作成します。コントロールの高さを指定するには、Rows プロパティを使用します。複数項目の選択を有効にするには、SelectionMode プロパティを ListSelectionMode.Multiple に設定します。
Items コレクションを使用して、ListBox コントロールの ListItem オブジェクトを調べます。たとえば、Items コレクションを列挙処理し、各 ListItem 要素の Selected 値をテストして、ListBox コントロールの選択された項目を確認できます。
![]() |
---|
このコントロールは、ユーザー入力を表示するために使用できます。ユーザー入力には悪意のあるクライアント スクリプトが含まれている可能性があります。アプリケーションに表示する前に、クライアントから送信された実行スクリプト、SQL ステートメントなどのコードの情報はすべて検証してください。入力テキストをコントロールに表示する前に、検証コントロールを使用してユーザー入力を検証できます。ASP.NET には入力要求の検証機能があり、ユーザー入力の中のスクリプトおよび HTML をブロックできます。詳細については、「標準コントロールのセキュリティ保護」、「方法 : HTML エンコーディングを文字列に適用して Web アプリケーションをスクリプトによる攻略から保護する」、および「ASP.NET Web ページにおけるユーザー入力の検証」を参照してください。 |
ユーザー補助
このコントロールに既定でレンダリングされるマークアップは、Web Content Accessibility Guidelines (WCAG) 1.0 の優先度 1 ガイドラインなどのユーザー補助に関する標準に適合しない可能性があります。このコントロールのユーザー補助サポートの詳細については、「ASP.NET コントロールとユーザー補助」を参照してください。

ListBox コントロールを作成する方法を次の例に示します。
<%@ Page Language="VB" AutoEventWireup="True" %> <html> <head> <script language="VB" runat="server"> Sub SubmitBtn_Click(sender As Object, e As EventArgs) If ListBox1.SelectedIndex > - 1 Then Label1.Text = "You chose: " & ListBox1.SelectedItem.Text End If End Sub 'SubmitBtn_Click </script> </head> <body> <h3>ListBox Example</h3> <form runat=server> <asp:ListBox id="ListBox1" Rows="6" Width="100px" SelectionMode="Single" runat="server"> <asp:ListItem>Item 1</asp:ListItem> <asp:ListItem>Item 2</asp:ListItem> <asp:ListItem>Item 3</asp:ListItem> <asp:ListItem>Item 4</asp:ListItem> <asp:ListItem>Item 5</asp:ListItem> <asp:ListItem>Item 6</asp:ListItem> </asp:ListBox> <asp:button id="Button1" Text="Submit" OnClick="SubmitBtn_Click" runat="server" /> <asp:Label id="Label1" Font-Name="Verdana" Font-Size="10pt" runat="server"/> </form> </body> </html>
<%@ Page Language="C#" AutoEventWireup="True" %> <html> <head> <script language="C#" runat="server"> void SubmitBtn_Click(Object sender, EventArgs e) { if (ListBox1.SelectedIndex > -1) Label1.Text="You chose: " + ListBox1.SelectedItem.Text; } </script> </head> <body> <h3>ListBox Example</h3> <form runat=server> <asp:ListBox id="ListBox1" Rows="6" Width="100px" SelectionMode="Single" runat="server"> <asp:ListItem>Item 1</asp:ListItem> <asp:ListItem>Item 2</asp:ListItem> <asp:ListItem>Item 3</asp:ListItem> <asp:ListItem>Item 4</asp:ListItem> <asp:ListItem>Item 5</asp:ListItem> <asp:ListItem>Item 6</asp:ListItem> </asp:ListBox> <asp:button id="Button1" Text="Submit" OnClick="SubmitBtn_Click" runat="server" /> <asp:Label id="Label1" Font-Name="Verdana" Font-Size="10pt" runat="server"/> </form> </body> </html>
データ連結により ListBox コントロールを作成する方法を次の例に示します。
<%@ Page Language="VB" AutoEventWireup="True" %> <html> <head> <script language="VB" runat="server"> Sub Page_Load(sender As Object, e As EventArgs) If Not IsPostBack Then Dim values As New ArrayList() values.Add("Item 1") values.Add("Item 2") values.Add("Item 3") values.Add("Item 4") values.Add("Item 5") values.Add("Item 6") ListBox1.DataSource = values ListBox1.DataBind() End If End Sub 'Page_Load Sub SubmitBtn_Click(sender As Object, e As EventArgs) If ListBox1.SelectedIndex > - 1 Then Label1.Text = "You chose: " & ListBox1.SelectedItem.Text End If End Sub 'SubmitBtn_Click </script> </head> <body> <form runat=server> <h3>Data Binding ListBox</h3> <asp:ListBox id="ListBox1" Width="100px" runat="server"/> <asp:button id="Button1" Text="Submit" OnClick="SubmitBtn_Click" runat="server" /> <asp:Label id="Label1" font-name="Verdana" font-size="10pt" runat="server"/> </form> </body> </html>
<%@ Page Language="C#" AutoEventWireup="True" %> <html> <head> <script language="C#" runat="server"> void Page_Load(Object sender, EventArgs e) { if (!IsPostBack) { ArrayList values = new ArrayList(); values.Add ("Item 1"); values.Add ("Item 2"); values.Add ("Item 3"); values.Add ("Item 4"); values.Add ("Item 5"); values.Add ("Item 6"); ListBox1.DataSource = values; ListBox1.DataBind(); } } void SubmitBtn_Click(Object sender, EventArgs e) { if ( ListBox1.SelectedIndex > -1 ) Label1.Text = "You chose: " + ListBox1.SelectedItem.Text; } </script> </head> <body> <form runat=server> <h3>Data Binding ListBox</h3> <asp:ListBox id="ListBox1" Width="100px" runat="server"/> <asp:button id="Button1" Text="Submit" OnClick="SubmitBtn_Click" runat="server" /> <asp:Label id="Label1" font-name="Verdana" font-size="10pt" runat="server"/> </form> </body> </html>


System.Web.UI.Control
System.Web.UI.WebControls.WebControl
System.Web.UI.WebControls.BaseDataBoundControl
System.Web.UI.WebControls.DataBoundControl
System.Web.UI.WebControls.ListControl
System.Web.UI.WebControls.ListBox


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


ListBox クラス
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

<ComVisibleAttribute(True)> _ <ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _ Public Class ListBox Inherits ListControl
[ComVisibleAttribute(true)] [ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] public class ListBox : ListControl
[ComVisibleAttribute(true)] [ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] public ref class ListBox : public ListControl

ListBox コントロールを使用すると、ユーザーがクリックして選択できる項目のリストを表示できます。SelectionMode プロパティを使用すると、ListBox コントロールを単一選択または複数選択のリスト ボックスにすることができます。ListBox には、項目を縦に並べたリストを表示するのではなく、複数の列に項目を表示できるようにする MultiColumn プロパティも用意されています。これにより、コントロールにより多くの項目を表示できるため、項目を表示するためにスクロールする手間を省くことができます。
通常、ListBox に表示される項目を描画するタスクは Windows によって処理されます。DrawMode プロパティを使用し、MeasureItem イベントと DrawItem イベントを処理することで、Windows が提供する自動描画をオーバーライドし、項目を独自に描画できます。オーナー描画 ListBox コントロールを使用すると、高さが可変の項目やイメージを表示したり、リスト内の各項目のテキストに異なる色やフォントを適用したりできます。HorizontalExtent プロパティ、GetItemHeight、および GetItemRectangle も、独自の項目を描画するときに役立ちます。
表示機能および選択機能の他に、ListBox には、ListBox に項目を効率よく追加したり、リストの項目にあるテキストを検索したりできる機能も用意されています。BeginUpdate メソッドおよび EndUpdate メソッドを使用すると、リストに項目を追加するたびにコントロールを再描画せずに ListBox に多数の項目を追加できます。FindString メソッドおよび FindStringExact メソッドを使用すると、特定の検索文字列を含むリスト内の項目を検索できます。
Items、SelectedItems、SelectedIndices の各プロパティを使用すると、ListBox によって使用される 3 つのコレクションにアクセスできます。ListBox によって使用される 3 つのコレクションとコントロール内でのそれらの使用方法について次の表で説明します。
ListBox.ObjectCollection | |
ListBox.SelectedObjectCollection | 選択されている項目のコレクションを格納します。このコレクションは、ListBox コントロールに格納されている項目のサブセットになります。 |
ListBox.SelectedIndexCollection | 選択されているインデックスのコレクションを格納します。このコレクションは、ListBox.ObjectCollection のインデックスのサブセットになります。これらのインデックスは、選択されている項目を指定します。 |
ListBox クラスでサポートされる 3 つのインデックス付きコレクションを次の 3 つの例で示します。
ListBox の項目がどのように ListBox.ObjectCollection に格納されるかを示す例と、その ListBox 内での各項目の選択状態を次の表に示します。
前の表の ListBox.ObjectCollection に基づいた ListBox.SelectedObjectCollection の内容を次の表に示します。
項目 | |
---|---|
0 | object2 |
1 | object4 |
2 | object5 |
前の表の ListBox.ObjectCollection に基づいた ListBox.SelectedIndexCollection の内容を次の表に示します。
ListBox.ObjectCollection クラスの Add メソッドを使用すると、項目を ListBox に追加できます。Add メソッドは、ListBox にメンバを追加するときに、任意のオブジェクトを受け入れます。オブジェクトが ListBox に追加されるときに、そのオブジェクト内のメンバの名前が DisplayMember プロパティに指定されていない限り、コントロールはオブジェクトの ToString メソッドで定義されたテキストを使用します。ListBox.ObjectCollection クラスの Add メソッドを使用して項目を追加する他に、ListControl クラスの DataSource プロパティを使用しても項目を追加できます。

複数の項目を列に表示し、リスト内で複数の項目を選択できる ListBox コントロールを作成する方法を次のコード例に示します。このサンプル コードは、ListBox.ObjectCollection クラスの Add メソッドを使用して 50 の項目を ListBox に追加し、SetSelected メソッドを使用してリストから 3 つの項目を選択します。次に、ListBox.SelectedObjectCollection コレクションの値 (SelectedItems プロパティを使用) と ListBox.SelectedIndexCollection の値 (SelectedIndices プロパティを使用) を表示します。この例では、コードが Form に含まれ、ここから呼び出される必要があります。
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
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()); }
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 ); // 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 ] ); }
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.set_Size(new System.Drawing.Size(200,100)); listBox1.set_Location(new System.Drawing.Point(10,10)); // Add the ListBox to the form. this.get_Controls().Add(listBox1); // Set the ListBox to display items in multiple columns. listBox1.set_MultiColumn(true); // Set the selection mode to multiple and extended. listBox1.set_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.get_Items().Add(("Item" + (new Integer(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.get_SelectedItems().get_Item(1).ToString()); // Display the index of the first selected item in the ListBox. System.Diagnostics.Debug.WriteLine((new Integer (listBox1.get_SelectedIndices().get_Item(0))).ToString()); } //button1_Click
private function button1_Click(sender : Object, e : System.EventArgs) { // Create an instance of the ListBox. var listBox1 : ListBox = 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 (var x : int = 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()); }

System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ListControl
System.Windows.Forms.ListBox
System.Windows.Forms.CheckedListBox


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


Weblioに収録されているすべての辞書からListBox クラスを検索する場合は、下記のリンクをクリックしてください。

- ListBox クラスのページへのリンク