ListBox クラスとは? わかりやすく解説

ListBox クラス

単一または複数の項目を選択できるリスト ボックス コントロール表します

名前空間: System.Web.UI.WebControls
アセンブリ: 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
/** @attribute ValidationPropertyAttribute("SelectedItem") */ 
public class ListBox extends ListControl implements
 IPostBackDataHandler
ValidationPropertyAttribute("SelectedItem") 
public class ListBox extends
 ListControl implements IPostBackDataHandler
解説解説

ListBox コントロール使用して単一または複数の項目を選択できるリスト コントロール作成しますコントロールの高さを指定するには、Rows プロパティ使用します複数項目の選択有効にするには、SelectionMode プロパティListSelectionMode.Multiple設定します

Items コレクション使用してListBox コントロールの ListItem オブジェクト調べます。たとえば、Items コレクション列挙処理し、各 ListItem 要素Selected 値をテストしてListBox コントロール選択された項目を確認できます

注意に関するメモ注意

このコントロールは、ユーザー入力表示するために使用できますユーザー入力には悪意のあるクライアント スクリプト含まれている可能性ありますアプリケーション表示する前にクライアントから送信され実行スクリプトSQL ステートメントなどのコード情報はすべて検証してください入力テキストコントロール表示する前に検証コントロール使用してユーザー入力検証できますASP.NET には入力要求検証機能があり、ユーザー入力の中のスクリプトおよび HTMLブロックできます詳細については、「標準コントロールセキュリティ保護」、「方法 : HTML エンコーディング文字列適用して Web アプリケーションスクリプトによる攻略から保護する」、および「ASP.NET Web ページにおけるユーザー入力検証」を参照してください

ユーザー補助

TopicLocation
チュートリアル : ASP.NET ユーザー コントロールによる再利用可能な要素作成Visual Studio での ASP .NET Web アプリケーション作成
チュートリアル : Visual Web Developer での ASP.NET マスタ ページ作成使用Visual Studio での ASP .NET Web アプリケーション作成
チュートリアル : カスタム ビジネス オブジェクトへのデータ バインディングVisual Studio での ASP .NET Web アプリケーション作成
方法 : ASP.NET Web サーバー コントロールフォーカス設定するASP .NET Web アプリケーション作成
方法 : Web フォーム ページListBox Web サーバー コントロール追加するASP .NET Web アプリケーション作成
方法 : Web フォーム ページListBox Web サーバー コントロール追加する (Visual Studio)Visual Studio での ASP .NET Web アプリケーション作成
方法 : データ ソースデータリスト Web サーバー コントロール読み込むASP .NET Web アプリケーション作成
方法 : データ ソースの項目をリスト Web サーバー コントロール読み込む (Visual Studio)Visual Studio での ASP .NET Web アプリケーション作成
方法 : リスト Web サーバー コントロールでの変更応答するASP .NET Web アプリケーション作成
方法 : リスト Web サーバー コントロールに項目を追加するASP .NET Web アプリケーション作成
方法 : リスト Web サーバー コントロールに項目を追加する (Visual Studio)Visual Studio での ASP .NET Web アプリケーション作成
方法 : リスト Web サーバー コントロール選択項目を確認するASP .NET Web アプリケーション作成
方法 : リスト Web サーバー コントロール選択項目を設定するASP .NET Web アプリケーション作成
方法 : リスト Web サーバー コントロール選択項目を設定する (Visual Studio)Visual Studio での ASP .NET Web アプリケーション作成
使用例使用例

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>
.NET Framework のセキュリティ.NET Frameworkセキュリティ
継承階層継承階層
System.Object
   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
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

ListBox クラス

項目のリスト表示する Windows コントロール表します

名前空間: System.Windows.Forms
アセンブリ: 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
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
public class ListBox extends ListControl
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
public class ListBox extends
 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 での使用方法

ListBox.ObjectCollection

ListBox コントロール格納されているすべての項目を格納します

ListBox.SelectedObjectCollection

選択されている項目のコレクション格納します。このコレクションは、ListBox コントロール格納されている項目のサブセットなります

ListBox.SelectedIndexCollection

選択されているインデックスコレクション格納します。このコレクションは、ListBox.ObjectCollectionインデックスサブセットなります。これらのインデックスは、選択されている項目を指定します

ListBox クラスサポートされ3 つのインデックス付きコレクション次の 3 つの例で示します

ListBox の項目がどのように ListBox.ObjectCollection格納されるかを示す例と、その ListBox 内での各項目の選択状態を次の表に示します

インデックス

項目

ListBox 内の選択状態

0

object1

選択されていない

1

object2

選択されている

2

object3

選択されていない

3

object4

選択されている

4

object5

選択されている

前の表の ListBox.ObjectCollection基づいた ListBox.SelectedObjectCollection内容次の表に示します

インデックス

項目

0

object2

1

object4

2

object5

前の表の ListBox.ObjectCollection基づいた ListBox.SelectedIndexCollection内容次の表に示します

インデックス

項目のインデックス

0

1

1

3

2

4

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.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.Control
         System.Windows.Forms.ListControl
          System.Windows.Forms.ListBox
             System.Windows.Forms.CheckedListBox
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からListBox クラスを検索した結果を表示しています。
Weblioに収録されているすべての辞書からListBox クラスを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からListBox クラス を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「ListBox クラス」の関連用語

ListBox クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ListBox クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS