Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. MouseEnter Control

MouseEnter Control

Scheduled Pinned Locked Moved C#
questiongraphics
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Luther Baker
    wrote on last edited by
    #1

    I'm building a custom control and I want it to change borders when a MouseEnter events happens. a) Can I simply paint the border in the MouseEvent method? OR b) Must I set a flag and call Invalidate(), and wire OnPaint to draw the new/old border appropriately? If a) is ok, then how do I get a graphics instance in the MouseEnter event. Or, is b) the preferred method to do this? Thanks, -Luther

    H 1 Reply Last reply
    0
    • L Luther Baker

      I'm building a custom control and I want it to change borders when a MouseEnter events happens. a) Can I simply paint the border in the MouseEvent method? OR b) Must I set a flag and call Invalidate(), and wire OnPaint to draw the new/old border appropriately? If a) is ok, then how do I get a graphics instance in the MouseEnter event. Or, is b) the preferred method to do this? Thanks, -Luther

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Because there are several things that can happen while your mouse is moving or resting comfortably in the bounds of your control, it'd probably be best to set a flag (ex: set to true) and call Invalidate() in the handler for MouseEnter event, and to clear the flag (ex: set to false) in the handler for the MouseLeave event. Of course, your override for OnPaint (note, this is an override, not an event handler, which are typically wired for other controls - use overrides when possible to override functionality from the base class) should draw the border appropriately depending on the flag. The first method is possible (see Control.CreateGraphics) but is too problematic since MouseEnter only fires once. Your border would get painted, sure, but then any successive painting required won't repaint the border.

      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

      L 1 Reply Last reply
      0
      • H Heath Stewart

        Because there are several things that can happen while your mouse is moving or resting comfortably in the bounds of your control, it'd probably be best to set a flag (ex: set to true) and call Invalidate() in the handler for MouseEnter event, and to clear the flag (ex: set to false) in the handler for the MouseLeave event. Of course, your override for OnPaint (note, this is an override, not an event handler, which are typically wired for other controls - use overrides when possible to override functionality from the base class) should draw the border appropriately depending on the flag. The first method is possible (see Control.CreateGraphics) but is too problematic since MouseEnter only fires once. Your border would get painted, sure, but then any successive painting required won't repaint the border.

        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

        L Offline
        L Offline
        Luther Baker
        wrote on last edited by
        #3

        The flag method works cleanly - I typically try to avoid saving and polling state like that ... but practically speaking, I assume it makes sense in this stateful, event driven world. I also appreciate your comment regarding overrides vs handlers. It wasn't concretely clear to me when to use which. I see that for every child control I add to a parent, I could add a handler. Self-documenting and a bit easier to organize. Whereas, the control's own painting naturally belongs in the override. Thanks! -Luther

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups