vb.net - How to set a listview FocusedItem programatically -


how can set focuseditem property programatically?

i've tried far no success:

if lvw.focuseditem nothing     if lvw.items.count > 0     lvw.focus()     lvw.hideselection = false     lvw.items(0).selected = true     lvw.items(0).focused = true     lvw.focuseditem = lvw.items(0)     lvw.select()     end if end if 

btw form listview has not called showdialog method yet. can reason not work?

you have understand each control on form , form window , window have focus must first created , assigned handle.

for basic description of this, refer to: all handles in windows forms following excerpts referenced article.

what handle?

a handle (hwnd) return value createwindowex windows operating system uses identify window. "window" in win32 broader concept may think - each individual button, combobox, listbox etc comprises window. (for more information see window classes ) note: there other things known "handles" in framework - e.g. gdi handles bitmap or handles device contexts (hdcs) - article discusses hwnds only.

...

when control create handle? (when control call createwindowex?)

a control tries as possible defer creating handle. because setting properties forces chatty interop between clr , user32.

typically handles controls created before form.load event called. handles can created if "handle" property called , handle has not yet been created, or createcontrol() called.

so window's handle not created when instantiate control. however, can force control create handle referencing handle property.

so if first listview create handle, when set properties wanted.

dim f2 new form2 ' not need condition, here demonstration purposes ' can step through code in debugger , observe ' code execution. if not f2.listview1.ishandlecreated    ' retrieval of handle cause handle created    ' if has not yet been created    ' if delete if-then block, need retain     ' following statement     dim h intptr = f2.listview1.handle end if  f2.listview1.focuseditem = f2.listview1.items(2) f2.listview1.items(2).selected = true f2.listview1.items(2).focused = true f2.activecontrol = f2.listview1 f2.showdialog() 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -