Tech Articles


Supporting Events from .NET Visual Components in PowerBuilder


This article seems like it should be the fourth in a series of articles. The first two were on non-visual components in August 2006 and July of 2007. The last one was in August of 2007. In that one, we looked at using the Interop Forms Toolkit to provide a COM wrapper for Visual .NET components - essentially making them ActiveX controls - so that PowerBuilder could use them. That article focused primarily on getting the visual component to display within PowerBuilder and being able to invoke functions on it. What we didn't look at then was allowing PowerBuilder to respond to events on the visual component. That's what we'll look at in this article.

Since Microsoft introduced the Interop Forms Toolkit for Classic Visual Basic users, it implements support for events in a method that Classic Visual Basic supports (using the WithEvents keyword) but PowerBuilder doesn't. In order to make the event visible to PowerBuilder, we need to define an interface with the event declarations and then indicate that the wrapper .NET component implements that interface. As daunting as that sounds, it's actually fairly simple and I'll explain the basics here. If you want more detailed information, you might check out the following article on the www.15seconds.com site. In fact, if you can follow that article, you can expose .NET visual components to PowerBuilder without having to use the Interop Forms Toolkit. The Interop Forms Toolkit is just helpful in automatically creating most of the code for you.

The particular .NET visual component that I'm going to use for this sample is the Office 2007 style ribbon toolbar from Pure Components. I didn't do a whole lot of comparison of the different options out there; I just like the price of this one. When implemented in a PowerBuilder window, it looks like Figure 1.

As mentioned earlier, the first thing you need to do is define an interface that contains the events we want to react to from PowerBuilder:

Imports System.Runtime.InteropServices

<Guid("A66356CF-7408-4bf5-B02E-17158FE30DA3"), _
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _
Public Interface IInteropUserControlEvents
<DispId(1)> _
Sub PasteClicked()
<DispId(2)> _
Sub CutClicked()
End Interface

The GUID is any GUID you want to assign. If you don't want to use the GuidGen utility from Microsoft to generate one, there are several online tools you can use to do it such as guidgen.com. PowerBuilder also has a GenerateGUID PowerScript function if you want to write a utility to expose it and create your own GuidGen utility. You can name the interface any name you want as well. Beyond that, the only thing you need to do is create the event prototypes and provide them with a unique DispID.

In the interop user control that the Interop Forms Toolkit generated, we need to make the following three changes.

1. Indicate that the event interface is implemented by the control. We do that by prefixing the class declaration for the control with a ComSourceInterfaces attribute that references the event interface we just created:

<ComSourceInterfaces(GetType(IInteropUserControlEvents))> _
Public Class InteropUserControl

2. Declare the events within the class. We don't have to add any code for them, but there at least has to be a declaration for them since we're implementing that interface:

Public Event PasteClicked()
Public Event CutClicked()

3. Wire the control events to the interop user control events. In order for the events to work, we need to relay the events from the .NET component to the events we've just declared on the wrapper. To do that, we simply perform a RaiseEvent within the event handler for the .NET events:

Private Sub PasteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteButton.Click
RaiseEvent PasteClicked()
End Sub

Private Sub CutButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutButton.Click
RaiseEvent CutClicked()
End Sub

When you add the interop user control to a PowerBuilder window, the new events will show up in the PowerBuilder script painter, and we can add our own code for them (see Figure 2).

That's it. Now we can not only call methods on the .NET component, but respond to its events as well. I'll upload the sample code to Sybase's CodeXchange site. So, at least until PowerBuilder 12 is available and supports this natively, we do have a method for using visual .NET components from PowerBuilder.

 

--This article was originally published on PBDJ.

Comments (0)
There are no comments posted here yet

Find Articles by Tag

SVN TFS Messagging API WinAPI PostgreSQL PBVM Database Object Database Painter ActiveX .NET Std Framework Service Validation Sort PBDOM Android PowerBuilder PowerServer Mobile Oracle Configuration Icon Syntax PowerBuilder (Appeon) UI Themes PowerServer Web Charts RibbonBar Builder Visual Studio TLS/SSL MessageBox Database Connection Transaction Database Data UI Modernization REST ODBC PostgreSQL ODBC driver License Application .NET DataStore External Functions Windows OS RichTextEdit Control IDE Event Debugger NativePDF Database Table Data Import JSON JSONGenerator Linux OS Encryption Migration PBNI Expression Deployment TreeView Repository DevOps RESTClient OAuth 2.0 SQL Server Authentication Variable Event Handling Web Service Proxy 64-bit Text RibbonBar Mobile Open Source UI Authorization SQL Design DLL PDF SOAP File Bug Graph Resize Platform Excel Source Control InfoMaker 32-bit DataType Debug Jenkins .NET Assembly SqlModelMapper Performance TortoiseGit SDK Source Code Database Profile HTTPClient SqlExecutor Error PFC DragDrop Model SnapDevelop C# OAuth JSONParser COM Import Filter Git Database Table Encoding Elevate Conference iOS Array Azure Window GhostScript DataWindow JSON Interface Stored Procedure DataWindow Database Table Schema Outlook Web API Branch & Merge CoderObject Windows 10 Icons Event Handler Script PowerBuilder Compiler XML SnapObjects Automated Testing Trial Class PowerScript (PS) OrcaScript CI/CD CrypterObject Testing Export JSON Export Debugging BLOB OLE Installation JSON Menu PDFlib WebBrowser