ListControl.SelectedValue 屬性

定義

取得或設定 ValueMember 屬性指定的成員屬性值。

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

屬性值

Object

物件,包含 ValueMember 屬性指定的資料來源成員值。

屬性

例外狀況

指派的值為 null 或空字串 ("")。

範例

下列程式碼範例顯示 傳 SelectedValue 回 屬性所 ValueMember 指定的 屬性值。 此程式碼是類別概觀中所顯示範例的 ListControl 摘錄。 如需整個程式代碼清單,請參閱 ListControl

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

備註

如果未在 中 ValueMember 指定屬性, SelectedValue 則會傳回 物件方法的結果 ToString

SelectedIndexSelectedValueFormattingEnabled 相關,如下所示:

適用於

另請參閱