ListViewItem::Selected Property


Gets or sets a value indicating whether the item is selected.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

property bool Selected {
	bool get();
	void set(bool value);

Property Value

Type: System::Boolean

true if the item is selected; otherwise, false.

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. 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. You can access all items that are selected in a ListView control by using the ListView::SelectedItems property.


Items appear selected only when the ListView control has 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.

The following code example demonstrates how to use the ListView::Clear and Selected members. 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. Call the InitializeListView method from the form's constructor or Load event-handling method.

   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->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"};
      PopulateMenu( "Lunch", lunch, lunchPrices );
      Button1->Enabled = false;

.NET Framework
Available since 1.1
Return to top