ListControl.SelectedValue Property

Definition

获取或设置由 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

Property Value

Object

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

Attributes

Exceptions

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

Examples

下面的代码示例演示 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. 有关完整代码列表,请参阅 ListControlSee 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

如果 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:

Applies to

See also