0

私の問題は、テキストボックスに何かを書くと、コンボボックスがその選択をクリアすることです。これは、以下のコードを使用してデータバインドされた 1 つの特定のコンボボックスでのみ発生します。

private void FillEmployemenetType()
{
    var items = new BindingList<KeyValuePair<string, string>>();

    items.Add(new KeyValuePair<string, string>("C", "Contract"));
    items.Add(new KeyValuePair<string, string>("P", "Permanent"));
    items.Add(new KeyValuePair<string, string>("V", "Vacation"));

    contractTypeComboBox.DataSource = items;
    contractTypeComboBox.ValueMember = "Key";
    contractTypeComboBox.DisplayMember = "Value";
    contractTypeComboBox.SelectedIndex = 0;
} 

テキスト変更イベントを発生させたり、そのためのコードを書いたりしていません。問題が発生する可能性がある場合に備えて、このウィンドウ フォームのバインディング ソースを使用します。

4

1 に答える 1

0

SelectedItem前のアイテムを再選択するために、データソースを設定した後に を設定してみてください。

private void FillEmployemenetType()
{
    KeyValuePair<string,string>? previous = null;
    if (contractTypeComboBox.DataSource != null) {
        previous = (KeyValuePair<string,string>)contractTypeComboBox.SelectedItem;
    }

    var items = new BindingList<KeyValuePair<string, string>>();

    items.Add(new KeyValuePair<string, string>("C", "Contract"));
    items.Add(new KeyValuePair<string, string>("P", "Permanent"));
    items.Add(new KeyValuePair<string, string>("V", "Vacation"));

    contractTypeComboBox.DataSource = items;
    contractTypeComboBox.ValueMember = "Key";
    contractTypeComboBox.DisplayMember = "Value";
    //contractTypeComboBox.SelectedIndex = 0;
    if (previous.HasValue) {
        try {
            contractTypeComboBox.SelectedItem = previous;
        }
        catch { }
    }
} 
于 2013-03-04T11:09:21.410 に答える