从 CheckedListBox 获取 CheckBox 的标签文本
我目前有一个带有几个框的 CheckedListBox。我希望能够测试列表中的每个复选框以查看它是否已选中,如果是,则将其文本值 (CheckBox.Text) 添加到字符串列表中。
这是我所拥有的:
for ( int i = 0; i < multiTaskChecks.Items.Count; i++ )
{
if ( multiTaskChecks.GetItemChecked(i) )
{
checkedMultiTasks.Add(multiTaskChecks.GetItemText(i));
}
}
使用此方法,GetItemText
返回 0、1、2、3 等,而不是我想要的文本值。我也尝试过 CheckedListBox.Text.IndexOf(i)
、CheckedListBox.Text.ToList()
,但都没有任何运气。
我只是无法从 CheckedListBox 中获取这些复选框之一的标签文本。对此的任何帮助将不胜感激。
I currently have a CheckedListBox with several boxes. I want to be able to test every Checkbox in the list to see if it's checked, and if it is, add it's text value (CheckBox.Text) to a List of strings.
Here is what I have:
for ( int i = 0; i < multiTaskChecks.Items.Count; i++ )
{
if ( multiTaskChecks.GetItemChecked(i) )
{
checkedMultiTasks.Add(multiTaskChecks.GetItemText(i));
}
}
Using this, GetItemText
is returning 0, 1, 2, 3, etc instead of the text values that I'm after. I have also tried CheckedListBox.Text.IndexOf(i)
, CheckedListBox.Text.ToList()
, each without any luck.
I just cannot get the label text of one of these CheckBoxes from the CheckedListBox. Any help with this would be really appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,您应该能够像这样循环遍历已检查的项目
,然后根据项目的类型,从中获取您想要的任何属性。听起来它只是一个文本或者你只想要字符串,所以
或者我更喜欢
Firstly, you should be able to loop through the checked items only like so
then depending on the type of the
item
, get whatever property you want from it. Sounds like it is just a Text or you just want the string, soor I prefer
试试这个:
ListControl.GetItemText 方法
注意 此方法的
DisplayMember
有一个警告:如果未指定 DisplayMember 属性,则 GetItemText 返回的值是项目的值ToString 方法。否则,该方法返回在 item 参数中指定的对象的 DisplayMember 属性中指定的成员的字符串值。
Try this:
ListControl.GetItemText Method
NOTE There's a caution regarding
DisplayMember
for this method:If the DisplayMember property is not specified, the value returned by GetItemText is the value of the item's ToString method. Otherwise, the method returns the string value of the member specified in the DisplayMember property for the object specified in the item parameter.
这应该有效:
This should work: