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. Forcing the dotted focus box on a button?

Forcing the dotted focus box on a button?

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

    Hi, Before I go into detail, is there any way to force a button to get that dotted box on it that shows it's in focus? Just saying btn1.Focus(); isn't working for me. Btn1 is definitely the thing being focused on, but you wouldn't know by just looking at it. And it seems that the only way to get a button to show highlight is to Tab into it. So, the weird details... :doh: Say I put a custom button on my form (and gave it tabstop 1). I then add a textbox with tabstop 2. And finally another button (tabstop 3). If I click in the textbox and press the Tab key, the 2nd button gets that dotted line, no problem. Same goes if I press Shift-Tab (the 1st button shows focus). I made the textbox so that it recognizes if the up or down keys are hit, so when I'm in the textbox and press the up key, I say: if (e.KeyCode == Keys.Up){SendKeys.Send("+{Tab}");} and have no problem here either - the first button shows focus. But if down is pressed: if (e.KeyCode == Keys.Down){SendKeys.Send("{Tab}");} it freaks out and everything freezes so that I have to open task manager to close it down. One other thing - if a button has been highlighted (tabbed into) once, saying just btn1.Focus(); is enough. But, of course, I can't be sure that tab will always be pressed. Hope this makes sense. Thanks for any help on this! Mel

    M 1 Reply Last reply
    0
    • M melanieab

      Hi, Before I go into detail, is there any way to force a button to get that dotted box on it that shows it's in focus? Just saying btn1.Focus(); isn't working for me. Btn1 is definitely the thing being focused on, but you wouldn't know by just looking at it. And it seems that the only way to get a button to show highlight is to Tab into it. So, the weird details... :doh: Say I put a custom button on my form (and gave it tabstop 1). I then add a textbox with tabstop 2. And finally another button (tabstop 3). If I click in the textbox and press the Tab key, the 2nd button gets that dotted line, no problem. Same goes if I press Shift-Tab (the 1st button shows focus). I made the textbox so that it recognizes if the up or down keys are hit, so when I'm in the textbox and press the up key, I say: if (e.KeyCode == Keys.Up){SendKeys.Send("+{Tab}");} and have no problem here either - the first button shows focus. But if down is pressed: if (e.KeyCode == Keys.Down){SendKeys.Send("{Tab}");} it freaks out and everything freezes so that I have to open task manager to close it down. One other thing - if a button has been highlighted (tabbed into) once, saying just btn1.Focus(); is enough. But, of course, I can't be sure that tab will always be pressed. Hope this makes sense. Thanks for any help on this! Mel

      M Offline
      M Offline
      melanieab
      wrote on last edited by
      #2

      It just hit me that the buttons I'm using are custom and descend from the checkbox class. Hopefully this will make more sense out of things, but I'd still like to know how to make it look in focus. Thanks! Mel

      M 1 Reply Last reply
      0
      • M melanieab

        It just hit me that the buttons I'm using are custom and descend from the checkbox class. Hopefully this will make more sense out of things, but I'd still like to know how to make it look in focus. Thanks! Mel

        M Offline
        M Offline
        microsoc
        wrote on last edited by
        #3

        hi! :) you can try this one: protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if(this.Focused) { Rectangle rec = new Rectangle(1, 1, Width - 2 , Height - 2); } ControlPaint.DrawFocusRectangle(e.Graphics, rec, ForeColor, BackColor); } } hope that helps! :)

        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