vb.net - How do I detect if a panel is in the viewable window? -
using winforms , vb.net vs 2012:
if have large scrollable panel , put bunch of smaller panels inside larger one. when scroll larger panel or down, of smaller panels scroll out of view. question is, can put listener on smaller panels smaller panel knows when has scrolled out of view?
i can't use .visible property because set true when panel not in window. know can set .focus property bring view, can still lose focus when in viewable window else takes focus.
so there property or can listen detects when panel has scrolled out of view? , if not, there else can detect this?
thanks.
from scroll() , mousewheel() events of panel, use code this:
private sub panel1_scroll(sender object, e scrolleventargs) handles panel1.scroll checkvisibility(sender) end sub private sub panel1_mousewheel(sender object, e mouseeventargs) handles panel1.mousewheel checkvisibility(sender) end sub private sub checkvisibility(byval pnl panel) dim results new list(of string) dim pnlrectscreen rectangle = pnl.parent.rectangletoscreen(pnl.bounds) each child control in pnl.controls dim childrectscreen rectangle = pnl.rectangletoscreen(child.bounds) if pnlrectscreen.intersectswith(childrectscreen) if pnlrectscreen.contains(childrectscreen) results.add(child.name & ": visible") else results.add(child.name & ": partially visible") end if else results.add(child.name & ": not visible") end if next listbox1.datasource = nothing listbox1.datasource = results end sub
if want know when becomes hidden only, track current visibility dictionary() , when changes hidden. here's more robust example:
public enum controlvisibility hidden partialyvisible fullyvisible end enum private visibiltystate new dictionary(of control, controlvisibility) private sub form1_shown(sender object, e eventargs) handles me.shown dim pnl panel = panel1 dim pnlrectscreen rectangle = pnl.parent.rectangletoscreen(pnl.bounds) each child control in pnl.controls dim childrectscreen rectangle = panel2.rectangletoscreen(child.bounds) if pnlrectscreen.intersectswith(childrectscreen) if pnlrectscreen.contains(childrectscreen) visibiltystate.add(child, controlvisibility.fullyvisible) else visibiltystate.add(child, controlvisibility.partialyvisible) end if else visibiltystate.add(child, controlvisibility.hidden) end if next end sub private sub panel1_scroll(sender object, e scrolleventargs) handles panel1.scroll checkvisibility(sender) end sub private sub panel1_mousewheel(sender object, e mouseeventargs) handles panel1.mousewheel checkvisibility(sender) end sub private sub checkvisibility(byval pnl panel) dim newvisibility controlvisibility dim pnlrectscreen rectangle = pnl.parent.rectangletoscreen(pnl.bounds) each child control in pnl.controls dim childrectscreen rectangle = pnl.rectangletoscreen(child.bounds) if pnlrectscreen.intersectswith(childrectscreen) if pnlrectscreen.contains(childrectscreen) newvisibility = controlvisibility.fullyvisible else newvisibility = controlvisibility.partialyvisible end if else newvisibility = controlvisibility.hidden end if ' has changed? if visibiltystate(child) <> newvisibility andalso newvisibility = controlvisibility.hidden debug.print(child.name & " has become hidden: " & datetime.now.tostring("h:mm:ss.ffff")) end if visibiltystate(child) = newvisibility next end sub
Comments
Post a Comment