示例
下面的示例是一个完整的应用程序,它说明在 ListBox 类实现了 ListControl 类的 DataSource、DisplayMember、ValueMember 和 SelectedValue 等成员后,如何使用这些成员。本示例加载了 ArrayList 和列表框。当用户在列表框中选择了一个项时,选定的值用于返回与选定的项关联的数据。[Visual Basic]
Imports System.Windows.Forms
Imports System.Drawing
Imports System.CollectionsPublic 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 Sample1" 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 Sample1" ; 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();
}
}
}
下面的示例是一个完整的应用程序,它说明在 ListBox 类实现了 ListControl 类的 DataSource、DisplayMember、ValueMember 和 SelectedValue 等成员后,如何使用这些成员。本示例加载了 ArrayList 和列表框。当用户在列表框中选择了一个项时,选定的值用于返回与选定的项关联的数据。[Visual Basic]
Imports System.Windows.Forms
Imports System.Drawing
Imports System.CollectionsPublic 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 Sample1" 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 Sample1" ; 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();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货