0

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.

Kobunite
  • 365
  • 8
  • 23
  • There's something else going on here. Can you provide more code? – Simon Whitehead Jan 22 '13 at 10:46
  • 2
    Are you sure you haven't got some other data binding going on elsewhere? – Matthew Watson Jan 22 '13 at 10:53
  • I've just edited the post with some extra code to provide more context. I double checked that there is no extra formatting going on and as far as I can see this is the only place this data is formatted. – Kobunite Jan 22 '13 at 10:56
  • NB is your `if` necessary? Can't you just call `listControl.Items.Add(...)`? – Rawling Jan 22 '13 at 11:12
  • Thanks for noting that ^. I've since removed the IF for a couple of reasons after looking at it thanks to your comment. – Kobunite Jan 23 '13 at 12:57
  • There definitely was a rather large logic error elsewhere in the program that I failed to notice for an age. Safe to say, that has been rectified and this now works. Thanks all for your help! – Kobunite Feb 18 '13 at 14:11

2 Answers2

1

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));
}
alont
  • 263
  • 2
  • 3
  • 12
1

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);
SWeko
  • 30,434
  • 10
  • 71
  • 106
  • or just simply `oTemp.Key + " - " + oTemp.Value`. I know that string.Format is good for joining string, but in this case, it won't have much impact since the number of joins is little. – alont Jan 22 '13 at 11:20
  • I just gave the main answer a shot. When inserted into item the data is formatted correctly, however once it's got into the list box it's formatting has changed. I guess that could mean that there is an issue elsewhere that I haven't managed to find yet. Thanks for your help. – Kobunite Jan 22 '13 at 11:26