4

Ive got a dictionary filled with KeyValuePairs (equalityMap) which I'm using to populate a combobox (comBox1).

I want to call the function below as a part of initializing comBox1. Then, I have a selectedValueChanged event from another combobox (comBox2) which calls the below function and changes comBox1's content based on the type of comBox2's selected value.

Everything is working as expected when the equalities combobox is first initialized. However, when this function is being called again, instead of just the "key" being displayed in the combobox it displays the "key" and the "value" in the format ["key", "value"]

I've only just started with c# (or anything with a GUI) so unsure of the best way to debug something like this. Any help appreciated.

public void popEqualities(String fieldType)
    {

        this.equalities.DataSource = null;
        this.equalities.Items.Clear();
        this.equalityMap.Clear();

        if (fieldType == "string")
        {
            equalityMap.Add("is", "=");
            equalityMap.Add("is not", "!=");
            equalityMap.Add("contains", "CONTAINS");
            equalityMap.Add("begins with", "LIKE '%");
        }
        else if (fieldType == "int")
        {
            equalityMap.Add("is equal to", "=");
            equalityMap.Add("is not equal to", "!=");
            equalityMap.Add("is greater than", ">");
            equalityMap.Add("is less than", "<");
        }
        else if (fieldType == "date")
        {
            equalityMap.Add("is", "=");
            equalityMap.Add("is not", "!=");
            equalityMap.Add("is after", ">");
            equalityMap.Add("is before", "<");
        }
        else if (fieldType == "boolean")
        {
            equalityMap.Add("is", "=");
        }
        else
        {
            MessageBox.Show("Recieved bad Field Type");
            return;
        }

        this.equalities.DisplayMember = "Key";
        this.equalities.ValueMember = "Value";
        this.equalities.DataSource = new BindingSource(equalityMap, null);
    }  

EDIT: To declare equity map i call

this.equalityMap = new Dictionary<string, string>();

in the class constructor and have the following as a private member of the class.

private Dictionary<string, string> equalityMap

The event that calls this function is simply

public void searchFieldChanged(object sender, EventArgs e)
    {
        string fieldType = getFieldType();
        popEqualities(fieldType);
    }

Here's a couple of pics to show the issue On the initial call

initial call.

On subsequent calls

subsequent calls.

Fixed:

Turns out that when I was rebinding the DataSource it was clearing the DisplayMember property each time -

this.equalities.DisplayMember = "Key";

When you move the line rebinding the Datasource above these assignments it fixes the problem.

this.equalities.DataSource = new BindingSource(equalityMap, null);
this.equalities.DisplayMember = "Key";
this.equalities.ValueMember = "Value";
Mark Gifford
  • 135
  • 1
  • 9
  • Please provide your declaration of `equalityMap`. – Rob Epstein Mar 27 '13 at 10:28
  • Given your exact code assuming that `equalities` is a standard ComboBox control, I am unable to reproduce your issue. Is there any other relevant code you can provide that may be impacting the solution? – Rob Epstein Mar 27 '13 at 10:49
  • The only code being called after the combobox has first been populated ,as in the first picture, is the event and the popEqualities function above. – Mark Gifford Mar 27 '13 at 11:05
  • Sorry. Still can't reproduce. Best I could recommend is a redefinition of your `equalitiesMap` list to be based off a `struct` that has a Name, Operator and Type, then set `DataSource = equalitiesMap.Where(x => x.Type == getFieldType())` in your `searchFieldChanged` function. Also change your DisplayMember to Name and ValueMember to Operator. – Rob Epstein Mar 27 '13 at 11:13

1 Answers1

0

An entry in a System.Collections.Generic.Dictionary contains the properties Key and Value to display the contents. If you just display an entry, you are implicitly using the ToString()-method, which will display the contents of the entry as ["key", "value"].

If you only want to show the keys, you must use the Key-property and print this out.

Have a look at MSDN and the methods/properties of System.Collections.Generic.Dictionary<TKey, TValue>.

bash.d
  • 13,029
  • 3
  • 29
  • 42
  • I thought thats what these two line were doing - this.equalities.DisplayMember = "Key"; this.equalities.ValueMember = "Value"; As i said, it works when this function is first called. – Mark Gifford Mar 27 '13 at 10:21
  • Why are you actually rebinding `this.equalities.DataSource = new BindingSource(equalityMap, null);` in your event? – bash.d Mar 27 '13 at 10:53
  • Originally it was to make this function re-usable. I could move that line so it only gets called initially though. Will the combo box update automatically when equityMap is changed whilst it's set as the DataSource? – Mark Gifford Mar 27 '13 at 11:10
  • If you bind it to an object it will change, of course! See [here](http://msdn.microsoft.com/en-us/library/ms750612.aspx). – bash.d Mar 27 '13 at 11:11
  • In my local tests, the combobox was not refreshing as data in the Dictionary changed. – Rob Epstein Mar 27 '13 at 11:25
  • Turns out you cant clear() a control when a DataSource is set so I cant see any other way round except rebinding each time? – Mark Gifford Mar 27 '13 at 11:27