ListControl.SelectedValue Property

Definition

Gets or sets the value of the member property specified by the ValueMember property.

C#
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.Browsable(false)]
public object SelectedValue { get; set; }
C#
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.Browsable(false)]
public object? SelectedValue { get; set; }

Property Value

An object containing the value of the member of the data source specified by the ValueMember property.

Attributes

Exceptions

The assigned value is null or the empty string ("").

Examples

The following code example shows the SelectedValue returning the value of the property specified by the ValueMember property. This code is an excerpt from the example shown in the ListControl class overview. See ListControl for the whole code listing.

C#
private void ListBox1_SelectedValueChanged(object sender, EventArgs e)
{
    if (ListBox1.SelectedIndex != -1)
    {
        textBox1.Text = ListBox1.SelectedValue.ToString();
        // If we also wanted to get the displayed text we could use
        // the SelectedItem item property:
        // string s = ((USState)ListBox1.SelectedItem).LongName;
    }
}

Remarks

If a property is not specified in ValueMember, SelectedValue returns the results of the ToString method of the object.

SelectedIndex, SelectedValue, and FormattingEnabled are related as follows:

Applies to

Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also