2016-11-28 12 views
1

に応じてテキストボックスの値を変更しますオブジェクト。は、私は、テキストボックスにコンボボックスの値を選択して取得する以下のコードを試してみましたが、それは私に次のエラーを与える</p> <p>エラーしている選択されたコンボボックスの値

コード

private void frmpaymentsearch_Load(object sender, EventArgs e) 
{ 
    txtcomvalue.Text = "PaymentVoucherCode"; 
    dllby.DisplayMember = "Text"; 
    dllby.ValueMember = "Value"; 
    dllby.Items.Add(new { Text = "P.Voucher Code", Value = "PaymentVoucherCode" }); 
    dllby.Items.Add(new { Text = "Vendor", Value = "VendorName" }); 
    dllby.SelectedIndex = 0; 
} 



private void dllby_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    txtcomvalue.Text = dllby.SelectedValue.ToString(); 
} 
+0

あなたはそれが 'SelectedValueのためにnullを返しますComboBox'' 'ためDataSource'を使用しない場合'。 –

+0

一般的な解決方法として、['GetItemValue'](http://stackoverflow.com/a/38305363/3110834)拡張メソッドに依存することができます。 'ValueMember'に基づいてitemの値を以下のように抽出します:' var value = comboBox1.GetItemValue(comboBox1.SelectedItem).ToString(); '。 'ComboBox'と' ListBox'はテキストを取得するための 'GetItemText'を持っていますが、GetItemValueがありません。リンクされたポストは、この不足を解決する拡張メソッドを共有しました。 –

答えて

2

ComboBox SelectedItem vs SelectedValue

private void dllby_SelectedIndexChanged(object sender, EventArgs e) 
{ 
     Type myType = dllby.SelectedItem.GetType(); 
     IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties()); 

     foreach (PropertyInfo prop in props) 
     { 
      if(prop.Name=="value") 
       textBox1.Text = prop.GetValue(dllby.SelectedItem, null).ToString(); 
     } 
} 
+0

'dllby.SelectedValue'が失敗する理由を説明できますか? – Damith

+0

ありがとうコード返す{テキスト= P.バウチャーコード、値= PaymentVoucherCode}私はサブ文字列を使用する必要があります値を取得する必要がありますか? – Ayman

+0

@Ayman - 匿名型の使用を避けるべきです(ジャスミンの回答を参照)。 'Value'を取得するには、あなたの現在のコードでこのようなもの(醜いです!)が必要です:' dllby.SelectedItem?.GetType()。GetProperty( "Value")?GetValue(dllby.SelectedItem、null) '(これはC#6が必要) –

0
Dictionary comboSource = new Dictionary(); 
      comboSource.Add("1", "Sunday"); 
      comboSource.Add("2", "Monday"); 
      comboSource.Add("3", "Tuesday"); 
      comboSource.Add("4", "Wednesday"); 
      comboSource.Add("5", "Thursday"); 
      comboSource.Add("6", "Friday"); 
      comboSource.Add("7", "Saturday"); 
      comboBox1.DataSource = new BindingSource(comboSource, null); 
      comboBox1.DisplayMember = "Value"; 
      comboBox1.ValueMember = "Key"; 
0
private void frmpaymentsearch_Load(object sender, EventArgs e) 
    { 
     txtcomvalue.Text = "PaymentVoucherCode"; 
     dllby.DisplayMember = "Text"; 
     dllby.ValueMember = "Value"; 
     dllby.Items.Add(new { Text = "P.Voucher Code", Value = "PaymentVoucherCode" }); 
     dllby.Items.Add(new { Text = "Vendor", Value = "VendorName" }); 
     dllby.SelectedIndexChanged -= dllby_SelectedIndexChanged; // unsubscribe you event 
     dllby.SelectedIndex = 0; 
     dllby.SelectedIndexChanged += dllby_SelectedIndexChanged; // subscribe you event 
    } 

    private void dllby_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     txtcomvalue.Text = dllby.Text.ToString(); // change selectedvalue to Text 
    } 
0
private void frmpaymentsearch_Load(object sender, EventArgs e) 
{ 
    txtcomvalue.Text = "PaymentVoucherCode"; 
    dllby.DisplayMember = "Text"; 
    dllby.ValueMember = "Value"; 
    dllby.Items.Add(new ComboboxItem(){ Text = "P.Voucher Code", Value = "PaymentVoucherCode" }); 
    dllby.Items.Add(new ComboboxItem(){ Text = "Vendor", Value = "VendorName" }); 

    dllby.SelectedIndex = 0; 
} 
private void dllby_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    txtcomvalue.Text = (dllby.SelectedItem as ComboboxItem).Value.ToString(); 
} 

public class ComboboxItem 
{ 
    public string Text { get; set; } 
    public string Value { get; set; } 

    public override string ToString() 
    { 
     return Text; 
    } 
} 
+0

タイプまたは名前空間 'ComboboxItem'が見つかりませんでした – Ayman

関連する問題