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'.
private void button3_Click(object sender, EventArgs e)
{
foreach (ListBox item in SelectTables_Listbox.Items)
{
开发者_Go百科 item.SelectedItem = true;
}
MessageBox.Show(" All tables Selected ");
}
The items in SelectedTables.Items
are not ListBox
es. In your case, each item is a 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 ???
foreach (ListBox item in SelectTables.Items)
I am not sure what the type should be, but not ListBox
精彩评论