programmatically select and highlight a row of a ListView in VB.NET -


i want seemingly simple - programmatically select and highlight row of listview in vb.net.

vb.net: how dynamically select list view item?

tells me should needed, isn't. row selected, not highlighted.

http://vbcity.com/forums/t/28260.aspx

tells me "hideselection" property , .focus() method (also referenced @ select programmatically row of listview), sounded hopeful, best can faint highlight mentioned, want full monty. tried noddy example listview, in details mode, fullrowselection = true, hideselection = false, 1 columnheader defined , then

    listview1.items.add("red")     listview1.items.add("orange")     listview1.items.add("yellow")     listview1.items.add("green")      listview1.items(2).selected = true 

i this

enter image description here

but want this

enter image description here

i can simulate highlighting adding these lines

listview1.selecteditems(0).backcolor = color.cornflowerblue listview1.selecteditems(0).forecolor = color.white 

but how can sure undo artificial highlight if row can implicitly explicitly deselected? have think of possible cases? that's work should simple operation. plus, since want color-code rows, there additional challenge when undo highlight color, have figure out color appropriate @ point. there better, simpler way?

you can access graphics object used draw each item, , draw them yourself.

make new project button , listview. paste following code:

form_load use multiple subitems

private sub form1_load(sender object, e eventargs) handles mybase.load     me.listview1.ownerdraw = true ' or else can't handle drawitem event     listview1.columns.add("columnheader1")     listview1.columns.add("columnheader2")     listview1.columns.add("columnheader3")     me.listview1.items.add("red")     me.listview1.items.add("orange")     me.listview1.items.add("yellow")     me.listview1.items.add("green")     listview1.items(0).subitems.add("strawberry")     listview1.items(0).subitems.add("apple")     listview1.items(1).subitems.add("pepper")     listview1.items(1).subitems.add("apricot")     listview1.items(2).subitems.add("plum")     listview1.items(2).subitems.add("banana")     listview1.items(3).subitems.add("apple")     listview1.items(3).subitems.add("lime") end sub 

three handlers listview's drawing related events. code copied this answer

private sub listview1_drawcolumnheader(sender object, e drawlistviewcolumnheadereventargs) handles listview1.drawcolumnheader     e.drawdefault = true end sub  private sub listview1_drawsubitem(sender object, e drawlistviewsubitemeventargs) handles listview1.drawsubitem     const text_offset integer = 1     ' don't know why text located @ 1px right. maybe it's me.     dim listview listview = directcast(sender, listview)      ' check if e.item selected , listview has focus.     if not listview.focused andalso e.item.selected         dim rowbounds rectangle = e.subitem.bounds         dim labelbounds rectangle = e.item.getbounds(itemboundsportion.label)         dim leftmargin integer = labelbounds.left - text_offset         dim bounds new rectangle(rowbounds.left + leftmargin, rowbounds.top, if(e.columnindex = 0, labelbounds.width, (rowbounds.width - leftmargin - text_offset)), rowbounds.height)         dim align textformatflags         select case listview.columns(e.columnindex).textalign             case horizontalalignment.right                 align = textformatflags.right                 exit select             case horizontalalignment.center                 align = textformatflags.horizontalcenter                 exit select             case else                 align = textformatflags.left                 exit select         end select         textrenderer.drawtext(e.graphics, e.subitem.text, listview.font, bounds, systemcolors.highlighttext, align or textformatflags.singleline or textformatflags.glyphoverhangpadding or textformatflags.verticalcenter or textformatflags.wordellipsis)     else         e.drawdefault = true     end if end sub  private sub listview1_drawitem(sender object, e drawlistviewitemeventargs) handles listview1.drawitem     dim listview listview = directcast(sender, listview)      ' check if e.item selected , listview has focus.     if not listview.focused andalso e.item.selected         dim rowbounds rectangle = e.bounds         dim leftmargin integer = e.item.getbounds(itemboundsportion.label).left         dim bounds new rectangle(leftmargin, rowbounds.top, rowbounds.width - leftmargin, rowbounds.height)         e.graphics.fillrectangle(systembrushes.highlight, bounds)     else         e.drawdefault = true     end if end sub 

button click handler simulate item(2) selected

private sub button1_click(sender object, e eventargs) handles button1.click     me.listview1.items(2).selected = true end sub 

this draw background color regardless of focus. have lot of control on other colors , fonts going route too.

here, button has been clicked, select item 2, while button still has focus, , item 2 selected.

enter image description here


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -