|
This describes how to integrate a Windows application (calculator) and a Web application (the Google search page) using VB.net code to tie them together.. It also demonstrates how to start OpenSpan application adapters and use them using .Net code. See the article, “Introductory Example using OpenSpan and C#” for a similar project written in C#.
This discussion assumes some familiarity with Visual Studio and VB.Net. It also assumes you know how to create adapters and how to interrogate controls.
Integrating applications requires the use of adapters which are essentially classes that describe and work with the application being integrated. The adapters in this case are placed in a single OpenSpan project.
The VB.Net code is placed in a separate Visual Studio project that is part of the same solution.
You will need to add references to your VB.Net project before it will recognize the adapters. You can do this in a couple of ways, the easiest being to press the Project menu and select Add Reference… The ones to add are:
OpenSpan
OpenSpan.Adapters
OpenSpan.Adapters.Web
OpenSpan.Adapters.Windows
You also need to add an adapter to your OpenSpan project. This is found under the Project References tab.
Next, rebuild the solution. This is important because we will start using the adapter and controls in the .Net project and we need a project reference that includes those object. When we add a project reference from the OpenSpanIntegration project to the VB_Calc_Sample project, the .Net code will recognize any controls interrogated.
Remember that if you add or remove controls or add/remove adapters, you should rebuild so that the references are also added/removed for any other projects using it.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Calc = New OpenSpanIntegration.Calculator WebSearch = New OpenSpanIntegration.WebSearch
'Handler for pressing the M+ key on calc AddHandler Calc.btnMPlus.Click, AddressOf CalcMPlusPressed
'Handler for when the web page changes AddHandler WebSearch.Search_Results.Created, AddressOf SearchResultsCreated
'By using the form (Me) as a sychronization object, we don't have to worry about 'cross-thread exceptions when an event happens on a different thread from this one. WebSearch.Start(Me)
'We don't use any events from Calc to modify the WinForm UI so we won't have cross-thread issues Calc.Start() End Sub
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing If Calc.IsRunning Then Calc.Stop() End If If WebSearch.IsRunning Then WebSearch.Stop() End If End Sub
Private Sub CalcStopped(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calc.Stopped 'If calculator is closed, close this application Application.Exit() End Sub
'When the Calculator M+ key is pressed, do a web search if the web query page can be found Private Sub CalcMPlusPressed(ByVal sender As System.Object, ByVal e As System.EventArgs) If WebSearch.query.IsCreated Then WebSearch.query.Text = Calc.txtTextBox.Text WebSearch.go.PerformClick() End If End Sub
'Displays all the links found on the search page after it loads Private Sub SearchResultsCreated(ByVal sender As System.Object, ByVal e As System.EventArgs) WebLinkList.Items.Clear() For i As Int16 = 0 To WebSearch.Google.Links.Count – 1 WebLinkList.Items.Add(WebSearch.Google.Links.Item(i).Url()) Next End Sub End Class
|