[英]getting selected index of listbox(c#)
我有一个SelectionBox设置为多个的ListBox
。 当我使用ListBox1.SelectedIndex
检查所选索引时,即使单击某个项目,我也总是得到-1? 我希望能够获得列表框中多个选定项目的索引。
使用GetSelectedIndices()
方法。
由于可以选择一个以上的项目,因此必须获得SelectedItems的集合。 遍历它们。 每个项目都有索引属性。
试试这个方法
ListBox.SelectedIndexCollection SelectedIndices { get; }
当您只允许选择一个值时,将使用SelectedIndex方法。
尝试这样的事情。 使用此代码,您将在一个字符串中获得所有选定的索引。
int length = this.ListBox1.Items.Count;
StringBuilder b = new StringBuilder();
for ( int i = 0 ; i < length; i++ )
{
if ( this.ListBox1.Items[ i ] != null && this.ListBox1.Items[ i ].Selected )
{
b.Append( this.ListBox1.Items[ i ].Selected );
b.Append( "," );
}
}
if ( b.Length > 0 )
{
b.Length = b.Length - 1;
}
return b.ToString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.