다음을 통해 공유


ListControl.DisplayMember 속성

ListControl에 대해 표시할 속성을 가져오거나 설정합니다.

네임스페이스: System.Windows.Forms
어셈블리: System.Windows.Forms(system.windows.forms.dll)

구문

‘선언
Public Property DisplayMember As String
‘사용 방법
Dim instance As ListControl
Dim value As String

value = instance.DisplayMember

instance.DisplayMember = value
public string DisplayMember { get; set; }
public:
property String^ DisplayMember {
    String^ get ();
    void set (String^ value);
}
/** @property */
public String get_DisplayMember ()

/** @property */
public void set_DisplayMember (String value)
public function get DisplayMember () : String

public function set DisplayMember (value : String)

속성 값

DataSource 속성에서 지정한 컬렉션에 포함된 개체 속성의 이름을 지정하는 String입니다. 기본값은 빈 문자열("")입니다.

설명

ListControl에서 상속한 컨트롤은 다양한 유형의 개체를 표시할 수 있습니다. 지정된 속성이 개체에 없거나 DisplayMember의 값이 빈 문자열("")인 경우 해당 개체의 ToString 메세드 결과가 대신 표시됩니다.

새 표시 멤버를 설정할 수 없는 경우 이전 표시 멤버 설정이 유지됩니다.

예제

다음 코드 예제는 ListBox 클래스에 의해 구현되는 대로 ListControl 클래스의 DataSource, DisplayMember, ValueMemberSelectedValue 멤버를 사용하는 방법을 보여 주는 완전한 응용 프로그램입니다. 이 예제는 ArrayList와 목록 상자를 로드합니다. 사용자가 목록 상자에서 항목을 선택할 때 선택된 값은 선택된 항목과 관련된 데이터를 반환하는 데 사용됩니다.

Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections

Public Class USState
   Private myShortName As String
   Private myLongName As String
   
   Public Sub New(strLongName As String, strShortName As String)
      Me.myShortName = strShortName
      Me.myLongName = strLongName
   End Sub 'New
   
   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
   
   Public Overrides Function ToString() As String
      Return Me.ShortName + " - " + Me.LongName
   End Function 'ToString
End Class 'USState


Public Class ListBoxSample3
   Inherits Form
   Private ListBox1 As New ListBox()
   Private textBox1 As New TextBox()
   
   <STAThread()>  _
   Shared Sub Main()
      Application.Run(New ListBoxSample3())
   End Sub 'Main
   
   Public Sub New()
      Me.ClientSize = New Size(292, 181)
      Me.Text = "ListBox Sample3"
      ListBox1.Location = New Point(24, 16)
      ListBox1.Name = "ListBox1"
      ListBox1.Size = New Size(232, 130)
      textBox1.Location = New Point(24, 160)
      textBox1.Name = "textBox1"
      textBox1.Size = New Size(240, 24)
      Me.Controls.AddRange(New Control() {ListBox1, textBox1})
      
      ' Populates the list box using DataSource. 
      ' DisplayMember is used to display just the long name of each state.
      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"))
      AddHandler ListBox1.SelectedValueChanged, AddressOf ListBox1_SelectedValueChanged
      ListBox1.DataSource = USStates
      ListBox1.DisplayMember = "LongName"
      ListBox1.ValueMember = "ShortName"
   End Sub 'New
   
   Private Sub InitializeComponent()
   End Sub 'InitializeComponent
   
   Private Sub ListBox1_SelectedValueChanged(sender As Object, e As EventArgs)
      If ListBox1.SelectedIndex <> - 1 Then
         textBox1.Text = ListBox1.SelectedValue.ToString()
      End If
   End Sub 'ListBox1_SelectedValueChanged
End Class 'ListBoxSample3
using System;
using System.Windows.Forms ;
using System.Drawing ;
using System.Collections ;


namespace MyListControlSample
{

    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 ;
            }
        }

        public override string ToString()
        {
            return this.ShortName + " - " + this.LongName;
        }
    }

    public class ListBoxSample3:Form
    {
        private ListBox ListBox1 = new ListBox();
        private TextBox textBox1 = new TextBox() ;
        
        [STAThread]
        static void Main() 
        {
            Application.Run(new ListBoxSample3()) ;
        }

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

            ListBox1.Location = new Point(24, 16) ;
            ListBox1.Name = "ListBox1" ;
            ListBox1.Size = new Size(232, 130) ;
            


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

            // Populates the list box using DataSource. 
            // DisplayMember is used to display just the long name of each state.
            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.SelectedValueChanged += new EventHandler(ListBox1_SelectedValueChanged);
            ListBox1.DataSource = USStates ;
            ListBox1.DisplayMember = "LongName"      ;
            ListBox1.ValueMember = "ShortName" ;

        }
        private void InitializeComponent()
        {
        
        }

        private void ListBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (ListBox1.SelectedIndex != -1)
                textBox1.Text = ListBox1.SelectedValue.ToString();
        }
    }
}
#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;
      }

   }
   virtual String^ ToString() override
   {
      return String::Concat( this->ShortName, " - ", this->LongName );
   }
};

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

public:
   ListBoxSample3()
   {
      ListBox1 = gcnew ListBox;
      textBox1 = gcnew TextBox;
      this->ClientSize = System::Drawing::Size( 292, 181 );
      this->Text = "ListBox Sample3";
      ListBox1->Location = Point(24,16);
      ListBox1->Name = "ListBox1";
      ListBox1->Size = System::Drawing::Size( 232, 130 );
      textBox1->Location = Point(24,160);
      textBox1->Name = "textBox1";
      textBox1->Size = System::Drawing::Size( 240, 24 );
      array<Control^>^temp2 = {ListBox1,textBox1};
      this->Controls->AddRange( temp2 );
      
      // Populates the list box using 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->SelectedValueChanged += gcnew EventHandler( this, &ListBoxSample3::ListBox1_SelectedValueChanged );
      ListBox1->DataSource = USStates;
      ListBox1->DisplayMember = "LongName";
      ListBox1->ValueMember = "ShortName";
   }

   void InitializeComponent(){}

private:

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

[STAThread]
int main()
{
   Application::Run( gcnew ListBoxSample3 );
}
package MyListControlSample;

import System.*;
import System.Windows.Forms.*;
import System.Drawing.*;
import System.Collections.*;

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

    public USState(String strLongName, String strShortName)
    {
        this.myShortName = strShortName;
        this.myLongName = strLongName;
    } //USState

    /** @property
     */
    public String get_ShortName()
    {
        return myShortName;
    }//get_ShortName

    /** @property 
     */
    public String get_LongName()
    {
        return myLongName;
    }//get_LongName

    public String ToString()
    {
        return this.get_ShortName() + " - " + this.get_LongName();
    } //ToString
} //USState

public class ListBoxSample3 extends Form
{
    private ListBox listBox1 = new ListBox();
    private TextBox textBox1 = new TextBox();

    /** @attribute STAThread()
     */
    public static void main(String[] args)
    {
        Application.Run(new ListBoxSample3());
    } //main

    public ListBoxSample3()
    {
        this.set_ClientSize(new Size(292, 181));
        this.set_Text("ListBox Sample3");

        listBox1.set_Location(new Point(24, 16));
        listBox1.set_Name("ListBox1");
        listBox1.set_Size(new Size(232, 130));
        textBox1.set_Location(new Point(24, 160));
        textBox1.set_Name("textBox1");
        textBox1.set_Size(new Size(240, 24));
        this.get_Controls().AddRange(new Control[] { listBox1, textBox1 });

        // Populates the list box using DataSource. 
        // DisplayMember is used to display just the long name of each state.
        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.add_SelectedValueChanged(
            new EventHandler(listBox1_SelectedValueChanged));
        listBox1.set_DataSource(uSStates);
        listBox1.set_DisplayMember("LongName");
        listBox1.set_ValueMember("ShortName");
    } //ListBoxSample3
    
    private void InitializeComponent()
    {
    } //InitializeComponent

    private void listBox1_SelectedValueChanged(Object sender, EventArgs e)
    {
        if (listBox1.get_SelectedIndex() != -1) {
            textBox1.set_Text(listBox1.get_SelectedValue().ToString());
        }
    } //listBox1_SelectedValueChanged
} //ListBoxSample3 

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

ListControl 클래스
ListControl 멤버
System.Windows.Forms 네임스페이스