arraylist我就知道。帮助里有个完整例子 下面的示例是一个完整的应用程序,它说明在 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();
}
}
}
[C++, JScript] 没有可用于 C++ 或 JScript 的示例。若要查看 Visual Basic 或 C# 示例,请单击页左上角的语言筛选器按钮 。
[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();
}
}
}
[C++, JScript] 没有可用于 C++ 或 JScript 的示例。若要查看 Visual Basic 或 C# 示例,请单击页左上角的语言筛选器按钮 。
解决方案 »
- WCF 中的回调异常处理问题??
- 高分.net图片展示分页代码
- 急:用C#实现对XML内容的操作(大家都来帮忙看看)
- UserControl中的问题?谢谢帮忙!!!
- 开心智力题:十枚硬币排5直排.每排4枚硬币,怎么排?
- 请兄弟们给点"DataSet的数据并发异常处理"的意见!!!!!!!!!
- HELP
- 高手帮忙啊,标准表达式中数据类型不匹配
- ActiveReports WinFroms 平台 PDF 导出代码
- 想用C#来写一个能充当代理服务器的软件,想听听大伙的意见啊(送一美女!)...
- VStudio2003的开发环境,能推荐一些加强IDE环境的第三方插件么?
- 怎样使用外部存储手段如使用配置文件等保存易变量,然后由程序读出。我想在客户处没有有编译环境的时候修改连接字符串?
static ICollection CreateDataSource(string[,] data) {
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("text", typeof(string)));
dt.Columns.Add(new DataColumn("value", typeof(string)));
DataRow dr;
for (int i = 0; i < data.GetLength(0); i++) {
dr = dt.NewRow();
dr[0] = data[i,0];
dr[1] = data[i,1];
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}绑定:
DropDownList1.DataSource = CreateDataSource();
DropDownList1.DataTextField="text";
DropDownList1.DataValueField="value";
DropDownList1.DataBind();