ListViewItem.Selected 속성

정의

항목이 선택되어 있는지 여부를 나타내는 값을 가져오거나 설정합니다.Gets or sets a value indicating whether the item is selected.

public:
 property bool Selected { bool get(); void set(bool value); };
[System.ComponentModel.Browsable(false)]
public bool Selected { get; set; }
member this.Selected : bool with get, set
Public Property Selected As Boolean

속성 값

해당 항목이 선택되면 true이고, 그렇지 않으면 false입니다.true if the item is selected; otherwise, false.

특성

예제

다음 코드 예제에서는 ListView.ClearSelected 멤버를 사용 하는 방법을 보여 줍니다.The following code example demonstrates how to use the ListView.Clear and Selected members. 예제를 실행 하려면 Button1이라는 폼의 맨 아래에 있는 ListView1Button라는 ListView 포함 된 폼에 다음 코드를 배치 합니다.To run the example, place the following code in a form containing a ListView named ListView1 and a Button, located toward the bottom of the form, named Button1. 폼의 생성자 또는 Load 이벤트 처리 메서드에서 InitializeListView 메서드를 호출 합니다.Call the InitializeListView method from the form's constructor or Load event-handling method.

private:
   void InitializeListView()
   {
      // Set up the inital values for the ListView and populate it.
      this->ListView1 = gcnew ListView;
      this->ListView1->Dock = DockStyle::Top;
      this->ListView1->Location = System::Drawing::Point( 0, 0 );
      this->ListView1->Size = System::Drawing::Size( 292, 130 );
      this->ListView1->View = View::Details;
      this->ListView1->FullRowSelect = true;
      array<String^>^breakfast = {"Continental Breakfast","Pancakes and Sausage","Denver Omelet","Eggs & Bacon","Bagel & Cream Cheese"};
      array<String^>^breakfastPrices = {"3.09","4.09","4.19","4.79","2.09"};
      PopulateMenu( "Breakfast", breakfast, breakfastPrices );
   }

   void PopulateMenu( String^ meal, array<String^>^menuItems, array<String^>^menuPrices )
   {
      ColumnHeader^ columnHeader1 = gcnew ColumnHeader;
      columnHeader1->Text = String::Concat( meal, " Choices" );
      columnHeader1->TextAlign = HorizontalAlignment::Left;
      columnHeader1->Width = 146;
      ColumnHeader^ columnHeader2 = gcnew ColumnHeader;
      columnHeader2->Text = "Price";
      columnHeader2->TextAlign = HorizontalAlignment::Center;
      columnHeader2->Width = 142;
      this->ListView1->Columns->Add( columnHeader1 );
      this->ListView1->Columns->Add( columnHeader2 );
      for ( int count = 0; count < menuItems->Length; count++ )
      {
         ListViewItem^ listItem = gcnew ListViewItem( menuItems[ count ] );
         listItem->SubItems->Add( menuPrices[ count ] );
         ListView1->Items->Add( listItem );

      }
      
      // Use the Selected property to select the first item on 
      // the list.
      ListView1->Focus();
      ListView1->Items[ 0 ]->Selected = true;
   }

   void Button1_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Create new values for the ListView, clear the list, 
      // and repopulate it.
      array<String^>^lunch = {"Hamburger","Grilled Cheese","Soup & Salad","Club Sandwich","Hotdog"};
      array<String^>^lunchPrices = {"4.09","5.09","5.19","4.79","2.09"};
      ListView1->Clear();
      PopulateMenu( "Lunch", lunch, lunchPrices );
      Button1->Enabled = false;
   }
private void InitializeListView()
{
    // Set up the inital values for the ListView and populate it.
    this.ListView1 = new ListView();
    this.ListView1.Dock = DockStyle.Top;
    this.ListView1.Location = new System.Drawing.Point(0, 0);
    this.ListView1.Size = new System.Drawing.Size(292, 130);
    this.ListView1.View = View.Details;
    this.ListView1.FullRowSelect = true;

    string[] breakfast = new string[]{"Continental Breakfast", 
        "Pancakes and Sausage", "Denver Omelet", "Eggs & Bacon", 
        "Bagel & Cream Cheese"};

    string[] breakfastPrices = new string[]{"3.09", "4.09", 
        "4.19", "4.79", "2.09"};

    PopulateMenu("Breakfast", breakfast, breakfastPrices);
}

private void PopulateMenu(string meal, 
    string[] menuItems, string[] menuPrices)
{
    ColumnHeader columnHeader1 = new ColumnHeader();
    columnHeader1.Text = meal + " Choices";
    columnHeader1.TextAlign = HorizontalAlignment.Left;
    columnHeader1.Width = 146;

    ColumnHeader columnHeader2 = new ColumnHeader();
    columnHeader2.Text = "Price";
    columnHeader2.TextAlign = HorizontalAlignment.Center;
    columnHeader2.Width = 142;

    this.ListView1.Columns.Add(columnHeader1);
    this.ListView1.Columns.Add(columnHeader2);

    for(int count=0; count < menuItems.Length; count++)
    {
        ListViewItem listItem = 
            new ListViewItem(menuItems[count]);
        listItem.SubItems.Add(menuPrices[count]);
        ListView1.Items.Add(listItem);
    }

    // Use the Selected property to select the first item on 
    // the list.
    ListView1.Focus();
    ListView1.Items[0].Selected = true;
}

private void Button1_Click(System.Object sender, System.EventArgs e)
{
    // Create new values for the ListView, clear the list, 
    // and repopulate it.
    string[] lunch = new string[]{"Hamburger", "Grilled Cheese",
        "Soup & Salad", "Club Sandwich", "Hotdog"};

    string[] lunchPrices = new string[]{"4.09", "5.09", "5.19", 
        "4.79", "2.09"};

    ListView1.Clear();

    PopulateMenu("Lunch", lunch, lunchPrices);
    Button1.Enabled = false;
}
Private Sub InitializeListView()

    ' Set up the inital values for the ListView and populate it.
    Me.ListView1 = New ListView
    Me.ListView1.Dock = DockStyle.Top
    Me.ListView1.Location = New System.Drawing.Point(0, 0)
    Me.ListView1.Size = New System.Drawing.Size(292, 130)
    Me.ListView1.View = View.Details
    Me.ListView1.FullRowSelect = True

    Dim breakfast() As String = New String() {"Continental Breakfast", "Pancakes and Sausage", _
   "Denver Omelet", "Eggs & Bacon", "Bagel & Cream Cheese"}

    Dim breakfastPrices() As String = New String() {"3.09", "4.09", "4.19", _
       "4.79", "2.09"}

    PopulateMenu("Breakfast", breakfast, breakfastPrices)
End Sub

Private Sub PopulateMenu(ByVal meal As String, _
    ByVal menuItems() As String, ByVal menuPrices() As String)
    Dim columnHeader1 As New ColumnHeader
    With columnHeader1
        .Text = meal & " Choices"
        .TextAlign = HorizontalAlignment.Left
        .Width = 146
    End With
    Dim columnHeader2 As New ColumnHeader
    With columnHeader2
        .Text = "Price"
        .TextAlign = HorizontalAlignment.Center
        .Width = 142
    End With
    Me.ListView1.Columns.Add(columnHeader1)
    Me.ListView1.Columns.Add(columnHeader2)

    Dim count As Integer

    For count = 0 To menuItems.Length - 1
        Dim listItem As New ListViewItem(menuItems(count))
        listItem.SubItems.Add(menuPrices(count))
        ListView1.Items.Add(listItem)
    Next

    ' Use the Selected property to select the first item on 
    ' the list.
    ListView1.Focus()
    ListView1.Items(0).Selected = True
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click

    ' Create new values for the ListView, clear the list, 
    ' and repopulate it.
    Dim lunch() As String = New String() {"Hamburger", _ 
        "Grilled Cheese", "Soup & Salad", "Club Sandwich", "Hotdog"}

    Dim lunchPrices() As String = New String() {"4.09", "5.09", _
        "5.19", "4.79", "2.09"}

    ListView1.Clear()

    PopulateMenu("Lunch", lunch, lunchPrices)
    Button1.Enabled = False
End Sub

설명

항목이 포함 된 ListView 컨트롤의 MultiSelect 속성이 true로 설정 된 경우이 속성의 값을 설정 하면 선택한 항목 집합에서 해당 항목이 추가 되거나 제거 됩니다.If the MultiSelect property of the ListView control the item is contained in is set to true, setting the value of this property adds or removes the item from the set of selected items. MultiSelect 속성이 false로 설정 된 경우이 속성의 값을 설정 하 여 항목을 선택 하면 ListView 컨트롤의 다른 항목에 대 한 선택 항목이 자동으로 취소 됩니다.If the MultiSelect property is set to false, setting the value of this property to select an item automatically cancels the selection for any other items in the ListView control. 항목을 선택 하는 경우를 확인 하거나 런타임 시 항목을 선택 하려면이 속성을 사용할 수 있습니다.You can use this property to determine if an item is selected or to select an item at run time. ListView.SelectedItems 속성을 사용 하 여 ListView 컨트롤에서 선택 된 모든 항목에 액세스할 수 있습니다.You can access all items that are selected in a ListView control by using the ListView.SelectedItems property.

참고

ListView 컨트롤에 포커스가 있는 경우에만 항목이 선택 된 것으로 나타납니다.Items appear selected only when the ListView control has focus. 단추 클릭과 같은 사용자 동작에 대 한 응답으로 항목을 선택 하려면이 속성을 설정 하는 것 외에도 Focus 메서드를 호출 해야 합니다.To select items in response to a user action such as a button click, be sure to call the Focus method in addition to setting this property.

적용 대상

추가 정보