Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

ListControl Class

Provides a common implementation of members for the ListBox and ComboBox classes.

For a list of all members of this type, see ListControl Members.

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Windows.Forms.Control
            System.Windows.Forms.ListControl
               System.Windows.Forms.ComboBox
               System.Windows.Forms.ListBox

[Visual Basic]
MustInherit Public Class ListControl
   Inherits Control
[C#]
public abstract class ListControl : Control
[C++]
public __gc __abstract class ListControl : public Control
[JScript]
public abstract class ListControl extends Control

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

The ListControl class provides implementations of common elements for the ListBox and ComboBox controls.

The following properties are of primary concern to users of a data-bound ListBox or ComboBox: DataSource, DisplayMember, SelectedValue, and ValueMember properties.

Example

[Visual Basic, C#, C++] The following example is a complete application illustrating how you can use DataSource, DisplayMember, ValueMember, and SelectedValue members of the ListControl class as implemented by the ListBox class. The example loads an ArrayList and the list box. When the user selects an item in the list box, the selected value is used to return the data associated with the selected item.

[Visual Basic] 
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections

Public Class USState

    Private myShortName As String
    Private myLongName As String

    Public Sub New(ByVal strlongName As String, ByVal strShortName As String)
        MyBase.New()
        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

    Public Overrides Function ToString() As String
        Return Me.ShortName & " - " & Me.LongName
    End Function
End Class


Public Class ListBoxSample3
    Inherits Form
    Friend WithEvents ListBox1 As ListBox = New ListBox()
    Dim textBox1 As TextBox = New TextBox()

    <System.STAThreadAttribute()> _
    Public Shared Sub Main()
        System.Windows.Forms.Application.Run(New ListBoxSample3())
    End Sub

    Public Sub New()
        Me.AutoScaleBaseSize = New Size(5, 13)
        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(40, 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("Washington", "WA"))
        USStates.Add(New USState("West Virginia", "WV"))
        USStates.Add(New USState("Wisconsin", "WI"))
        USStates.Add(New USState("Wyoming", "WY"))

        ListBox1.DataSource = USStates
        ListBox1.DisplayMember = "LongName"
        ListBox1.ValueMember = "ShortName"

    End Sub

    Private Sub InitializeComponent()

    End Sub
    
    Private Sub ListBox1_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedValueChanged
        If ListBox1.SelectedIndex <> -1 Then
            textBox1.Text = ListBox1.SelectedValue
        End If
    End Sub
End Class

[C#] 
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.AutoScaleBaseSize = new Size(5, 13) ;
            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();
        }
    }
}

[C++] 
#using <mscorlib.dll>
#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 __gc class USState
{
private:
    String* myShortName;
    String* myLongName;

public:
    USState(String* strLongName, String* strShortName)
    {

        this->myShortName = strShortName;
        this->myLongName = strLongName;
    }

    __property String* get_ShortName()
    {
        return myShortName;
    }

    __property String* get_LongName()
    {
        return myLongName;
    }

    String* ToString()
    {
        return String::Concat( this->ShortName, S" - ", this->LongName );
    }
};

public __gc class ListBoxSample3:public Form
{
private:
    ListBox* ListBox1;
    TextBox* textBox1;

public:
    ListBoxSample3()
    {
        ListBox1 = new ListBox();
        textBox1 = new TextBox();
        this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
        this->ClientSize = System::Drawing::Size(292, 181);
        this->Text = S"ListBox Sample3";

        ListBox1->Location = Point(24, 16);
        ListBox1->Name = S"ListBox1";
        ListBox1->Size = System::Drawing::Size(232, 130);

        textBox1->Location = Point(24, 160);
        textBox1->Name = S"textBox1";
        textBox1->Size = System::Drawing::Size(240, 24);

        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 = new ArrayList();
        USStates->Add(new USState(S"Alabama", S"AL"));
        USStates->Add(new USState(S"Washington", S"WA")); 
        USStates->Add(new USState(S"West Virginia", S"WV"));
        USStates->Add(new USState(S"Wisconsin", S"WI"));
        USStates->Add(new USState(S"Wyoming", S"WY"));

        ListBox1->SelectedValueChanged += new EventHandler(this, &ListBoxSample3::ListBox1_SelectedValueChanged);
        ListBox1->DataSource = USStates;
        ListBox1->DisplayMember = S"LongName";
        ListBox1->ValueMember = S"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(new ListBoxSample3());
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Windows.Forms

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Windows.Forms (in System.Windows.Forms.dll)

See Also

ListControl Members | System.Windows.Forms Namespace

Show:
© 2014 Microsoft. All rights reserved.