【发布时间】: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