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

备注

使用 Sorted 属性可使 ListBox 中的字符串按字母顺序自动排序。在向已排序的 ListBox 添加项时,这些项会移动到排序列表中适当的位置。向 ListBox 添加项时,如果先对项进行排序,然后添加新项,则效率更高。

Sorted 设置为 trueListBox 不应通过 DataSource 属性绑定到数据。若要在绑定的 ListBox 中显示排序数据,应绑定到支持排序的数据源并使该数据源提供排序功能。

示例

下面的代码示例演示如何使用 GetSelected 方法来确定已选择了 ListBox 中的哪些项,以便选择那些尚未选定的项及取消选择那些已选定的项。该示例还演示了如何使用 SelectionMode 属性以使 ListBox 能够有多个选定的项,并使用 Sorted 属性演示了如何对 ListBox 中的项进行自动排序。此示例要求已将名为 listBox1ListBox 添加到窗体,并要求从该窗体的 Load 事件调用在该示例中定义的 InitializeMyListBox 方法。

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 命名空间