閱讀英文

共用方式為


SelectionMode 列舉

定義

指定清單方塊的選取行為。

C#
[System.Runtime.InteropServices.ComVisible(true)]
public enum SelectionMode
C#
public enum SelectionMode
繼承
SelectionMode
屬性

欄位

名稱 Description
MultiExtended 3

有多種項目可供選取,且使用者可以利用 SHIFT、CTRL 和方向鍵選取。

MultiSimple 2

可以選取多重項目。

None 0

沒有可選取項目。

One 1

只能選取一個項目。

範例

下列範例示範如何使用 GetSelected 方法來判斷 中 ListBox 選取的專案,以選取未選取的專案,並取消選取選取的專案。 此範例也會示範如何使用 SelectionMode 屬性讓 ListBox 擁有多個選取的專案,並使用 Sorted 屬性來示範如何自動排序專案中 ListBox 的專案。 這個範例假設 ListBoxlistBox1 名為 的 已加入表單,而且 InitializeMyListBox 範例中所定義的方法是從表單的 事件呼叫 Load

C#
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;
}

備註

這類類別會使用此列舉,例如 ListBoxCheckedListBox

適用於

產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

另請參閱