【问题标题】:How do I stop [ ] appearing when inserting the value of a KeyValuePair<string, string> into a list control?将 KeyValuePair<string, string> 的值插入列表控件时,如何停止出现 [ ]?
【发布时间】:2020-05-06 23:22:38
【问题描述】:

就目前而言,我正在使用字典以以下格式存储问题的答案:-

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

A1、A2 等是用于在别处选择问题的唯一标识符,答案在末尾标记,字典存储 ID 和答案。

这部分一切正常。问题是在将数据插入列表框/组合框时。目前我正在使用以下方法:

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));
    }
}

这会将正确的数据插入到列表/组合框中,但格式如下:

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

我尝试了许多其他方法来摆脱方括号。有趣的是,如果我 做吧

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

我仍然以 [A1: Warehouse] 格式获取数据。我怎样才能摆脱方括号?

编辑:被要求添加更多代码。这是完整的添加到列表控制方法:

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));
            }
        }
    }

此方法调用自:

    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;
    }

使用以下行调用:

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

希望对您有所帮助。

编辑:我已经尝试了下面的所有建议,但仍然没有改进 - 我已经检查并重新检查了代码以及与之相关的所有方法,以获取我错过的一些额外格式,并没有发现任何可能导致格式在一个阶段正确设置,然后在它出现在屏幕上时分档。

【问题讨论】:

  • 这里发生了其他事情。能否提供更多代码?
  • 您确定没有在其他地方进行其他数据绑定吗?
  • 我刚刚用一些额外的代码编辑了帖子以提供更多上下文。我仔细检查了没有额外的格式化,据我所知,这是该数据被格式化的唯一地方。
  • NB 你的if 有必要吗?你不能打电话给listControl.Items.Add(...)吗?
  • 感谢您注意到 ^。由于您的评论,我在查看 IF 后出于几个原因删除了它。

标签: c# combobox listbox keyvaluepair


【解决方案1】:

我看不出有问题。

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));
}

【讨论】:

  • 是的,这正是让我感到困惑的地方。我在列表框中看不到代码有问题,数据完全忽略了 string.format。
  • 这被标记为答案。解决问题了吗?
【解决方案2】:

这很愚蠢,但请尝试更改:

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

符合

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

【讨论】:

  • 或者只是简单的oTemp.Key + " - " + oTemp.Value。我知道 string.Format 对连接字符串有好处,但是在这种情况下,它不会产生太大影响,因为连接的数量很少。
  • 我只是试了一下主要答案。当插入项目时,数据的格式正确,但是一旦进入列表框,它的格式就改变了。我想这可能意味着我还没有找到其他地方的问题。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多