How do I stop [ ] appearing when inserting the value of a KeyValuePair<string, string> into a list control?

StackOverflow https://stackoverflow.com/questions/14456620

Question

As it stands I'm using a Dictionary to store answers to a question in the following format:-

Destination:
A1 - Warehouse
A2 - Front Office
A3 - Developer Office
A4 - Admin Office
B1 - Support

A1, A2 etc are unique identifiers used to select questions elsewhere and the answer is tagged on at the end, the dictionary stores ID and the Answer.

This part all works fine. The problem is when inserting the data into a list box/combo box. At the moment I'm using the method below:

foreach (KeyValuePair<string, string> oTemp in aoObjectArray)
{
    if (listControl is ComboBox)
    {
        ((ComboBox)listControl).Items.Add(string.Format("{0} - {1}", 
                                          oTemp.Key, oTemp.Value));
    }
    else if (listControl is ListBox)
    {
        ((ListBox)listControl).Items.Add(string.Format("{0} - {1}",
                                         oTemp.Key, oTemp.Value));
    }
}

This inserts the correct data into the list/combo box but in the following format:

Destination:
[A1: Warehouse]
[A2: Front Office]
[A3: Developer Office]
[A4: Admin Office]
[B1: Support]

I've tried a number of other methods to get rid of the squared brackets. Interestingly if I just do

((ComboBox)listControl).Items.Add(string.Format(oTemp.Value));

I still get the data out in the [A1: Warehouse] format. How can I get rid of the squared brackets?

EDIT: Been asked to add more code. Here is the full add to list control method:

public static void AddDictionaryToListControl(ListControl listControl,
                              Dictionary<string, string> aoObjectArray)
    {
        foreach (KeyValuePair<string, string> oTemp in aoObjectArray)
        {
            if (listControl is ComboBox)
            {
                ((ComboBox)listControl).Items.Add(string.Format(oTemp.Value));
            }
            else if (listControl is ListBox)
            {
                ((ListBox)listControl).Items.Add(string.Format(oTemp.Value));
            }
        }
    }

This method is called from:

    public ComboBox AddQuestionsComboBox(Dictionary<string, string> Items,
                       string Label, string Key, int Order, bool Mandatory)
    {
        ComboBox output; 

        output = AddControl<ComboBox>(Label, Key, Order);
        FormsTools.AddDictionaryToListControl(output, Items);
        AddTagField(output, Tags.Mandatory, Mandatory);

        return output;
    }

Which is called using the following line:

AddQuestionsComboBox(question.PickList, question.PromptTitle, question.FieldTag, 
i, offquest.Mandatory);

Hope that helps.

EDIT: I've tried all the suggestions below and still no improvement - I've checked and rechecked the code and all methods that relate to it for some additional formatting I've missed and found nothing that could cause the formatting to be set correctly at one stage and then binned by the time it gets on screen.

Was it helpful?

Solution

I can't see there is a problem.

var aoObjectArray = new Dictionary<string, string>();
aoObjectArray["A1"] = "Warehouse";
aoObjectArray["A2"] = "Front Office";
aoObjectArray["A3"] = "Developer Office";

foreach (KeyValuePair<string, string> oTemp in aoObjectArray)
{
    ((ComboBox)listControl).Items.Add(string.Format("{0} - {1}", oTemp.Key, oTemp.Value));
}

OTHER TIPS

This is silly, but try changing the:

((ComboBox)listControl).Items.Add(string.Format("{0} - {1}", 
                                  oTemp.Key, oTemp.Value));

line with

string item = string.Format("{0} - {1}", oTemp.Key, oTemp.Value);
((ComboBox)listControl).Items.Add(item);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top