在 C# 中将 ComboBox 项复制到 StringCollection

发布于 2024-09-11 20:38:15 字数 278 浏览 5 评论 0原文

如何将 comboBox 中的项目集合复制到 C# 应用程序中的 StringCollection?我只对按各自顺序捕获每个项目的字符串文本感兴趣。我正在尝试创建一个在会话之间保存的 MRU 文件列表,因此我想将 comboBox.Items 复制到 StringCollection Properties.Settings.Default.MostRecentlyUsedHexFiles。如果您有任何想法或建议,我们将不胜感激。谢谢。

How can I copy a collection of items in a comboBox to a StringCollection in my C# application? I'm only interested in capturing the string text for each item in their respective order. I am trying to make a MRU file list that is saved between sessions, so I would like to copy comboBox.Items to StringCollection Properties.Settings.Default.MostRecentlyUsedHexFiles. Any thoughts or suggestions you may have would be appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池木 2024-09-18 20:38:15

您应该能够遍历combobox.items并简单地使用stringcollection.Add()将字符串添加到集合中。

tostring 方法将按照此处:

尽管 ComboBox 通常是
用于显示文本项,您可以
将任何对象添加到组合框。
通常,表示一个
ComboBox 中的对象是字符串
由该对象的 ToString 返回
方法。如果你想成为会员
相反显示的对象,
选择将成为的成员
通过设置 DisplayMember 显示
财产名称
合适的成员。您还可以
选择对象的一个​​成员
将表示返回的值
通过设置 ValueMember 来获取对象
财产。有关更多信息,请参阅
列表控件。

所以像这样:

Foreach(object o in combobox.items)
{
//might need to access a datamember of the combobox's item if more complex solution is required, but this will probably do
stringcollection.Add(o.ToString);
}

You should be able to loop over the combobox.items and simply use stringcollection.Add() to add the string to the collection.

The tostring method will perform as described here:

Although the ComboBox is typically
used to display text items, you can
add any object to the ComboBox.
Typically, the representation of an
object in the ComboBox is the string
returned by that object's ToString
method. If you want to have a member
of the object displayed instead,
choose the member that will be
displayed by setting the DisplayMember
property to the name of the
appropriate member. You can also
choose a member of the object that
will represent the value returned by
the object by setting the ValueMember
property. For more information, see
ListControl.

So something like:

Foreach(object o in combobox.items)
{
//might need to access a datamember of the combobox's item if more complex solution is required, but this will probably do
stringcollection.Add(o.ToString);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文