1

在应用程序设置中,我有一个名为 Locations 的设置,类型是 System.Collections.Specialized.StringCollection。

我还有一个文本框和一个按钮,它们应该向 StringCollection 添加值。

将值输入文本框后,应该将值添加到列表框和 StringCollection 中。向列表框和 StringCollection 添加新值的代码如下:

       string newPrintLocation = tbAddPrintLocation.Text;

        if (lbPrintLocations.Items.Contains(newPrintLocation) == false)
        {
            lbPrintLocations.Items.Add(newPrintLocation);

            Properties.Settings.Default.Locations.Add(newPrintLocation);

            cbPrintLocation.Items.Add(newPrintLocation);

            tbAddPrintLocation.Clear();
        }

目标是当用户重新启动表单时,会将值添加回列表框中。目前,这是我用来尝试实现这一目标的代码:

           foreach (var item in Properties.Settings.Default.Locations)
        {
            lbPrintLocations.Items.Add(item);
        }

问题是只有第一个值被添加到列表框中,其余的都没有。在这一点上,我不知道我做错了什么。所以我想知道是否有人可以帮助我或指出正确的方向。

非常感谢所有帮助。

干杯,

季军。

4

0 回答 0