c# - Rectangle around selected ListView item -
i draw rectangle around selected item in listview, due reading somewhere microsoft recommends against changing 'highlighted colour' of said item. however, i'm using selectedindexchanged event , when actual listviewitem drawn rectangle disappears. educated guess suggest rectangle either behind or has being cleared when has being redrawn? question is, when best time draw rectangle visible? code far can seen below:
void lvmain_selectedindexchanged(object sender, eventargs e) { if (lvmain.selecteditems.count > 0) { if (lastselecteditem == null) // first time called { lastselecteditem = (sender system.windows.forms.listview).selecteditems[0]; drawhighlightrectanlge(lastselecteditem); } else { // todo: remove previous highlight lastselecteditem = (sender system.windows.forms.listview).selecteditems[0]; drawhighlightrectanlge(lastselecteditem); } } } internal void drawhighlightrectanlge(system.windows.forms.listviewitem item) { using (graphics g = item.listview.creategraphics()) { g.drawrectangle(new pen(color.red), new rectangle(item.position.x, item.position.y, item.bounds.width, item.bounds.height)); } }
tia
here basic version owner-drawn listview
. set ownerdraw
property true , code drawitem
event, maybe this:
private void listview1_drawitem(object sender, drawlistviewitemeventargs e) { e.drawbackground(); e.drawtext(); if (e.item.selected) { rectangle r = e.bounds; r.inflate(-1, -1); using (pen pen = new pen(color.red, 1.5f)) e.graphics.drawrectangle(pen, r); } }
i make rectangle little smaller work in details view
, should play around make suit needs , fancy..!
note: if have columnheaders need code drawcolumnheader
event, in simplest form this:
private void listview1_drawcolumnheader(object sender, drawlistviewcolumnheadereventargs e) { e.drawdefault = true; }
and if have subitems need have drawsubitem
event, again @ least this:
private void listview1_drawsubitem(object sender, drawlistviewsubitemeventargs e) { e.drawdefault = true; }
obviously need write more code event, if want rectangle drawn here well. default function of drawbackground
, drawtext
available here well.
Comments
Post a Comment