|
A common situation is getting information from a Listview control of some application. The techniques shown here also apply to other controls.
This example uses the Task Manager application that comes with Windows as an example and shows how to read information from that application and then manipulate it in your own custom application.
The attached sample has all the code for this project. Here we’ll highlight some of the interesting portions of it.
The main points of using OpenSpan with .Net demonstrated here are:
- Starting and stopping an application using an adapter
- Reading from a Listview control by treating it as a datasource
The example has a WinForm that copies information from the Task Manager:

The application we are integrating with, Task Manager, is a standard Windows application.
Starting the Task Manager Application
There is an important change in how the application is started compared to the earlier project.
if ( !m_TaskManager.IsRunning ) { m_TaskManager.Start(); }
The Start()method can take a parameter which provides a synchronization object so that events coming in on other threads are automatically marshaled to the UI thread. However we don’t need to do so since we are not handling events from the Task Manager application. If we were, then we could use the this object of the WinForm to be used to synchronize events to the UI thread. Doing this allows the OpenSpan adapters to automatically marshal their events to your UI so you don’t have to.
You only need to do this for adapters which you handle events from and that the event handlers modify UI items. Since neither of those conditions are true, we just use Start().
Working with the ListView control
What makes this interesting is that, in a sense, there isn’t anything to show outside of how to start the Task Manager application. This is because once it’s started, we work with the ListView as though it were an object in our own C# project. Some of the methods are different from a standard ListView, in fact some are more powerful than what you get from .Net.
For instance in the following routine, you see code that uses the m_TaskManager adapter to assign a DataTable to the dataGridView we are using on the WinForm. That single line of code causes the information to be copied from one application to the other. The rest of the code then writes a message indicating how many rows and columns there are:
/// /// This automation sends the contents of the ListView from /// the Processes tab of the Task Manager /// to a DataGrid on a Windows Form. /// The automation also determines a count of the total number /// of rows returned for the ListView and generates /// a message if no rows are returned. /// The number of rows is displayed on the Win Form. /// private void SendListViewtoDataGrid() { // Populates the Data Grid View with data from Task Manager. dataGridView1.DataSource = m_TaskManager.listView.DataTable;
// Update windows form based on number of rows returned. int numRows = m_TaskManager.listView.DataTable.Rows.Count; if ( numRows > 0 ) { label8.Text = "The Process Grid contains " + numRows + " rows"; } else { label8.Text = "Task Manager returned No Processes"; } }
Likewise the other parts of the code search for items in the ListView such as :
txtRowDataResults.Text = m_TaskManager.listView.GetItemFullText(rowIndex,numColumns);
to read a line of text from the other application.
When you run the application, you should also select a different tab in the Task Manager and then refresh the dataGrid on the C# application. When you do this, it just works. The OpenSpan adapter is reading the data from the listview and not directly from the screen.
Once you’ve created the adapter to the application, using the controls in it is like using objects in any other .Net application and its easy in some cases to not need to know that the objects you are working with, are in fact part of a separate application for which you don’t have the source code.
|