c# - Unable to cast object of type ,Drawing Item -
how fix problem follow :
when drawitem, message error: "unable cast object of type 'system.collections.generic.list`1[mypro.infodialog+mycontact]' type 'mycontact'."
c# code @ line number:
public class mycontact { public string p_display_name { get; set; } public string p_availability { get; set; } public string p_avatar_image { get; set; } } mycontact fbcontact; private void adddatatolist() { var fblist = new list<mycontact>(); foreach (dynamic item in result.data) { fbcontact = new mycontact() { p_display_name = (string)item["name"], p_availability = (string)item["online_presence"]}; fblist.add(fbcontact); listbox1.items.add(fblist); } } private int mouseindex = -1; private void listbox1_drawitem(object sender, drawitemeventargs e) { if (e.index == -1) return; line number: mycontact contact = (mycontact)listbox1.items[e.index]; brush textbrush = systembrushes.windowtext; if (e.index > -1) { // drawing frame if (e.index == mouseindex) { e.graphics.fillrectangle(systembrushes.hottrack, e.bounds); textbrush = systembrushes.highlighttext; } else { if ((e.state & drawitemstate.selected) == drawitemstate.selected) { e.graphics.fillrectangle(systembrushes.highlight, e.bounds); textbrush = systembrushes.highlighttext; }else{ e.graphics.fillrectangle(systembrushes.window, e.bounds); } // drawing text e.graphics.drawstring(contact.p_display_name, e.font, textbrush, e.bounds.left + 20, e.bounds.top); } } }
it seems whole listed added listbox1 instead of single item (listbox1.items.add(fblist))
shouldn't be:
listbox1.items.add(fbcontact);
alternatively, set listbox1.datasource = fblist after loop
Comments
Post a Comment