編輯

共用方式為


ListControl.SelectedValue Property

Definition

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

public:
 property System::Object ^ SelectedValue { System::Object ^ get(); void set(System::Object ^ value); };
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.Browsable(false)]
public object SelectedValue { get; set; }
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.Browsable(false)]
public object? SelectedValue { get; set; }
[<System.ComponentModel.Bindable(true)>]
[<System.ComponentModel.Browsable(false)>]
member this.SelectedValue : obj with get, set
Public Property SelectedValue As Object

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.

void ListBox1_SelectedValueChanged( Object^ /*sender*/, EventArgs^ /*e*/ )
{
   textBox1->Text="";
   if ( ListBox1->SelectedIndex != -1 )
         textBox1->Text = ListBox1->SelectedValue->ToString();
}
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;
    }
}
    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
End Class

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

See also