2015-09-23 7 views
12

ListからKeyValuePair<int, string>のコンボボックスを作成しています。これまでは、私に数字のIDを返す間に、ユーザーに説明的な名前を提供するうえで非常にうまくいっています。
しかし、私が何をしようと、私は最初に選択した値を選択することができません。DataSourceとしてKeyValuePairのリストを持つComboBoxの最初に選択した値を選択

public StartUpForm() 
{ 
    InitializeComponent(); 

    FlowLayoutPanel flowLayout = new FlowLayoutPanel(); //This is necessary to protect the table, which is for some reason collapsing... 
    flowLayout.FlowDirection = FlowDirection.TopDown; 
    flowLayout.AutoSize = true; 
    flowLayout.AutoSizeMode = AutoSizeMode.GrowAndShrink; 

    var comboBox = new ComboBox(); 

    { 
     var choices = new List<KeyValuePair<int, string>>(); 
     choices.Add(new KeyValuePair<int, string>(1, "hello")); 
     choices.Add(new KeyValuePair<int, string>(2, "world")); 
     comboBox.DataSource = choices; 
     comboBox.ValueMember = "Key"; 
     comboBox.DisplayMember = "Value"; 
     flowLayout.Controls.Add(comboBox); 
    } 
    Controls.Add(flowLayout); 

    //None of these work: 
    comboBox.SelectedValue = 2; 
    comboBox.SelectedValue = 2.ToString(); 
    comboBox.SelectedValue = new KeyValuePair<int, string>(2, "world"); 
    comboBox.SelectedValue = "world"; 
    comboBox.SelectedItem = 2; 
    comboBox.SelectedItem = 2.ToString(); 
    comboBox.SelectedItem = new KeyValuePair<int, string>(2, "world"); 
    comboBox.SelectedItem = "world"; 

    return; 
} 

結果は常に同じです:

enter image description here

私はデータソースList<KeyValuePair<int, string>>として使用してComboBoxに最初に選択された値を選択することができますどのように?

答えて

8

コンストラクタの内部で非常にうまく機能し、そのフォームのスコープにコンボボックスの宣言を移動しようとすると、読み込み時のオーバーライドを使用しようとしない結合:なぜあなたはバインディングを使用している

ComboBox comboBox = new ComboBox(); 

protected override void OnLoad(EventArgs e) { 
    comboBox.SelectedValue = 2; 
    base.OnLoad(e); 
} 
+0

素晴らしいです!ありがとう! 1つの質問:どのようにしてこれを知っていますか? – Antonio

+0

@Antonio同じ問題がありました。 。 。何年も何年も前です。 – LarsTech

0

?文字列のテキストを表示する)int型と文字列プロパティを持つクラスを作成し、ToStringメソッド(オーバーライドすることが容易ではないでしょう:

public class ComboItem 
{ 
    public int Key {get; set;} 
    public string Text {get; set;} 
    public override string ToString() 
    { 
     return this.Text; 
    } 
} 

public void OnFormLoad(object Sender, ...) 
{ 
    IEnumerable<ComboItem> comboItems = CreateListComboItems(); 
    this.ComboBox1.Items.Clear(); 
    foreach (var comboitem in comboItems) 
    { 
     this.comboBox1.Items.Add(comboItem); 
    } 
} 

は、ComboBoxイベントをサブスクライブのSelectedIndexChanged:

private void OnComboSelectedIndexChanged(object sender, EventArgs e) 
{ 
    ComboItem selectedItem = (ComboItem)this.comboBox1.SelectedItem; 
    ProcessSelectedKey(selectedItem.Key); 
} 

補遺:最初に選択した値を選択できません。

私は、ComboBoxにそのキーに属するテキストを表示させるために、指定された値のキーを持つコンボボックス項目をプログラムで選択できるようにしたいと思います。

プロパティComboBox.ItemsはComboBoxCollection型ですが、IEnumerableを実装しています。つまり、Enumerable.Cast()を使用してComboItemsのシーケンスにキャストできます。要求されたキーを持つComboItemがあるかどうかを見つけるためにLINQのを使用して選択するには、最初のComboItemを発見した、または内の項目のコレクションを取得します。このキー

// selects the combobox item with key == value 
private void SelectComboItem(int value) 
{ 
    this.comboBox1.SelectedItem = 
     this.ComboBox1.Items.Cast<ComboItem>() 
      .FirstOrDefault(comboItem => comboItem.Key == value); 
} 

コードとはComboItemがない場合に選択するか、または何も選択しませんコンボボックス(これはComboItemsのシーケンスです)です。したがって、すべてのアイテムをComboItemにキャストできます。 次に、Valueと等しいKeyを持つ最初のComboItemを見つけようとします。そのようなComboItemが存在しない場合はnullを返します。最後に、選択した項目にします。 nullの値は何も選択しないことを示します

+1

なぜ車輪を再発明するのですか? – Antonio

+0

あなたは、動作することが証明されたパターンを再利用する理由を意味しますか? (1)使いやすさ、(2)将来のクラス変更のための理解の容易さ、(3)変更の容易さ、例えば将来的に1つではなく2つのキー値が必要な場合 –

+0

ヒントをお寄せいただきありがとうございます。私は、これが最初に選択された値を選ぶことに関する私の質問には言及していないが、言わなければならない。 – Antonio

関連する問題