Hi there, I need some help. I have a windows form, that is opened by a system-wide hotkey. When the form is opened, I want it to be put on top of all windows, and give focus to the first TextBox in the Form, so one can start typing after opening the window up, with no mouse interaction. Thing is, the code I have DOES work, just not always. That's odd, but there's more, the control appears to have focus (the window is the active one, judging by the title bar, AND there's a "beam" blinking in the TextBox)... but the text goes in the previously selected window, such as the .cs file I was editing in VS.NET before pressing the global hotkey. Any ideas? The little code I have, so far, is:
Code Snippet
private void SearchWindow_Load(object sender, EventArgs e) { this.BringToFront(); this.Focus(); this.myTextBox.Focus(); } Needless to say, the function is the event handler for "Load". | | exscape Sunday, April 22, 2007 6:19 PM | OK, I "solved" it. Ugly solution, but it works... When the window is first created, I Show() it and hide it at once again. That way, the first time the user activated it actually the second - and the problem was solved.
A better solution would be appreciated, but I'm not sure that you can figure it out without me posting a whole lot of extra code. It's not necessary to "solve it further", though, so I'll leave it at this.
| | exscape Wednesday, April 25, 2007 6:31 PM | You can't give the TextBox the focus in the Load event, it is not yet visible. Instead:
this.myTextBox.Select();
| | nobugz Sunday, April 22, 2007 6:56 PM | Where should I put that? If I simply replace the Focus() line, it doesn't work any better.
But, if you by 'visible' mean that the controls aren't initialized/doesn't exist yet then I suppose that's no good, anyway.
Can I put it anywhere else?
| | exscape Sunday, April 22, 2007 7:06 PM | You can try instead of show the form and hide it when is not needed and show it again, to use the form as dialog, and always when you need that form to open the form as a dialog using ShowDialog method. Then you will not nead Focus method, if that text box has tabindex = 0. | | boban.s Sunday, April 22, 2007 8:55 PM | Override the form's OnShown() method, and put it in there:
protected override void OnShown(EventArgs e) { base.OnShown(e); textBox.Focus(); }
Note: It is better to override the virtual methods like that (if you can), instead of subscribing to an event. Subscribing to an event adds unneccessary overhead.
(The MS documentation says: "The OnShown method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.") | | Matthew Watson Monday, April 23, 2007 9:35 AM |
| Matthew Watson wrote: |
Override the form's OnShown() method, and put it in there:
protected override void OnShown(EventArgs e) { base.OnShown(e); textBox.Focus(); }
Note: It is better to override the virtual methods like that (if you can), instead of subscribing to an event. Subscribing to an event adds unneccessary overhead.
(The MS documentation says: "The OnShown method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.") | | Thanks; however, this does not help either, the same problem remains. After some further testing I've realized that the problem only occurs once. The second time the window is opened, it always works. Same with the third, fourth etc. It keeps on working until the process/program is restarted. After restarting, it goes back to being broken the first time it's opened. | | exscape Monday, April 23, 2007 11:37 AM | Strange - it works fine for me.
I just made a default windows forms app and added a textbox and the code above. Then created a shortcut to the executable and set a hotkey for the shortcut (Ctrl+Alt+L).
When I press the hotkey, the application starts, and the text box has the focus and I can start typing into it.
Are you doing anything else that might upset things?
| | Matthew Watson Monday, April 23, 2007 12:31 PM |
| Matthew Watson wrote: |
Strange - it works fine for me.
I just made a default windows forms app and added a textbox and the code above. Then created a shortcut to the executable and set a hotkey for the shortcut (Ctrl+Alt+L).
When I press the hotkey, the application starts, and the text box has the focus and I can start typing into it.
Are you doing anything else that might upset things?
| | The form isn't the default one, it is opened in code by the application (RegisterHotKey is used for the hotkeys). The hotkey system is hardly related to the problem, though, I've been using it for a year or so. Anyway, it has to be something with the initialization since it's only broken the first time since the app is started. I'll tr to find something more, if I can. | | exscape Monday, April 23, 2007 12:43 PM | OK, I "solved" it. Ugly solution, but it works... When the window is first created, I Show() it and hide it at once again. That way, the first time the user activated it actually the second - and the problem was solved.
A better solution would be appreciated, but I'm not sure that you can figure it out without me posting a whole lot of extra code. It's not necessary to "solve it further", though, so I'll leave it at this.
| | exscape Wednesday, April 25, 2007 6:31 PM | Just put this in the form shown event:
| | mcnamaragio Wednesday, April 25, 2007 7:02 PM | You could also simply set that text boxes "Tab Index" to 0. This means that is will be the default control that has the focus when the form is loaded. Works well for me and minimises coding. | | SiN_1 Wednesday, June 13, 2007 3:59 AM |
|