ListControl.SelectedValue 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定由 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
屬性值
一個包含屬性 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
備註
若某屬性未在 中ValueMemberSelectedValue指定,則會回傳該物件方法的結果ToString。
SelectedIndex, SelectedValue, , FormattingEnabled 的關係如下:
若 FormattingEnabled 為
falseSelectedIndex空白,則 不會設定為 -1SelectedValue。若 FormattingEnabled 為 ,
trueSelectedIndex則 將設為 -1 當為空白。SelectedValue