다음을 통해 공유


ListBox.Sorted 속성

ListBox의 항목이 사전순으로 정렬되는지 여부를 나타내는 값을 가져오거나 설정합니다.

네임스페이스: System.Windows.Forms
어셈블리: System.Windows.Forms(system.windows.forms.dll)

구문

‘선언
Public Property Sorted As Boolean
‘사용 방법
Dim instance As ListBox
Dim value As Boolean

value = instance.Sorted

instance.Sorted = value
public bool Sorted { get; set; }
public:
property bool Sorted {
    bool get ();
    void set (bool value);
}
/** @property */
public boolean get_Sorted ()

/** @property */
public void set_Sorted (boolean value)
public function get Sorted () : boolean

public function set Sorted (value : boolean)

속성 값

컨트롤의 항목이 정렬되어 있으면 true이고, 그렇지 않으면 false입니다. 기본값은 false입니다.

설명

ListBox에서 사전순으로 문자열을 자동 정렬하려면 Sorted 속성을 사용하십시오. 정렬된 ListBox에 항목을 추가하면 항목은 정렬된 목록에서 해당 위치로 이동합니다. ListBox에 항목을 추가할 때는 우선 기존 항목을 정렬한 다음 새 항목을 추가하는 것이 더욱 효과적입니다.

해당 Sortedtrue로 설정된 ListBoxDataSource 속성을 사용하여 데이터에 바인딩할 수 없습니다. 바인딩된 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에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

참고 항목

참조

ListBox 클래스
ListBox 멤버
System.Windows.Forms 네임스페이스