ListBox.Sorted プロパティ
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

/** @property */ public boolean get_Sorted () /** @property */ public void set_Sorted (boolean value)
コントロール内の項目を並べ替える場合は true。それ以外の場合は false。既定値は false です。

Sorted プロパティを使用すると、ListBox 内の文字列を自動的にアルファベット順に並べ替えることができます。並べ替えられた ListBox に項目を追加すると、追加された項目は並べ替えられたリスト内の適切な位置に移動されます。項目を ListBox に追加するときは、既存の項目を並べ替えてから新しい項目を追加した方が効率的です。
Sorted が true に設定された ListBox を、DataSource プロパティを使ってデータにバインドすることは避けてください。バインドした ListBox に、並べ替え済みのデータを表示する場合は、並べ替えをサポートするデータ ソースにバインドし、そのデータ ソース側で並べ替えを行う必要があります。

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

- ListBox.Sorted プロパティのページへのリンク