CheckedListBox.DisplayMember 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定字串,該字串指定所要顯示清單方塊中所包含物件的屬性。
public:
property System::String ^ DisplayMember { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.Browsable(false)]
public string DisplayMember { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.DisplayMember : string with get, set
Public Property DisplayMember As String
屬性值
字串,指定清單方塊中所包含物件的屬性名稱。 預設為空字串 ("")。
- 屬性
範例
下列程式碼範例示範如何藉由設定 CheckOnClick 、 SelectionMode 和 ThreeDCheckBoxes 屬性來初始化 CheckedListBox 控制項。 此範例會以 CheckedListBox 控制項填入 ,並將 設定 DisplayMember 為 Control.Name 控制項的 屬性。
若要執行此範例,請將下列程式碼貼到包含 CheckedListBox 具名 CheckedListBox1
的表單中,並從表單的建構函式或 Load 方法呼叫 InitializeCheckListBox
方法。
// This method initializes CheckedListBox1 with a list of all
// the controls on the form. It sets the selection mode
// to single selection and allows selection with a single click.
// It adds itself to the list before adding itself to the form.
internal:
System::Windows::Forms::CheckedListBox^ CheckedListBox1;
private:
void InitializeCheckedListBox()
{
this->CheckedListBox1 = gcnew CheckedListBox;
this->CheckedListBox1->Location = System::Drawing::Point( 40, 90 );
this->CheckedListBox1->CheckOnClick = true;
this->CheckedListBox1->Name = "CheckedListBox1";
this->CheckedListBox1->Size = System::Drawing::Size( 120, 94 );
this->CheckedListBox1->TabIndex = 1;
this->CheckedListBox1->SelectionMode = SelectionMode::One;
this->CheckedListBox1->ThreeDCheckBoxes = true;
System::Collections::IEnumerator^ myEnum = this->Controls->GetEnumerator();
while ( myEnum->MoveNext() )
{
Control^ aControl = safe_cast<Control^>(myEnum->Current);
this->CheckedListBox1->Items->Add( aControl, false );
}
this->CheckedListBox1->DisplayMember = "Name";
this->CheckedListBox1->Items->Add( CheckedListBox1 );
this->Controls->Add( this->CheckedListBox1 );
}
// This method initializes CheckedListBox1 with a list of all
// the controls on the form. It sets the selection mode
// to single selection and allows selection with a single click.
// It adds itself to the list before adding itself to the form.
internal System.Windows.Forms.CheckedListBox CheckedListBox1;
private void InitializeCheckedListBox()
{
this.CheckedListBox1 = new CheckedListBox();
this.CheckedListBox1.Location = new System.Drawing.Point(40, 90);
this.CheckedListBox1.CheckOnClick = true;
this.CheckedListBox1.Name = "CheckedListBox1";
this.CheckedListBox1.Size = new System.Drawing.Size(120, 94);
this.CheckedListBox1.TabIndex = 1;
this.CheckedListBox1.SelectionMode = SelectionMode.One;
this.CheckedListBox1.ThreeDCheckBoxes = true;
foreach ( Control aControl in this.Controls )
{
this.CheckedListBox1.Items.Add(aControl, false);
}
this.CheckedListBox1.DisplayMember = "Name";
this.CheckedListBox1.Items.Add(CheckedListBox1);
this.Controls.Add(this.CheckedListBox1);
}
' This method initializes CheckedListBox1 with a list of all the controls
' on the form. It sets the selection mode to single selection and
' allows selection with a single click. It adds itself to the list before
' adding itself to the form.
Friend WithEvents CheckedListBox1 As System.Windows.Forms.CheckedListBox
Private Sub InitializeCheckedListBox()
Me.CheckedListBox1 = New CheckedListBox
Me.CheckedListBox1.Location = New System.Drawing.Point(40, 90)
Me.CheckedListBox1.CheckOnClick = True
Me.CheckedListBox1.Name = "CheckedListBox1"
Me.CheckedListBox1.Size = New System.Drawing.Size(120, 94)
Me.CheckedListBox1.TabIndex = 1
Me.CheckedListBox1.SelectionMode = SelectionMode.One
Me.CheckedListBox1.ThreeDCheckBoxes = True
Dim aControl As Control
For Each aControl In Me.Controls
Me.CheckedListBox1.Items.Add(aControl, False)
Next
Me.CheckedListBox1.DisplayMember = "Name"
Me.CheckedListBox1.Items.Add(CheckedListBox1)
Me.Controls.Add(Me.CheckedListBox1)
End Sub
備註
根據預設, CheckedListBox 會顯示其包含之專案的預設字串。 不過, CheckedListBox 可以顯示各種不同的物件類型,而且您可以藉由指定 DisplayMember 屬性來變更顯示的字串。 如果指定的屬性不存在,或屬性的值 DisplayMember 是空字串 (「」「) ,則會改為顯示物件方法 ToString 的結果。
如果無法設定 的新值 DisplayMember ,則會維護先前的值。