谷歌高清卫星地图:ListControl.DisplayMember 属性的VB.NET例子

来源:百度文库 编辑:九乡新闻网 时间:2024/04/27 18:57:54

ListControl.DisplayMember 属性的VB.NET例子

[日期:2007-12-05]   来源:互联网整理  作者:佚名   [字体:大 中 小]     新闻简介:下面的代码示例是一个完整的应用程序,它说明在 ListBox 类实现了 ListControl 类的 DataSource、DisplayMember、 ValueMember 和 SelectedValue 等成员后,如何使用这些成员。本示例加载了 ArrayList 和列表框。当用户在列表框中选择了一个项时,将把选定的值用于返回与选定的项关联的数据。
Imports System Imports        关 键 词:  

下面的代码示例是一个完整的应用程序,它说明在 ListBox 类实现了 ListControl 类的 DataSourceDisplayMember、 ValueMember 和 SelectedValue 等成员后,如何使用这些成员。本示例加载了 ArrayList 和列表框。当用户在列表框中选择了一个项时,将把选定的值用于返回与选定的项关联的数据。

Imports SystemImports System.Windows.FormsImports System.DrawingImports System.CollectionsPublic Class USStatePrivate myShortName As StringPrivate myLongName As StringPublic Sub New(strLongName As String, strShortName As String)Me.myShortName = strShortNameMe.myLongName = strLongNameEnd Sub 'NewPublic ReadOnly Property ShortName() As StringGetReturn myShortNameEnd GetEnd PropertyPublic ReadOnly Property LongName() As StringGetReturn myLongNameEnd GetEnd PropertyPublic Overrides Function ToString() As StringReturn Me.ShortName + " - " + Me.LongNameEnd Function 'ToStringEnd Class 'USStatePublic Class ListBoxSample3Inherits FormPrivate ListBox1 As New ListBox()Private textBox1 As New TextBox()  _Shared Sub Main()Application.Run(New ListBoxSample3())End Sub 'MainPublic 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_SelectedValueChangedListBox1.DataSource = USStatesListBox1.DisplayMember = "LongName"ListBox1.ValueMember = "ShortName"End Sub 'NewPrivate Sub InitializeComponent()End Sub 'InitializeComponentPrivate Sub ListBox1_SelectedValueChanged(sender As Object, e As EventArgs)If ListBox1.SelectedIndex <> - 1 ThentextBox1.Text = ListBox1.SelectedValue.ToString()End IfEnd Sub 'ListBox1_SelectedValueChangedEnd Class 'ListBoxSample3

If you believe an article violates your rights or the rights of others, please contact us.