ListControl.SelectedValue ListControl.SelectedValue ListControl.SelectedValue ListControl.SelectedValue Property

定义

获取或设置由 ValueMember 属性指定的成员属性的值。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; }
member this.SelectedValue : obj with get, set
Public Property SelectedValue As Object

属性值

包含由 ValueMember 属性指定的数据源成员的值的对象。An object containing the value of the member of the data source specified by the ValueMember property.

异常

指定的值为 null 或是空字符串 ("")。The assigned value is null or the empty string ("").

示例

下面的代码示例演示如何SelectedValue返回ValueMember由属性指定的属性的值。The following code example shows the SelectedValue returning the value of the property specified by the ValueMember property. 此代码摘自ListControl类概述中所示的示例。This code is an excerpt from the example shown in the ListControl class overview. 有关ListControl完整的代码清单, 请参阅。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 'ListBox1_SelectedValueChanged
End Class 'ListBoxSample3

注解

如果未在中ValueMember指定属性, SelectedValue则将ToString返回对象的方法的结果。If a property is not specified in ValueMember, SelectedValue returns the results of the ToString method of the object.

SelectedIndexSelectedValueFormattingEnabled是相关的, 如下所示:SelectedIndex, SelectedValue, and FormattingEnabled are related as follows:

适用于

另请参阅