Este artigo foi traduzido por máquina. Coloque o ponteiro do mouse sobre as frases do artigo para ver o texto original. Mais informações. |
Tradução
Original
|
Classe ListControl
Assembly: System.Windows.Forms (em System.Windows.Forms.dll)
The ListControl classe fornece implementações dos elementos comuns para o ListBox e ComboBox controles.
As propriedades a seguir são uma preocupação principal para usuários de um com dados vinculados ListBox ou ComboBox: DataSource, DisplayMember, SelectedValue, e ValueMember Propriedades.
O exemplo de código a seguir é um aplicativo completo que mostra sistema autônomo você pode usar DataSource, DisplayMember, ValueMember, e SelectedValue sistema autônomo membros do ListControl conforme implementado de classe a ListBox classe. O exemplo carrega um ArrayList e o lista caixa. Quando o usuário seleciona um item na caixa de listagem, o valor selecionado é usado para retornar os dados associados com o item selecionado.
using System; using System.Windows.Forms ; using System.Drawing ; using System.Collections ; namespace MyListControlSample { publicclass 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 ; } } publicoverride string ToString() { returnthis.ShortName + " - " + this.LongName; } } publicclass ListBoxSample3:Form { private ListBox ListBox1 = new ListBox(); private TextBox textBox1 = new TextBox() ; [STAThread] staticvoid 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" ; } privatevoid InitializeComponent() { } privatevoid ListBox1_SelectedValueChanged(object sender, EventArgs e) { if (ListBox1.SelectedIndex != -1) textBox1.Text = ListBox1.SelectedValue.ToString(); } } }
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");
} //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 Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
o.NET Framework e.NET Compact Framework não oferecem suporte a todas as versões de cada plataforma. Para obter uma lista de versões suportadas, consulte Requisitos de sistema do .NET framework.