vb.net - MouseLeave and Enter on Form -
i trying make form fade away taskbar if user not hovering on form mouse. (the form contains hyperlinks). on adverse, want form reset original position if mouse comes form. however, whatever reason, seems thought enter , leave evens fire in sync when either occurs. if leave form mouse, both events fire. if enter form, both events fire. wrong?
sub formleave() mouseform = false until y = screen.primaryscreen.workingarea.height + 50 sleep(10) y = y + 1 me.location = new point(x, y) if mouseform = true exit sub end if loop end sub sub formenter() mouseform = true me.visible = true x = screen.primaryscreen.workingarea.width - me.width y = screen.primaryscreen.workingarea.height - me.height me.location = new point(x, y) end sub
animation best done timer (i.e. event driven) rather sleeping.
you have make sure cursor has left boundary of form, leave event fired on form when mouse enters control within form.
i added longer delay when mouse leaves form otherwise annoying.
to demonstrate, use code new windows forms project. add controls form if want confirm not misbehave when put mouse on control on form.
public class form1 dim x integer dim y integer dim tim timer sub moveformaway(sender object, e eventargs) ' called on timer tick event dim desty = screen.primaryscreen.workingarea.height - 100 if y >= desty tim.enabled = false exit sub end if ' interval form position changes tim.interval = 10 y += 2 me.location = new point(x, y) end sub sub formbodyleave(sender object, e eventargs) ' if mouse has not left outside of form, ' i.e. has entered control on form, exit: if me.clientrectangle.contains(pointtoclient(cursor.position)) exit sub end if if tim nothing orelse not tim.enabled x = me.location.x y = me.location.y tim = new timer ' initial interval until form starts running away tim.interval = 1000 addhandler tim.tick, addressof moveformaway tim.enabled = true end if end sub sub formbodyenter(sender object, e eventargs) if tim isnot nothing tim.enabled = false end if movetodefaultposition() end sub sub addformdisappearinghandlers() addhandler me.mouseenter, addressof formbodyenter addhandler me.mouseleave, addressof formbodyleave end sub sub movetodefaultposition() x = screen.primaryscreen.workingarea.width - me.width y = screen.primaryscreen.workingarea.height - me.height me.location = new point(x, y) end sub private sub form1_load(sender object, e eventargs) handles mybase.load movetodefaultposition() addformdisappearinghandlers() end sub private sub form1_formclosing(sender object, e formclosingeventargs) handles mybase.formclosing ' tidy up. if tim isnot nothing tim.dispose() end if end sub end class
Comments
Post a Comment