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控件填充并设置DisplayMemberControl.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 ,则保留上一个值。