Drawing the focus rectangle for a button
-
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 sayingGraphics.FromHwnd(button2.Handle)
, I saide.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 -
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 sayingGraphics.FromHwnd(button2.Handle)
, I saide.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, 2006Why 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