设置 ListBox 项的标签 [英] Set tag of a ListBox item

查看:30
本文介绍了设置 ListBox 项的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以设置 ListBox 的单个项目的 Tag 属性而不是整个控件?

Is it possible to set the Tag property of an individual item of the ListBox instead of only the whole control?

我试过了

listbox.Items(i).Tag = "Test"

不幸的是没有任何运气.

Unfortunately without any luck.

推荐答案

ListBox 不是这样工作的.它没有像 TreeView 或 ListView 那样的专用项目类型,也没有 ListBoxItem 类.所以也没有 Tag 属性.

That's not how ListBox works. It doesn't have a dedicated item type like TreeView or ListView has, there is no ListBoxItem class. So there's no Tag property either.

ListBox 更为通用,它在其 Items.Add() 方法中接受 任何 对象.规则是您添加的任何对象都需要覆盖 ToString() 方法.这就是 ListBox 用来显示项目文本的内容.这也消除了对 Tag 属性的需要.只需将您从 listBox1.Items[listBox1.SelectedIndex] 返回的对象转换回您的类类型.

ListBox is far more general, it accepts any object in its Items.Add() method. The rule is that whatever object you add needs to override the ToString() method. That's what ListBox uses to display the text for the item. Which then also removes the need for a Tag property. Just cast the object you get back from, say, listBox1.Items[listBox1.SelectedIndex] back to your class type.

如果这很难处理,请考虑使用 ListView,将其 View 属性设置为 List 也会为您提供一个列表框.使用您添加的 ListViewItems 的 Tag 属性.

Do consider ListView if that's hard to deal with, setting its View property to List gets you a list box too. With a Tag property for the ListViewItems you add.

这篇关于设置 ListBox 项的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆