-
August 16th, 2010, 03:12 PM
#1
[RESOLVED] How to return keyboard focus to topmost form?
Developing in Visual C# 2008 Express Edition.
My Windows Form program floats a small form containing a textbox on top of a commercial graphics application. The form communicates with the graphics software via its API and is kept on top by: 'this.TopMost = true;'.
When I click on a graphical object, some associated data appears in my textbox. When I type a new value in the textbox and hit enter, the value is transferred to the graphical object.
This all works OK, except when I click on the graphics window my form loses the keyboard focus so I have to click on the form before I can enter a new value. In an attempt to get the focus to return automatically I have tried running a 250ms timer routine to execute 'this.Focus();' or 'myTextBox.Focus();' without success.
How can I get the keyboard focus to return to my form automatically?
Thanks,
Kevin
-
August 16th, 2010, 08:37 PM
#2
Re: How to return keyboard focus to topmost form?
Sounds like you have a Modeless dialog, when what you really want is a Modal dialog. Could you pop up the child window using "ShowDialog" instead of "Show"?
See here for more info.
This will prevent the parent window from being focused until the child window is closed like how "Open File" dialog works.
Would this work for you?
An alternative might be to handle OnGotFocus in the parent form and call childWindow.Focus() when it receives focus.
Last edited by enfekted; August 16th, 2010 at 08:41 PM.
-
August 17th, 2010, 03:34 AM
#3
Re: How to return keyboard focus to topmost form?
Thanks for the reply Enfekted,
I don't think I can use either of your suggestions because my form is not a child of the underlying graphics program (GP). The GP API has an interface only for VBscript so I use a button in the GP window to launch a VBscript which then launches my C# .exe file. The VBscript then passes communication between the GP and my C# form.
I did try using 'ShowDialog' to display my form, but then it failed to stay on top of the GP when I selected a graphical object. If it had stayed on top I think it would have prevented selection of a graphical object in the window below.
In case you are wondering why I don't do everything in the VBscript, I could not find a method of keeping a form on top using VBscript and hoped for success using C#.
Kevin
-
August 17th, 2010, 07:06 AM
#4
Re: How to return keyboard focus to topmost form?
Have you tried any of the following APIs :
BringWindowToFront
SetWindowPos
SetTopmostWindow
?
-
August 17th, 2010, 09:37 AM
#5
Re: How to return keyboard focus to topmost form?
In vb6 there was a function to activate an application that would likely handle what you need here. I have not looked to see if this is still supported in VB.Net or C# but may be worth a look.
Always use [code][/code] tags when posting code.
-
August 17th, 2010, 05:13 PM
#6
Re: How to return keyboard focus to topmost form?
Thanks Hannes & DataMiser for showing the way.
The VB6 method I found used SetWindowPos. From http://www.pinvoke.net I found how to implement it in C# ... but it did not have the desired effect. While searching for BringWindowToFront and SetTopmostWindow I discovered SetForegroundWindow which with GetForegroundWindow does exactly what I want.
For the benefit of anyone with a similar problem, this is based on code copied from http://www.pinvoke.net :
Code:
using System.Runtime.InteropServices;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
static extern IntPtr GetForegroundWindow();
static void restoreKbFocus(IntPtr hWnd)
{
if (GetForegroundWindow() != hWnd)
{
SetForegroundWindow(hWnd);
}
}
void timer_Tick(object sender, EventArgs eArgs)
{
if (sender == timer)
{
restoreKbFocus(this.Handle);
}
}
The timer routine executes restoreKbFocus every 250ms so the textbox is ready to receive keyboard input immediately after I click on the underlying graphics program.
-
August 18th, 2010, 01:04 AM
#7
Re: How to return keyboard focus to topmost form?
Good Work!
Please remember to mark your thread resolved. To do this, follow these steps :
Click Thread Tools ( above your first post )
Click Mark Thread Resolved.
Thanks,
Hannes
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|