Setting focus to a Button
-
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 -
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? thanksYou 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[^]
-
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? thanksWhy 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
-
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? thanksI 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