[英]Error when trying to select all items in listbox on button click event
Error : 错误:
Unable to cast object of type 'System.String' to type 'System.Windows.Forms.ListBox'. 无法将类型为“ System.String”的对象转换为类型为“ System.Windows.Forms.ListBox”的对象。
private void button3_Click(object sender, EventArgs e)
{
foreach (ListBox item in SelectTables_Listbox.Items)
{
item.SelectedItem = true;
}
MessageBox.Show(" All tables Selected ");
}
The items in SelectedTables.Items
are not ListBox
es. SelectedTables.Items
中的项目不是ListBox
。 In your case, each item is a string
. 在您的情况下,每个项目都是一个
string
。 The easiest method for selecting all elements would be something like this: 选择所有元素的最简单方法是这样的:
for(int i = 0; i < SelectTables.Items.Count; i++)
{
SelectTables.SetSelected(i, true);
}
Aren't you trying to cast list box items into a ListBox ??? 您不是试图将列表框项目投射到ListBox中吗?
foreach ( ListBox item in SelectTables.Items) foreach(SelectTables.Items中的ListBox项)
I am not sure what the type should be, but not ListBox 我不确定应该是什么类型,但不是ListBox
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.