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. Setting focus to a Button

Setting focus to a Button

Scheduled Pinned Locked Moved C#
data-structurestutorialquestion
4 Posts 4 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.
  • D Offline
    D Offline
    danielk_
    wrote on last edited by
    #1

    I have a windows form with some buttons and I am having problems setting focus on the buttons. Basically an array of buttons gets passed in to the the form along with a default button as follows: public void Show (Button defaultSelectedButton, Buttons[] btns) { foreach (Button btn in btns) pnlButtons.Controls.Add (btn); //pnlButtons is a Panel defaultSelectedButton.Focus(); } however defaultSelectedButton.Focus(); does not seem to have any effect and the button with focus is always the first button that was added to pnlButtons. Does anyone know how to apply focus to a particular button? thanks

    T A U 3 Replies Last reply
    0
    • D danielk_

      I have a windows form with some buttons and I am having problems setting focus on the buttons. Basically an array of buttons gets passed in to the the form along with a default button as follows: public void Show (Button defaultSelectedButton, Buttons[] btns) { foreach (Button btn in btns) pnlButtons.Controls.Add (btn); //pnlButtons is a Panel defaultSelectedButton.Focus(); } however defaultSelectedButton.Focus(); does not seem to have any effect and the button with focus is always the first button that was added to pnlButtons. Does anyone know how to apply focus to a particular button? thanks

      T Offline
      T Offline
      Thomas Stockwell
      wrote on last edited by
      #2

      You might try first changing the foreach statement to just a for statement. Foreach statements have an odd way of going through the order of the buttons. If that doesn't do anything, if the for loop you might try comparing a unique aspect of the defaultSelectedButton and a button in the btns array (I would use the Text property). If the unique aspects are the same then use focus. If neither of those suggestions work, post back and I will try to find another alternative.

      Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my homepage Oracle Studios[^]

      1 Reply Last reply
      0
      • D danielk_

        I have a windows form with some buttons and I am having problems setting focus on the buttons. Basically an array of buttons gets passed in to the the form along with a default button as follows: public void Show (Button defaultSelectedButton, Buttons[] btns) { foreach (Button btn in btns) pnlButtons.Controls.Add (btn); //pnlButtons is a Panel defaultSelectedButton.Focus(); } however defaultSelectedButton.Focus(); does not seem to have any effect and the button with focus is always the first button that was added to pnlButtons. Does anyone know how to apply focus to a particular button? thanks

        A Offline
        A Offline
        Anthony Mushrow
        wrote on last edited by
        #3

        Why no use AddRange for adding the buttons, then you don't need a loop at all. And of course, make sure that defaultSelectedButton is actually one of the buttons in the array, rather than a copy of. I've just done a test and dynamically added a button to a form, then called myButton.Focus() and the newly added button does indeed get focus, so check up on the defaultSelectedButton, because it should be working.

        My current favourite word is: Bacon!

        -SK Genius

        1 Reply Last reply
        0
        • D danielk_

          I have a windows form with some buttons and I am having problems setting focus on the buttons. Basically an array of buttons gets passed in to the the form along with a default button as follows: public void Show (Button defaultSelectedButton, Buttons[] btns) { foreach (Button btn in btns) pnlButtons.Controls.Add (btn); //pnlButtons is a Panel defaultSelectedButton.Focus(); } however defaultSelectedButton.Focus(); does not seem to have any effect and the button with focus is always the first button that was added to pnlButtons. Does anyone know how to apply focus to a particular button? thanks

          U Offline
          U Offline
          Urs Enzler
          wrote on last edited by
          #4

          I just wanted to add that - foreach loops the same way as you would probably yourself with a for loop (nothing strange about it, really) --> keep the foreach, because it is better readable - Focus should work, to debug the problem, try to have a look at the ActiveControl property of your form or user control to see, which control has currently the focus and whether that control has a validate method that prevents setting of the focus to the button. Urs

          -^-^-^-^-^-^-^- no risk no funk

          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