ListBox.SelectionMode プロパティ
アセンブリ: System.Web (system.web.dll 内)

Dim instance As ListBox Dim value As ListSelectionMode value = instance.SelectionMode instance.SelectionMode = value
public: virtual property ListSelectionMode SelectionMode { ListSelectionMode get (); void set (ListSelectionMode value); }
/** @property */ public ListSelectionMode get_SelectionMode () /** @property */ public void set_SelectionMode (ListSelectionMode value)
public function get SelectionMode () : ListSelectionMode public function set SelectionMode (value : ListSelectionMode)
ListSelectionMode 値の 1 つ。既定値は Single です。


SelectionMode プロパティを使用して、ListBox コントロールのモード動作を指定します。このプロパティを ListSelectionMode.Single に設定した場合、ListBox コントロールで選択できるのは 1 つの項目だけですが、ListSelectionMode.Multiple に設定すると複数の項目を選択できます。
SelectionMode プロパティの値はビューステートに格納されます。

SelectionMode プロパティを使用して、ユーザーが ListBox コントロールで複数の項目を選択できるようにする方法を次の例に示します。
<%@ Page Language="VB" AutoEventWireup="True" %> <html> <head> <script runat="server"> Sub SubmitBtn_Click(sender As Object, e As EventArgs) Message.Text = "You chose: <br>" ' Iterate through the Items collection of the ListBox and ' display the selected items. Dim item As ListItem For Each item in ListBox1.Items If item.Selected Then Message.Text &= item.Text & "<br>" End If Next End Sub </script> </head> <body> <h3>ListBox Example</h3> <form runat=server> Select items from the list and click Submit. <br> <asp:ListBox id="ListBox1" Rows="6" Width="100px" SelectionMode="Multiple" runat="server"> <asp:ListItem Selected="True">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> <br><br> <asp:button id="Button1" Text="Submit" OnClick="SubmitBtn_Click" runat="server" /> <br><br> <asp:Label id="Message" runat="server"/> </form> </body> </html>
<%@ Page Language="C#" AutoEventWireup="True" %> <html> <head> <script runat="server"> void SubmitBtn_Click(Object sender, EventArgs e) { Message.Text = "You chose: <br>"; // Iterate through the Items collection of the ListBox and // display the selected items. foreach (ListItem item in ListBox1.Items) { if(item.Selected) { Message.Text += item.Text + "<br>"; } } } </script> </head> <body> <h3>ListBox Example</h3> <form runat=server> Select items from the list and click Submit. <br> <asp:ListBox id="ListBox1" Rows="6" Width="100px" SelectionMode="Multiple" runat="server"> <asp:ListItem Selected="True">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> <br><br> <asp:button id="Button1" Text="Submit" OnClick="SubmitBtn_Click" runat="server" /> <br><br> <asp:Label id="Message" runat="server"/> </form> </body> </html>

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.SelectionMode プロパティ
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Dim instance As ListBox Dim value As SelectionMode value = instance.SelectionMode instance.SelectionMode = value
public: virtual property SelectionMode SelectionMode { SelectionMode get (); void set (SelectionMode value); }
/** @property */ public SelectionMode get_SelectionMode () /** @property */ public void set_SelectionMode (SelectionMode value)
public function get SelectionMode () : SelectionMode public function set SelectionMode (value : SelectionMode)
SelectionMode 値の 1 つ。既定値は SelectionMode.One です。


SelectionMode プロパティを使用すると、ユーザーが ListBox 内で一度に選択できる項目数や、複数の項目を選択する方法を指定できます。SelectionMode プロパティが SelectionMode.MultiExtended に設定されている場合は、Shift キーを押しながらマウスをクリックするか、Shift キーを押しながら方向キー (↑、↓、←、および→) の 1 つを押すと、前に選択した項目から現在選択している項目までを選択できます。Ctrl キーを押しながらマウスをクリックすると、リスト内の項目を選択または選択解除できます。このプロパティが SelectionMode.MultiSimple に設定されている場合は、マウスをクリックするか、Space キーを押すと、リスト内の項目を選択または選択解除できます。

GetSelected メソッドを使用して ListBox 内のどの項目が選択されているかを判別し、現在選択されていない項目を選択して、現在選択されている項目の選択を解除する方法を次のコード例に示します。この例では、SelectionMode プロパティを使用して ListBox で複数の項目の選択を許可する方法と、Sorted プロパティを使用して ListBox 内の項目を自動的に並べ替える方法についても示します。この例では、listBox1 という名前の ListBox がフォームに追加されており、この例で定義されている InitializeMyListBox メソッドがフォームの Load イベントから呼び出される必要があります。
Private Sub InitializeMyListBox() ' Add items to the ListBox. listBox1.Items.Add("A") listBox1.Items.Add("C") listBox1.Items.Add("E") listBox1.Items.Add("F") listBox1.Items.Add("G") listBox1.Items.Add("D") listBox1.Items.Add("B") ' Sort all items added previously. listBox1.Sorted = True ' Set the SelectionMode to select multiple items. listBox1.SelectionMode = SelectionMode.MultiExtended ' Select three initial items from the list. listBox1.SetSelected(0, True) listBox1.SetSelected(2, True) listBox1.SetSelected(4, True) ' Force the ListBox to scroll back to the top of the list. listBox1.TopIndex = 0 End Sub Private Sub InvertMySelection() Dim x As Integer ' Loop through all items the ListBox. For x = 0 To listBox1.Items.Count - 1 ' Determine if the item is selected. If listBox1.GetSelected(x) = True Then ' Deselect all items that are selected. listBox1.SetSelected(x, False) Else ' Select all items that are not selected. listBox1.SetSelected(x, True) End If Next x ' Force the ListBox to scroll back to the top of the list. listBox1.TopIndex = 0 End Sub
private void InitializeMyListBox() { // Add items to the ListBox. listBox1.Items.Add("A"); listBox1.Items.Add("C"); listBox1.Items.Add("E"); listBox1.Items.Add("F"); listBox1.Items.Add("G"); listBox1.Items.Add("D"); listBox1.Items.Add("B"); // Sort all items added previously. listBox1.Sorted = true; // Set the SelectionMode to select multiple items. listBox1.SelectionMode = SelectionMode.MultiExtended; // Select three initial items from the list. listBox1.SetSelected(0,true); listBox1.SetSelected(2,true); listBox1.SetSelected(4,true); // Force the ListBox to scroll back to the top of the list. listBox1.TopIndex=0; } private void InvertMySelection() { // Loop through all items the ListBox. for (int x = 0; x < listBox1.Items.Count; x++) { // Determine if the item is selected. if(listBox1.GetSelected(x) == true) // Deselect all items that are selected. listBox1.SetSelected(x,false); else // Select all items that are not selected. listBox1.SetSelected(x,true); } // Force the ListBox to scroll back to the top of the list. listBox1.TopIndex=0; }
private: void InitializeMyListBox() { // Add items to the ListBox. listBox1->Items->Add( "A" ); listBox1->Items->Add( "C" ); listBox1->Items->Add( "E" ); listBox1->Items->Add( "F" ); listBox1->Items->Add( "G" ); listBox1->Items->Add( "D" ); listBox1->Items->Add( "B" ); // Sort all items added previously. listBox1->Sorted = true; // Set the SelectionMode to select multiple items. listBox1->SelectionMode = SelectionMode::MultiExtended; // Select three initial items from the list. listBox1->SetSelected( 0, true ); listBox1->SetSelected( 2, true ); listBox1->SetSelected( 4, true ); // Force the ListBox to scroll back to the top of the list. listBox1->TopIndex = 0; } void InvertMySelection() { // Loop through all items the ListBox. for ( int x = 0; x < listBox1->Items->Count; x++ ) { // Select all items that are not selected, // deselect all items that are selected. listBox1->SetSelected( x, !listBox1->GetSelected( x ) ); } listBox1->TopIndex = 0; }
private void InitializeMyListBox() { // Add items to the ListBox. listBox1.get_Items().Add("A"); listBox1.get_Items().Add("C"); listBox1.get_Items().Add("E"); listBox1.get_Items().Add("F"); listBox1.get_Items().Add("G"); listBox1.get_Items().Add("D"); listBox1.get_Items().Add("B"); // Sort all items added previously. listBox1.set_Sorted(true); // Set the SelectionMode to select multiple items. listBox1.set_SelectionMode(SelectionMode.MultiExtended); // Select three initial items from the list. listBox1.SetSelected(0, true); listBox1.SetSelected(2, true); listBox1.SetSelected(4, true); // Force the ListBox to scroll back to the top of the list. listBox1.set_TopIndex(0); } //InitializeMyListBox private void InvertMySelection() { // Loop through all items the ListBox. for (int x = 0; x < listBox1.get_Items().get_Count(); x++) { // Determine if the item is selected. if (listBox1.GetSelected(x) == true) { // Deselect all items that are selected. listBox1.SetSelected(x, false); } else { // Select all items that are not selected. listBox1.SetSelected(x, true); } } // Force the ListBox to scroll back to the top of the list. listBox1.set_TopIndex(0); } //InvertMySelection

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.SelectionModeを検索する場合は、下記のリンクをクリックしてください。

- ListBox.SelectionModeのページへのリンク