How do I remotely trigger the calculation of a text box

Asked By 260 points N/A Posted on -
qa-featured

Our office is using a finance application. It contains a big worksheet to calculate the pay for a given employee. It contains different variables for benefits calculation. I wrote a program to directly modify the database field values directly. But the problem is that the screen contains the logic to calculate the final calculation.

When I re-open the screen after changing values in the database, I can see the values changed but the calculation is still old. If I click on a field and press the tab key, the form re-calculates the value. Is it possible to write a simple tool to automate the process on a click of a button?

SHARE
Best Answer by Edvard
Answered By 0 points N/A #101734

How do I remotely trigger the calculation of a text box

qa-featured

You can remotely automate an application by sending messages to the target application. The requirement is that the application needs to support listening for messages. This is called Windows Messages. Each application written for a Graphical User Interface, contains a global loop that listens to incoming input. Since the inputs are serial i.e. only one input at a time, the interface executes commands based on the input.

For example you can simulate a keyboard entry or a mouse click by sending the required input device message via programming code. If your finance application is running on Windows, it is possible to send the message to a specific window. This is done by using the Windows Messaging Application Programming Interface.

You need to target the window, then send the required message with the correct parameters. So it is possible to write a simple tool to automate the process via a click of a button.

Answered By 0 points N/A #101735

How do I remotely trigger the calculation of a text box

qa-featured

You will need to send the "on focus" and "lost focus" messages to the targeted text boxes. Each text box is actually a window by itself. It will be specified as an "Edit" control, which is the base class for editable text boxes. When you click on a text box, the text box gets the focus. When you press the tab button on the keyboard, two things happen simultaneously: the focus of the present text box is terminated and the next control in sequence gets the focus.

The order of the focus depends on the "tab index" property added by the programmer. Therefore, the focus on the next control is not actually what you see on the screen, rather it's the order in which the developer had designed the screen.

The tab order or the focus of screen objects can be defined via Visual Studio .Net. Presuming you are using the same. The following screen shot shows the tab order of a screen at design time. Notice that, unless the developer explicitly specifies the tab order, the focus of the next object may differ.

Answered By 260 points N/A #101736

How do I remotely trigger the calculation of a text box

qa-featured

How to get the target window class? I checked out the Windows Application Programming Interface and it does talk about getting a handle to a window.

There seems to be a method of getting a handle by giving the title of the program that is currently running. It requires a second parameter of a class name. Is this class name "Window" or is it something else?

Best Answer
Best Answer
Answered By 0 points N/A #101737

How do I remotely trigger the calculation of a text box

qa-featured

The window class of an active window can be taken by using "Spy++". This tool is shipped with Microsoft Visual Studio. Spy++ shows all the windows' handles of all active applications on your desktop. If you know the title of the window, do a search on the result. The corresponding active class name is displayed right after the title.

The following screenshot displays the windows currently active on my desktop. Notice the title of the Google Chrome application and its corresponding windows class name. The class name will differ from application to application. Please note that you may come across unexpected class names such as "#32323", but these are actually valid class names as presented to the Microsoft Windows Operating System. I have circled in green the corresponding window class name in the Spy++ output of the screen shot.

 

Answered By 0 points N/A #101738

How do I remotely trigger the calculation of a text box

qa-featured

Using SPY++, you can get the class name of a currently running application. You will need to use the title text in order to get the correct window. Having done that, you need to be aware that apart from the windows handle the class name is constant for all the text boxes on the screen.

If you are attempting to target a specific instance, you need to do a loop and obtain the handles of all controls of a specified class to a list. The list index position will correspond to the correct instance position. The position of the text box handle does not change on each application instance. Therefore it is safe to collect the handles when you enumerate the current windows into a list and refer it by the indexed position.

I have attached a screen shot of the SPY++ output for the previous posting. You will notice that there are two text box instances. Each "position" is a constant on each application invocation. Therefore you can target the respective handle by getting the index position of the collected handles.

Answered By 260 points N/A #101739

How do I remotely trigger the calculation of a text box

qa-featured

That is interesting. Having collected the windows' handles and the corresponding index position, which message should I send to execute a "tab" event?

I noticed that I can also use the "Search" tool of the SPY ++ in order to search for the corresponding control in the target application. Now, what is the message that I should use to trigger the calculation? Is it a keyboard event or a mouse click event?

Answered By 0 points N/A #101740

How do I remotely trigger the calculation of a text box

qa-featured

You can use the "kill focus" and "get focus" Windows API calls instead of a keyboard or a mouse click event.

SendMessage(controlHandle, WM_SETFOCUS, 0, 0)
SendMessage(controlHandle, WM_KILLFOCUS, 0, 0)

The WM_SETFOCUS and WM_KILLFOCUS will set the focus on the text box and then kill it respectively. It does not take any parameters, only the windows handle of the target control. The constants are of hexadecimal value and is specified as below.

Const WM_SETFOCUS = &H7
Const WM_KILLFOCUS = &H8

You need to remember again, that if you miss the subsequent API method, your text box will be in focus state and your calculations will not trigger. It does not matter if the correct text box has focus when you send the "set focus" message. There are no side effects of the same. I would advise you to check the sequence of the finance application and then execute the focus events on the respective text boxes.

Answered By 260 points N/A #101741

How do I remotely trigger the calculation of a text box

qa-featured

Using the SET FOCUS and KILL FOCUS events worked on my sample prototype! Thank you experts for the information and expert guidance provided. The finance application now re-calculates the correct values when I use my program. The next stage is to automate the entire process.  Well, I will start doing it now and will post here if I encounter any problems.

Thank you again!

Related Questions