I have an ActiveX control (written in VB 6.0 or C++ maybe) that we are using as an AxInterop in a C# WinForms program. It is very much like a text box but with some special logi
You can use the WindowsApi.SetFocus method to set the focus.
This method can be used to set a focus on a specific control in an external application, so it should work in your application on a 3rd party control.
Here is another option - a working block of code to set focus for a control in an external application in winforms:
[DllImport("user32.dll")]
static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentThread();
[DllImport("user32.dll")]
static extern bool AttachThreadInput(IntPtr idAttach, IntPtr idAttachTo, bool fAttach);
[DllImport("user32.dll", SetLastError = true)]
static extern bool BringWindowToTop(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("User32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
[DllImport("user32.dll")]
static extern IntPtr SetFocus(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, string lParam);
private delegate bool EnumWindowsProc(IntPtr hWnd, string className);
[DllImport("user32.dll")]
static extern uint RealGetWindowClass(IntPtr hwnd, [Out] StringBuilder pszType, uint cchType);
void SetFocus(IntPtr hwndTarget, string childClassName)
{
// hwndTarget is the other app's main window
// ...
IntPtr targetThreadID = GetWindowThreadProcessId(hwndTarget, IntPtr.Zero); //target thread id
IntPtr myThreadID = GetCurrentThread(); // calling thread id, our thread id
bool lRet = AttachThreadInput(myThreadID, targetThreadID, true); // attach current thread id to target window
// if it's not already in the foreground...
lRet = BringWindowToTop(hwndTarget);
SetForegroundWindow(hwndTarget);
// Enumerate and find target to set focus on
EnumChildWindows(hwndTarget, OnChildWindow, childClassName);
}
List
If you don't know the classname of that textbox you can use spy++