开发者

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 ListBoxes. 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜