2017-02-13 22 views
0

私はcomboBoxで表示したいいくつかの異なる辞書構造を持っています。辞書をキーに基づいて辞書に表示する(comboBox)

JumpType.csで:

Select Key:  _____________ 
       | ComboBox | 
       --------------  __________ 
       _____________  | OK | 
Select Value: | ComboBox |  ---------- 
       -------------- 
:私はこのように私のUIに2つのコンボボックスを作成しました

Key Values 
1  Flygande 
     EjFlygande 
2  Bak 
     Pik 
     Test 
3  ... 

public SortedDictionary<int, List<string>> jumpCombination = new SortedDictionary<int, List<string>>(); 

辞書の構造は以下のようになります

In Form1.cs

InitializeComponent(); 
JumpType jt = new JumpType(); 
jt.addjumpCombination(); // populating the dictionary 
if (jt.jumpCombination != null) 
{ 
      comboBoxJumpComboKey.DataSource = new BindingSource(jt.jumpCombination, null); // Key => null 
      comboBoxJumpComboKey.DisplayMember = "Value"; 
      comboBoxJumpComboKey.ValueMember = "Key"; 
      comboBoxJumpComboValue.DisplayMember = "Value"; 
      var selectedValues = jt.jumpCombination //here i'm trying to access value 
        .Where(j => j.Key == Convert.ToInt32(comboJumpComboKey.SelectedItem.Value)) 
        .Select(a => a.Value) 
        .ToList(); 
} 

どのように私は、選択されたキーに応じて対応する値を選択しようとして行くのでしょうか?

ありがとうございます。 イメージでわかるように、キーは表示されていますが、その下のコンボボックスからは何も選択できません。 comboBox

+0

最初のインデックスが変更されます。したがって、 'comboBoxJumpComboKey​​'のインデックスが変更されたイベントハンドラを追加することができます。その場合、 'comboBoxJumpComboValue'の' DataSource'を変更します。 – Everyone

+0

@Everyone yeah。私はそれをどうやって行うのか本当に分かりません。ここに救援の手を差し伸べてもらえますか? – Joel

+0

WPFまたはWinFormsを使用していますか? – Everyone

答えて

2

DictionaryをUIクラス自体の一部として初期化します。

public SortedDictionary<int, List<string>> jumpCombination; 
    public Form1() { 
     InitializeComponent(); 
     jumpCombination = new SortedDictionary<int, List<string>>(); 
     // do whatever needed to populate the dictionary here 
     // now add the DataSource as the Keys of your dictionary which are integers 
     comboBoxJumpComboKey.DataSource = new BindingSource(jumpCombination.Keys, null); 
    } 

その後、新しいメソッドが来ると、あなたのUIデザイナでのごcomboBoxJumpComboKeyをダブルクリックし、これでそれを埋める:いつでも二コンボボックスのリストを変更している何をしたい

private void comboBoxJumpComboKey_SelectedIndexChanged(object sender, EventArgs e) { 
     comboBoxJumpComboValue.DataSource = jumpCombination[int.Parse(comboBoxJumpComboKey.Items[comboBoxJumpComboKey.SelectedIndex].ToString())]; 
    } 
+0

"invalidArgument = -1の値は 'index'には有効ではありません – Joel

+0

辞書に値が設定されていますか?あなたは、 'comboBoxJumpComboValue.DataSource = jumpCombination [comboBoxJumpComboKey​​.SelectedIndex];を使用することができます。 – Everyone

+0

はい、私はinit関数の後にそれを呼び出しています、そして、今のところキーを取得しています。キーが含まれている値を選択できるようにする(リスト内) – Joel

関連する問題