ListViewItem.Selected 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置指示是否选定了项的值。
public:
property bool Selected { bool get(); void set(bool value); };
[System.ComponentModel.Browsable(false)]
public bool Selected { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.Selected : bool with get, set
Public Property Selected As Boolean
属性值
如果选定了项,则为 true
;否则为 false
。
- 属性
示例
下面的代码示例演示如何使用 ListView.Clear 和 Selected 成员。 若要运行该示例,请将以下代码放在包含 ListView 名为 ListView1
和 Button的窗体中,该窗体位于窗体底部,名为 Button1
。
InitializeListView
从窗体的构造函数或Load事件处理方法调用 方法。
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
注解
MultiSelect如果包含项的控件的 ListView 属性设置为 true
,则设置此属性的值会在所选项集中添加或删除该项。 如果 属性 MultiSelect 设置为 false
,将此属性的值设置为选择项将自动取消控件中任何其他项的选择 ListView 。 可以使用此属性来确定是否选择了某个项,或者在运行时选择一个项。 可以使用 属性访问控件ListView.SelectedItems中选择ListView的所有项。