开发者

Move list box items from one listbox to another listbox?

开发者 https://www.devze.com 2023-04-09 01:19 出处:网络
I am trying to movelistbox items between two listboxes.Before that i created two arrraylistsfor each listbox.so before moving the listbox item i am trying to add the items to the arraylist. here is t开

I am trying to move listbox items between two listboxes.Before that i created two arrraylists for each listbox.so before moving the listbox item i am trying to add the items to the arraylist. here is t开发者_如何学JAVAhe code

 foreach (string st in listbox1.Items)
            {
                arraylist1.Add(st1);
            }

but i am getting an exeption Unable to cast object of type 'System.Web.UI.WebControls.ListItem' to type 'System.String'.


You're getting this error because listbox1.Items is a ListItemCollection.

foreach (ListItem li in listbox1.Items)
{
    arraylist1.Add(li.Text);
}


foreach (string st in listbox1.Items) should be

 foreach (ListItem st in listbox1.Items) 
           // your code


Try using

foreach (ListItem li in listbox1.Items) 
{     
     arraylist1.Add(li.Text); 
}


ListBox.Items is of ListItemCollection type. It consists of ListItem objects, not strings.

What You are trying to do is:

        foreach (ListItem lstItem in ListBox1.Items)
        {
            arraylist1.Add(lstItem);
        } 


This is because a list item is an object in it's own right and not a string object. So change the string to ListItem in your code above and it should work fine.

0

精彩评论

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

关注公众号