Sheldon Livingston
asked on
c#.NET set focus on a window?
I have written a "sendKeys" app that works well.
I'd like to further its reliability by being able to set focus on expected windows/forms.
The desktop application is written in C#.
So, for example, if the app is ready to supply a username and password to a form who's title bar reads Intuit - Quickbooks Log On, how do I make sure that that window/form has the focus?
Thank you...
I'd like to further its reliability by being able to set focus on expected windows/forms.
The desktop application is written in C#.
So, for example, if the app is ready to supply a username and password to a form who's title bar reads Intuit - Quickbooks Log On, how do I make sure that that window/form has the focus?
Thank you...
ASKER
Dorababu... how do I find the appropriate window handle? For example, I would want the log on window associated with the QuickBooks process.
Can you show some code how you are opening up the quick books thing
ASKER
Yes...
System.Diagnostics.Process.Start("\"C:\\Program Files (x86)\\Inuit\\QuickBooks 2019\\QBW32.exe\"");
System.Diagnostics.Process.Start("\"C:\\Program Files (x86)\\Inuit\\QuickBooks 2019\\QBW32.exe\"");
You want the focus back to the form from you are opening?
ASKER
I want to make sure that I have the log on form has focus before I send the username and password.
I also want to do this periodically to make sure everything is working as planned.
I also want to do this periodically to make sure everything is working as planned.
Can you try this as per required
DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
private void button1_Click(object sender, EventArgs e)
{
Process process = new Process();
process.StartInfo.FileName = @"D:\LoadTest\WriteLines1.txt";
process.Start();
process.WaitForInputIdle();
SetForegroundWindow(this.Handle);
}
ASKER
The log on form was just an example... let's say I log in and want to create an account. I push the sendkeys to do that and now I want to make sure that I am at the "create account" screen before I start sending for keys...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@jonathan... although this looks pretty cool I don't see much documentation.
The issue seems to be that sometimes the main program opened loses focus... so I want to set focus back to the main program... particularly the username text box.
The help.chm file shows lots of things in the index, but no pages pop up on the right side.
The issue seems to be that sometimes the main program opened loses focus... so I want to set focus back to the main program... particularly the username text box.
The help.chm file shows lots of things in the index, but no pages pop up on the right side.
ASKER
@Jonathan I implemented this solution and it appears to be working. Thank you!
Open in new window