WinForms/C#:将项目添加到 Combox 并控制项目值(数字)
我一直使用设计器将我的项目填充到组合框中,我传递的只是一个字符串。
虽然现在我需要控制每个项目存储哪个键/索引。
我以为有一个 item 对象,但我查看了 ADD 方法,它接受对象。
我如何在控件中传递键/索引,即当我执行 SelectedItem 时返回的内容。
因此,如果我执行 selectedtext,我会返回一个显示在当前选定下拉列表中的字符串,但如果我执行 selecteditem,我想返回一个需要存储的自定义数字...
有什么想法如何做到这一点?
提前致谢
I have been populating my Items to a combox using the designer and all i pass is a string.
Although now i need to control which key/index is stored with each item.
I thought there was a item object, but i looked at the method ADD and it accepts object..
How do i pass in an control the key/index i.e. what is returned when i do SelectedItem.
So if i do selectedtext i get back a string that is displayed in the current selected dropdown but if i do selecteditem i want to get back a custom number that i need to store with it...
Any ideas how to do this?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将其绑定到键\值对象的集合并使用 DisplayMember 和 ValueMember 属性来设置显示/返回的内容。
下面是一个示例:
然后,
您也可以通过添加 Tag 属性(通常用于存储此类内容)通过创建自定义组合项,阅读此处了解如何执行此操作
You need to bind it to a collection of key\value objects and use the DisplayMember and ValueMember properties to set what is displayed/returned.
Heres an example:
Then
Alternatively you could store the index by adding a Tag property (which is often used to store things like this) by creating a custom combo item, have a read here for how to do this