user1167662
user1167662

Reputation: 1265

How to find and reference a certain form? (VB.NET)

I am currently making an application to aid me in testing another separate application (both are made in VB.NET). I want my tester app to be able to enter info in a form on my other application. Since I have familiarity with both applications, I know the name and everything about the form I want to do things with on my testee app. My problem, is that I need a way to use this information of the testee form to get a handle or something that I can actually reference that form with in code for my tester app.

How can I go about getting this form? my only experience with something similar was getting the handle of a main window of a process, but that was a bit easier by doing this:

Dim iTunesID as intptr
    for each process as process in process.getprocesses
if proc.mainwindowtitle = "iTunes" then iTunesID = proc.MainwindowHandle
next

However even that I am not really happy with because it needs to iterate through every process. and even from this, I am not really sure how to reference my specific window, since it isn't the main window in this case. I have tried searching for how to do this, but my search is a bit too vague I think as I just don't know what methods are available to use to do this or assist in doing it.

my first thought was something similar to above but with something like "for each form as forms.form in 'forms.formsrunning'" kind of thing, but I don't even know a command to get forms running, and though I guess I could probably find one, I am hoping for a better solution than iterating through all processes/forms.

Thanks for any help in advance! :)

Upvotes: 0

Views: 1498

Answers (2)

ClockEndGooner
ClockEndGooner

Reputation: 671

If I'm following your questions correctly, the following VB.NET code fragments may be of help:

Dim procNameSought As String = "Notepad2"

'
' Use LINQ to get the Process ID for the Application whose window should
' be brought into focus if you don't already have the Process ID...
'
Dim processID As Integer = (From proc As Process In Process.GetProcesses
                            Where proc.ProcessName.Trim() = procNameSought
                            Select proc.Id).FirstOrDefault()

'
' Use AppActivate to bring the Main Window for the Process into Focus.... 
'
Microsoft.VisualBasic.AppActivate(processID)

If the main window for the application is not minimized to the Task Bar, (e.g., the main window is hidden by another application), it will be brought to the foreground and have focus. However, if the Process's Main Window has been minimized to the Task Bar, you may need to look at using PInvoke to call the Windows API Function SetActiveWindow (http://msdn.microsoft.com/en-us/library/ms939986.aspx).

caeasy's explanation here covers one approach on how to do this: Correct way (in .NET) to switch the focus to another application

For using SetActiveWindow(), you need to specify the instance of the Windows Handle you're trying to set focus to, which can be obtained through the following calls:

'
' 32-Bit Window Handle : You may need to change this over to Int64 if 
' you're running on a 64-Bit OS...
'
Dim windowHandle As Int32 = (From proc As Process In Process.GetProcesses
                             Where proc.ProcessName.Trim() = procNameSought
                             Select proc.MainWindowHandle).FirstOrDefault()

Dim winHandlePtr As New IntPtr(windowHandle)

I hope this was of help and interest...

Upvotes: 1

Max
Max

Reputation: 7586

If you have some time, you can take a look at Hawkeye source (http://hawkeye.codeplex.com/) This is an opensource utility to hook up into a running .Net application, and change property value, call method, etc...

Hawkeye actually hook up to a running application by a point-and-click action.

You could find the running application using a loop on existing process, then maybe you can use some know-how from the Hawkeye source-code to hook into this application and start changing values, call methods, etc...

Upvotes: 0

Related Questions