为什么在调用 ClearSelected 并附加数据源后 SelectedIndex 可能为 0?
Why might SelectedIndex be 0 after calling ClearSelected and attaching a DataSource?
我正在使用 ListBox
,它使用 List<SomeObject>
作为其 DataSource
。
当需要从列表框中删除项目时,我会从列表中删除该项目,然后将列表重新添加为数据源。
除列表末尾的项目(包括只有一项的列表)外,这工作正常。在这些情况下,当我尝试重新附加数据源时出现错误:
InvalidArgument=Value of '0' is not valid for 'SelectedIndex'.
这对我来说很有意义 - ListBox 试图 Select 索引中的一个项目现在超出了范围。没有意义的是,即使我在重新附加之前立即在 ListBox 上调用 ClearSelected()
,这种行为仍然存在:
if (this.RulesBox.DataSource != null) this.RulesBox.DataSource = null;
this.RulesBox.ClearSelected();
this.RulesBox.DataSource = this.Rules;
我需要做什么才能正确清除 SelectedIndex?
我建议使用 BindingSource 从您的列表中删除项目。
这将避免将列表重新附加到列表框。
在这个例子中我使用了 List<string>
ListBox l = new ListBox();
BindingSource bs = new BindingSource();
void Main()
{
Form f = new Form();
Button b = new Button();
b.Click += onclick;
b.Dock = DockStyle.Bottom;
List<string> ls = new List<string>()
{"Steve", "Mark", "John"};
bs.DataSource = ls;
l.DataSource = bs;
l.Dock = DockStyle.Fill;
f.Controls.Add(b);
f.Controls.Add(l);
f.Show();
}
void onclick(object sender, EventArgs e)
{
if(l.SelectedIndex != -1)
{
bs.RemoveAt(l.SelectedIndex);
}
}
顺便说一句,我能够使用 ClearSelected 重现您的问题。看来您需要调用 ClearSelected 两次才能有效地从列表框中的任何项目中删除选择。
像这样
this.RulesBox.ClearSelected();
this.RulesBox.ClearSelected();
但是,我再次认为您应该使用 BindingSource,而不是分离和重新附加 DataSource。对于很少的项目,可能没什么大不了的,但如果你有很多项目,我认为你应该注意到这种 attach/detach 方法的性能下降。
这可能会有所帮助:
设置列表框 属性 Listbox.FormattingEnabled = true
或设置 .SelectedValue
来自列表框控件 FormattingEnabled 页面:
将 FormattingEnabled
属性 设置为 true 会导致为 ListControl
、SelectedIndex
、SelectedValue
和 SelectedValue
的每个可见成员引发 Format 事件FormattingEnabled
相关如下:
If FormattingEnabled
is false
, SelectedIndex
will not be set to -1
when SelectedValue
is blank.
If FormattingEnabled
is true, SelectedIndex
will be set to -1 when
SelectedValue
is blank.
我正在使用 ListBox
,它使用 List<SomeObject>
作为其 DataSource
。
当需要从列表框中删除项目时,我会从列表中删除该项目,然后将列表重新添加为数据源。
除列表末尾的项目(包括只有一项的列表)外,这工作正常。在这些情况下,当我尝试重新附加数据源时出现错误:
InvalidArgument=Value of '0' is not valid for 'SelectedIndex'.
这对我来说很有意义 - ListBox 试图 Select 索引中的一个项目现在超出了范围。没有意义的是,即使我在重新附加之前立即在 ListBox 上调用 ClearSelected()
,这种行为仍然存在:
if (this.RulesBox.DataSource != null) this.RulesBox.DataSource = null;
this.RulesBox.ClearSelected();
this.RulesBox.DataSource = this.Rules;
我需要做什么才能正确清除 SelectedIndex?
我建议使用 BindingSource 从您的列表中删除项目。
这将避免将列表重新附加到列表框。
在这个例子中我使用了 List<string>
ListBox l = new ListBox();
BindingSource bs = new BindingSource();
void Main()
{
Form f = new Form();
Button b = new Button();
b.Click += onclick;
b.Dock = DockStyle.Bottom;
List<string> ls = new List<string>()
{"Steve", "Mark", "John"};
bs.DataSource = ls;
l.DataSource = bs;
l.Dock = DockStyle.Fill;
f.Controls.Add(b);
f.Controls.Add(l);
f.Show();
}
void onclick(object sender, EventArgs e)
{
if(l.SelectedIndex != -1)
{
bs.RemoveAt(l.SelectedIndex);
}
}
顺便说一句,我能够使用 ClearSelected 重现您的问题。看来您需要调用 ClearSelected 两次才能有效地从列表框中的任何项目中删除选择。
像这样
this.RulesBox.ClearSelected();
this.RulesBox.ClearSelected();
但是,我再次认为您应该使用 BindingSource,而不是分离和重新附加 DataSource。对于很少的项目,可能没什么大不了的,但如果你有很多项目,我认为你应该注意到这种 attach/detach 方法的性能下降。
这可能会有所帮助:
设置列表框 属性 Listbox.FormattingEnabled = true
或设置 .SelectedValue
来自列表框控件 FormattingEnabled 页面:
将 FormattingEnabled
属性 设置为 true 会导致为 ListControl
、SelectedIndex
、SelectedValue
和 SelectedValue
的每个可见成员引发 Format 事件FormattingEnabled
相关如下:
If
FormattingEnabled
isfalse
,SelectedIndex
will not be set to -1 whenSelectedValue
is blank.If
FormattingEnabled
is true,SelectedIndex
will be set to -1 whenSelectedValue
is blank.