Export (0) Print
Expand All

ListControl.ValueMember Property

Gets or sets the path of the property to use as the actual value for the items in the ListControl.

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

'Declaration
Public Property ValueMember As String

Property Value

Type: System.String
A String representing a single property name of the DataSource property value, or a hierarchy of period-delimited property names that resolves to a property name of the final data-bound object. The default is an empty string ("").

ExceptionCondition
ArgumentException

The specified property path cannot be resolved through the object specified by the DataSource property.

Specify the contents of the ValueMember property in cases where you bind data.

You can clear the ValueMember property by setting the property to an empty string ("") or Nothing.

Setting a new ValueMember property raises the ValueMemberChanged and SelectedValueChanged events.

The following code example shows loading the data into an ArrayList and setting the ValueMember to a property of the object in the list.

Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections

Public Class ListBoxSample3
    Inherits Form

    Private ListBox1 As New ListBox()
    Private label1 As New Label()
    Private textBox1 As New TextBox()

    <STAThread()> _
    Shared Sub Main()
        Application.Run(New ListBoxSample3())
    End Sub 'Main

    Public Sub New()
        Me.ClientSize = New Size(307, 206)
        Me.Text = "ListBox Sample3"

        ListBox1.Location = New Point(54, 16)
        ListBox1.Name = "ListBox1"
        ListBox1.Size = New Size(240, 130)

        label1.Location = New Point(14, 150)
        label1.Name = "label1"
        label1.Size = New Size(40, 24)
        label1.Text = "Value"

        textBox1.Location = New Point(54, 150)
        textBox1.Name = "textBox1"
        textBox1.Size = New Size(240, 24)

        Me.Controls.AddRange(New Control() {ListBox1, label1, textBox1})

        ' Populate the list box using an array as DataSource.  
        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"))
        ListBox1.DataSource = USStates

        ' Set the long name as the property to be displayed and the short 
        ' name as the value to be returned when a row is selected.  Here 
        ' these are properties; if we were binding to a database table or 
        ' query these could be column names.
        ListBox1.DisplayMember = "LongName"
        ListBox1.ValueMember = "ShortName" 

        ' Bind the SelectedValueChanged event to our handler for it. 
        AddHandler ListBox1.SelectedValueChanged, AddressOf ListBox1_SelectedValueChanged

        ' Ensure the form opens with no rows selected.
        ListBox1.ClearSelected()
    End Sub 'New 

    Private Sub InitializeComponent()
    End Sub 'InitializeComponent

    Private Sub ListBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As EventArgs)
        If ListBox1.SelectedIndex <> -1 Then
            textBox1.Text = ListBox1.SelectedValue.ToString()
            ' If we also wanted to get the displayed text we could use 
            ' the SelectedItem item property: 
            ' Dim s = CType(ListBox1.SelectedItem, USState).LongName 
        End If 
    End Sub 'ListBox1_SelectedValueChanged
End Class 'ListBoxSample3

Public Class USState
    Private myShortName As String 
    Private myLongName As String 

    Public Sub New(ByVal strLongName As String, ByVal strShortName As String)
        Me.myShortName = strShortName
        Me.myLongName = strLongName
    End Sub 'New 

    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 

End Class 'USState

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft