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. Drawing the focus rectangle for a button

Drawing the focus rectangle for a button

Scheduled Pinned Locked Moved C#
graphicsjsonquestiondiscussion
2 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.
  • M Offline
    M Offline
    melanieab
    wrote on last edited by
    #1

    Hi, I'm trying to draw a focus rectangle on a button, since, when I load a tabpage, if the first control on that page is a button, it doesn't highlight and you can't tell that it has focus. So I stumbled across the ControlPaint.DrawFocusRectangle command and put this in after loading the rest of the page: int x = button2.ClientRectangle.Location.X + 3; int y = button2.ClientRectangle.Location.Y + 3; int w = button2.ClientRectangle.Width - 6; int h = button2.ClientRectangle.Height - 6; Rectangle r = new Rectangle(x,y,w,h); ControlPaint.DrawFocusRectangle(Graphics.FromHwnd(button2.Handle), r, Color.Red, Color.Red); But I get nothing. Then I added a paint event for the button and moved the code there, although instead of saying Graphics.FromHwnd(button2.Handle), I said e.Graphics. Now it worked, but I'm not able to move away from the button using the up/down arrows. If I throw a messagebox in, after I click the OK button, I can move around without trouble. It seems to have something to do with returning control to the system. Is there anything I can say to give control back to the system? Or am I doing stuff completely wrong? Thanks so much for any thoughts!!!!!! Mel -- modified at 12:18 Monday 1st May, 2006

    J 1 Reply Last reply
    0
    • M melanieab

      Hi, I'm trying to draw a focus rectangle on a button, since, when I load a tabpage, if the first control on that page is a button, it doesn't highlight and you can't tell that it has focus. So I stumbled across the ControlPaint.DrawFocusRectangle command and put this in after loading the rest of the page: int x = button2.ClientRectangle.Location.X + 3; int y = button2.ClientRectangle.Location.Y + 3; int w = button2.ClientRectangle.Width - 6; int h = button2.ClientRectangle.Height - 6; Rectangle r = new Rectangle(x,y,w,h); ControlPaint.DrawFocusRectangle(Graphics.FromHwnd(button2.Handle), r, Color.Red, Color.Red); But I get nothing. Then I added a paint event for the button and moved the code there, although instead of saying Graphics.FromHwnd(button2.Handle), I said e.Graphics. Now it worked, but I'm not able to move away from the button using the up/down arrows. If I throw a messagebox in, after I click the OK button, I can move around without trouble. It seems to have something to do with returning control to the system. Is there anything I can say to give control back to the system? Or am I doing stuff completely wrong? Thanks so much for any thoughts!!!!!! Mel -- modified at 12:18 Monday 1st May, 2006

      J Offline
      J Offline
      Josh Smith
      wrote on last edited by
      #2

      Why don't you just call the button's Select method in the form Load handler, and then set the form's ActiveControl to the button? That will select the button and give it a focus rect. Josh

      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