ListControl.DisplayMember 屬性

定義

取得或設定要針對這個 ListControl 顯示的屬性。

public:
 property System::String ^ DisplayMember { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string DisplayMember { get; set; }
[System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string DisplayMember { get; set; }
[System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string DisplayMember { get; set; }
[System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Windows.Forms.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string DisplayMember { get; set; }
[<System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]
member this.DisplayMember : string with get, set
[<System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]
member this.DisplayMember : string with get, set
[<System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]
member this.DisplayMember : string with get, set
[<System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Windows.Forms.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]
member this.DisplayMember : string with get, set
Public Property DisplayMember As String

屬性值

String,指定物件屬性的名稱,而此物件屬性包含在 DataSource 屬性指定的集合中。 預設為空字串 ("")。

屬性

範例

下列程式碼範例是完整的應用程式,示範如何使用 、、 和 類別的成員 ListControl ,如 類別所實作 ListBoxSelectedValueValueMemberDisplayMemberDataSource 此範例會載入 ArrayList 和 清單方塊。 當使用者在清單方塊中選取專案時,會使用選取的值來傳回與選取專案相關聯的資料。

#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Windows::Forms;
using namespace System::Drawing;
using namespace System::Collections;
public ref class USState
{
private:
   String^ myShortName;
   String^ myLongName;

public:
   USState( String^ strLongName, String^ strShortName )
   {
      this->myShortName = strShortName;
      this->myLongName = strLongName;
   }

   property String^ ShortName 
   {
      String^ get()
      {
         return myShortName;
      }
   }

   property String^ LongName 
   {
      String^ get()
      {
         return myLongName;
      }

   }
};

public ref class ListBoxSample3: public Form
{
private:
   ListBox^ ListBox1;
   Label^ label1;
   TextBox^ textBox1;

public:
   ListBoxSample3()
   {
      ListBox1 = gcnew ListBox;
      label1 = gcnew Label;
      textBox1 = gcnew TextBox;
      this->ClientSize = System::Drawing::Size(307, 206 );
      this->Text = "ListBox Sample3";
      ListBox1->Location = Point(54,16);
      ListBox1->Name = "ListBox1";
      ListBox1->Size = System::Drawing::Size( 240, 130 );
      label1->Location = Point(14,150);
      label1->Name = "label1";
      label1->Size = System::Drawing::Size(40, 24);
      label1->Text = "Value";
      textBox1->Location = Point(54,150);
      textBox1->Name = "textBox1";
      textBox1->Size = System::Drawing::Size( 240, 24 );
      array<Control^>^temp2 = {ListBox1,label1, textBox1};
      this->Controls->AddRange( temp2 );

      // Populate the list box using an array as DataSource. 
      // DisplayMember is used to display just the long name of each state.
      ArrayList^ USStates = gcnew ArrayList;
      USStates->Add( gcnew USState( "Alabama","AL" ) );
      USStates->Add( gcnew USState( "Washington","WA" ) );
      USStates->Add( gcnew USState( "West Virginia","WV" ) );
      USStates->Add( gcnew USState( "Wisconsin","WI" ) );
      USStates->Add( gcnew USState( "Wyoming","WY" ) );
      ListBox1->DataSource = USStates;
      ListBox1->DisplayMember = "LongName";
      ListBox1->ValueMember = "ShortName";
      ListBox1->SelectedValueChanged += gcnew EventHandler( this, &ListBoxSample3::ListBox1_SelectedValueChanged );
      ListBox1->SetSelected(0, false);
   }

   void InitializeComponent(){}

private:
   void ListBox1_SelectedValueChanged( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      textBox1->Text="";
      if ( ListBox1->SelectedIndex != -1 )
            textBox1->Text = ListBox1->SelectedValue->ToString();
   }
};

[STAThread]
int main()
{
   Application::Run( gcnew ListBoxSample3 );
}
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;

namespace MyListControlSample
{
    public class ListBoxSample3 : Form
    {
        private ListBox ListBox1 = new ListBox();
        private Label label1 = new Label();
        private TextBox textBox1 = new TextBox();

        [STAThread]
        static void Main()
        {
            Application.Run(new ListBoxSample3());
        }

        public ListBoxSample3()
        {
            this.ClientSize = new Size(307, 206);
            this.Text = "ListBox Sample3";

            ListBox1.Location = new Point(54, 16);
            ListBox1.Name = "ListBox1";
            ListBox1.Size = new Size(240, 130);

            label1.Location = new Point(14, 150);
            label1.Name = "label1";
            label1.Size = new Size(40, 24);
            label1.Text = "Value";

            textBox1.Location = new Point(54, 150);
            textBox1.Name = "textBox1";
            textBox1.Size = new Size(240, 24);
            
            this.Controls.AddRange(new Control[] { ListBox1, label1, textBox1 });

            // Populate the list box using an array as DataSource.
            ArrayList USStates = new ArrayList();
            USStates.Add(new USState("Alabama", "AL"));
            USStates.Add(new USState("Washington", "WA"));
            USStates.Add(new USState("West Virginia", "WV"));
            USStates.Add(new USState("Wisconsin", "WI"));
            USStates.Add(new USState("Wyoming", "WY"));
            ListBox1.DataSource = USStates;

            // Set the long name as the property to be displayed and the short
            // name as the value to be returned when a row is selected.  Here
            // these are properties; if we were binding to a database table or
            // query these could be column names.
            ListBox1.DisplayMember = "LongName";
            ListBox1.ValueMember = "ShortName";

            // Bind the SelectedValueChanged event to our handler for it.
            ListBox1.SelectedValueChanged += 
                new EventHandler(ListBox1_SelectedValueChanged);

            // Ensure the form opens with no rows selected.
            ListBox1.ClearSelected();
        }

        private void InitializeComponent()
        {
        }

        private void ListBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (ListBox1.SelectedIndex != -1)
            {
                textBox1.Text = ListBox1.SelectedValue.ToString();
                // If we also wanted to get the displayed text we could use
                // the SelectedItem item property:
                // string s = ((USState)ListBox1.SelectedItem).LongName;
            }
        }
    }

    public class USState
    {
        private string myShortName;
        private string myLongName;

        public USState(string strLongName, string strShortName)
        {

            this.myShortName = strShortName;
            this.myLongName = strLongName;
        }

        public string ShortName
        {
            get
            {
                return myShortName;
            }
        }

        public string LongName
        {

            get
            {
                return myLongName;
            }
        }
    }
}
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections

Public Class ListBoxSample3
    Inherits Form

    Private ListBox1 As New ListBox()
    Private label1 As New Label()
    Private textBox1 As New TextBox()

    <STAThread()> _
    Shared Sub Main()
        Application.Run(New ListBoxSample3())
    End Sub

    Public Sub New()
        Me.ClientSize = New Size(307, 206)
        Me.Text = "ListBox Sample3"

        ListBox1.Location = New Point(54, 16)
        ListBox1.Name = "ListBox1"
        ListBox1.Size = New Size(240, 130)

        label1.Location = New Point(14, 150)
        label1.Name = "label1"
        label1.Size = New Size(40, 24)
        label1.Text = "Value"

        textBox1.Location = New Point(54, 150)
        textBox1.Name = "textBox1"
        textBox1.Size = New Size(240, 24)

        Me.Controls.AddRange(New Control() {ListBox1, label1, textBox1})

        ' Populate the list box using an array as DataSource. 
        Dim USStates As New ArrayList()
        USStates.Add(New USState("Alabama", "AL"))
        USStates.Add(New USState("Washington", "WA"))
        USStates.Add(New USState("West Virginia", "WV"))
        USStates.Add(New USState("Wisconsin", "WI"))
        USStates.Add(New USState("Wyoming", "WY"))
        ListBox1.DataSource = USStates

        ' Set the long name as the property to be displayed and the short
        ' name as the value to be returned when a row is selected.  Here
        ' these are properties; if we were binding to a database table or
        ' query these could be column names.
        ListBox1.DisplayMember = "LongName"
        ListBox1.ValueMember = "ShortName"

        ' Bind the SelectedValueChanged event to our handler for it.
        AddHandler ListBox1.SelectedValueChanged, AddressOf ListBox1_SelectedValueChanged

        ' Ensure the form opens with no rows selected.
        ListBox1.ClearSelected()
    End Sub

    Private Sub InitializeComponent()
    End Sub

    Private Sub ListBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As EventArgs)
        If ListBox1.SelectedIndex <> -1 Then
            textBox1.Text = ListBox1.SelectedValue.ToString()
            ' If we also wanted to get the displayed text we could use
            ' the SelectedItem item property:
            ' Dim s = CType(ListBox1.SelectedItem, USState).LongName
        End If
    End Sub
End Class

Public Class USState
    Private myShortName As String
    Private myLongName As String

    Public Sub New(ByVal strLongName As String, ByVal strShortName As String)
        Me.myShortName = strShortName
        Me.myLongName = strLongName
    End Sub

    Public ReadOnly Property ShortName() As String
        Get
            Return myShortName
        End Get
    End Property

    Public ReadOnly Property LongName() As String
        Get
            Return myLongName
        End Get
    End Property

End Class

備註

繼承自 ListControl 的控制項可以顯示各種不同的物件類型。 如果 指定的屬性不存在於 物件上,或 的值 DisplayMember 是空字串 (「」) ,則會改為顯示物件 ToString 方法的結果。

如果無法設定新的顯示成員,則會維護先前的顯示成員設定。

適用於