Welcome!

Microsoft Cloud Authors: Janakiram MSV, Yeshim Deniz, David H Deans, Andreas Grabner, Stackify Blog

Related Topics: Microsoft Cloud, PowerBuilder

Microsoft Cloud: Article

Writing COM Controls for PowerBuilder in C#

Property Pages, Persistence, and Registration Free Deployment

While there is literature describing how to use .NET controls in PB (see for example, past issues of this magazine), all are silent on how to set control properties at runtime and how to persist them. This article will address both issues.

Like regular OLE controls, you can allow your .NET control properties to be set at design-time by means of property pages (see Figure 1).

Sample Code in PB12 Resource Page
The ideas in this article have been implemented in source code available at the PB12 Resource (http://www.sybase.com/powerbuilder12). Look for "Writing A COM Control for PB using C# Part 6." This article will reference it.

Required COM Interfaces
The COM interfaces required for OLE Control property pages are the same as those required by .NET controls. The only difference being that you can implement them in C# (or VB.NET).

The interfaces required are ISpecifyPropertyPages and IPropertyPage (see Figure 2)

The ISpecifyPropertyPages interface must be implemented by the control itself. By implementing the ISpecifyPropertyPages interface, a control indicates to the hosting IDE that it supports property pages. The Control can specify any number of property pages by providing their GUIDs in the implementation of the GetPages method of ISpecifyPropertyPages. Each "page" is a tab on the property tab dialog. In Figure 3, you would get two tabs on the dialog.

For each tab on the property page dialog, you need a COM object that implements the IPropertyPage interface. In our case this would be a C# (or VB.NET) object implementing the  IPropertyPage interface (see Figures 4 and 5). This object does not have to be a control. It just needs to be a COM creatable object with GUIDs that match those specified in the implementation of ISpecifyPropertyPages (the GetPages method).

The object implementing the IPropertyPage interface creates a Control to be parented to a hosting PB IDE dialog. This control is what appears in the tabs of the property page dialog. This is done through the Activate method of the IPropertyPage interface. The title of the hosting tab page can be specified in the GetPageInfo method.  In Figure 6, a PropertyGrid control is created and made a child window of the window passed in from the Activate method. In Figure 7, the hosting tab page has its title set.

The property page will need a reference to the control being designed (whose properties are being set through the property page). This reference is needed so that when you click the "OK" button on the property page, your code can synchronize the properties of the control to the values set through the property pages (see Figure 8). This is provided for by the SetObjects method of the IPropertyPage interface.

The synchronization of the values in the property page and the property values of the control when you click the "OK" or "Apply" button is done in the Apply method of the IPropertyPage interface.

The .NET PropertyGrid Control as a Property Page
One of the advantages of implementing property pages in C# is the greater ease with which the UI can be implemented. The .NET PropertyGrid control provides (out of the box) a GUI for editing properties of .NET objects (see Figure 9). It can be customized declaratively per property using attributes on properties. It can also launch customized dialogs for properties with the System.Drawing.Design.UITypeEditor attribute. The SelectedObject property of the PropertyGrid contains the object whose properties are being edited. The property values of the object are set immediately when set in the PropertyGrid. But we want the properties to be set only when we click "OK", and not immediately, and not at all when we click "Cancel." The solution is to avoid setting the SelectedObject property to the Control. Define another object to hold the same visible properties as the control and use that object as SelectedObject instead. Synchronization between the object and the control is done during the launching of the property page dialog and when "OK" is clicked.

Alternatives to the .NET PropertyGrid
While the PropertyGrid control is the most convenient way to provide a UI to edit .NET properties, you are not limited to it. You can use Window Forms UserControls or WPF UserControls hosted in an ElementHost in a Windows Forms UserControl. The code in Figures 10 and 11 has examples of both.

Persistence
Control properties set at design-time need to be saved, so that they can be reloaded at the next design session and also at runtime. There is good news here! Control properties that are public, "scalar" (strings, numbers or enumerations) and not named "Text" (!) are automatically persisted without you having to do anything. It is also possible to suppress the persistence of selected properties. To suppress persistence for a given property, use the System.ComponentModel.DesignerSerializationVisibility attribute on the property.

Persistence of "Non-scalar" Type Properties
This is another area where the comprehensiveness of the .NET Framework makes it easy to implement COM. The framework provides several ways to turn an object into a stream of bytes and back. The most convenient form of streams of bytes for our purpose is the string. A way of doing this is with the TypeConverter: turn your properties into strings properties using System.ComponentModel.TypeConverter. See Figure 12 for an example of this.

Another approach is to use XAML as the persistence format (see Figure 13).

Conclusion
Also in the sample code are examples on how to make Control backgrounds transparent at design-time. With this article, you have everything you need to create COM controls written in C#, lacking neither runtime nor design-time features.

For those who don't like the use of "regasm" for deployment, the registration-free option is also available. See http://msdn.microsoft.com/en-us/library/ms973915.aspx for pointers on how to write the required manifests and http://blogs.msdn.com/b/astebner/archive/2006/02/28/541036.aspx on how to embed manifests as resources.

More Stories By Frederick Koh

Frederick Koh is a Staff Engineer at Sybase, Inc., and has been a member of the PowerBuilder and DataWindow.NET development team since 2005. Frederick has over 14 years of IT industry experience.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


@ThingsExpo Stories
While some developers care passionately about how data centers and clouds are architected, for most, it is only the end result that matters. To the majority of companies, technology exists to solve a business problem, and only delivers value when it is solving that problem. 2017 brings the mainstream adoption of containers for production workloads. In his session at 21st Cloud Expo, Ben McCormack, VP of Operations at Evernote, will discuss how data centers of the future will be managed, how th...
SYS-CON Events announced today that Massive Networks, that helps your business operate seamlessly with fast, reliable, and secure internet and network solutions, has been named "Exhibitor" of SYS-CON's 21st International Cloud Expo ®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. As a premier telecommunications provider, Massive Networks is headquartered out of Louisville, Colorado. With years of experience under their belt, their team of...
SYS-CON Events announced today that Mobile Create USA will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Mobile Create USA Inc. is an MVNO-based business model that uses portable communication devices and cellular-based infrastructure in the development, sales, operation and mobile communications systems incorporating GPS capabi...
SYS-CON Events announced today that MIRAI Inc. will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. MIRAI Inc. are IT consultants from the public sector whose mission is to solve social issues by technology and innovation and to create a meaningful future for people.
SYS-CON Events announced today that Keisoku Research Consultant Co. will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Keisoku Research Consultant, Co. offers research and consulting in a wide range of civil engineering-related fields from information construction to preservation of cultural properties. For more information, vi...
SYS-CON Events announced today that Interface Corporation will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Interface Corporation is a company developing, manufacturing and marketing high quality and wide variety of industrial computers and interface modules such as PCIs and PCI express. For more information, visit http://www.i...
SYS-CON Events announced today that Fusic will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Fusic Co. provides mocks as virtual IoT devices. You can customize mocks, and get any amount of data at any time in your test. For more information, visit https://fusic.co.jp/english/.
SYS-CON Events announced today that TMC has been named “Media Sponsor” of SYS-CON's 21st International Cloud Expo and Big Data at Cloud Expo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Global buyers rely on TMC’s content-driven marketplaces to make purchase decisions and navigate markets. Learn how we can help you reach your marketing goals.
SYS-CON Events announced today that Enroute Lab will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Enroute Lab is an industrial design, research and development company of unmanned robotic vehicle system. For more information, please visit http://elab.co.jp/.
There is huge complexity in implementing a successful digital business that requires efficient on-premise and cloud back-end infrastructure, IT and Internet of Things (IoT) data, analytics, Machine Learning, Artificial Intelligence (AI) and Digital Applications. In the data center alone, there are physical and virtual infrastructures, multiple operating systems, multiple applications and new and emerging business and technological paradigms such as cloud computing and XaaS. And then there are pe...
SYS-CON Events announced today that Daiya Industry will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Daiya Industry specializes in orthotic support systems and assistive devices with pneumatic artificial muscles in order to contribute to an extended healthy life expectancy. For more information, please visit https://www.daiyak...
Real IoT production deployments running at scale are collecting sensor data from hundreds / thousands / millions of devices. The goal is to take business-critical actions on the real-time data and find insights from stored datasets. In his session at @ThingsExpo, John Walicki, Watson IoT Developer Advocate at IBM Cloud, will provide a fast-paced developer journey that follows the IoT sensor data from generation, to edge gateway, to edge analytics, to encryption, to the IBM Bluemix cloud, to Wa...
In his session at @ThingsExpo, Greg Gorman is the Director, IoT Developer Ecosystem, Watson IoT, will provide a short tutorial on Node-RED, a Node.js-based programming tool for wiring together hardware devices, APIs and online services in new and interesting ways. It provides a browser-based editor that makes it easy to wire together flows using a wide range of nodes in the palette that can be deployed to its runtime in a single-click. There is a large library of contributed nodes that help so...
With major technology companies and startups seriously embracing Cloud strategies, now is the perfect time to attend 21st Cloud Expo October 31 - November 2, 2017, at the Santa Clara Convention Center, CA, and June 12-14, 2018, at the Javits Center in New York City, NY, and learn what is going on, contribute to the discussions, and ensure that your enterprise is on the right path to Digital Transformation.
SYS-CON Events announced today that App2Cloud will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct. 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. App2Cloud is an online Platform, specializing in migrating legacy applications to any Cloud Providers (AWS, Azure, Google Cloud).
Agile has finally jumped the technology shark, expanding outside the software world. Enterprises are now increasingly adopting Agile practices across their organizations in order to successfully navigate the disruptive waters that threaten to drown them. In our quest for establishing change as a core competency in our organizations, this business-centric notion of Agile is an essential component of Agile Digital Transformation. In the years since the publication of the Agile Manifesto, the conn...
WebRTC is great technology to build your own communication tools. It will be even more exciting experience it with advanced devices, such as a 360 Camera, 360 microphone, and a depth sensor camera. In his session at @ThingsExpo, Masashi Ganeko, a manager at INFOCOM Corporation, will introduce two experimental projects from his team and what they learned from them. "Shotoku Tamago" uses the robot audition software HARK to track speakers in 360 video of a remote party. "Virtual Teleport" uses a mu...
Internet of @ThingsExpo, taking place October 31 - November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA, is co-located with 21st Cloud Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The Internet of Things (IoT) is the most profound change in personal and enterprise IT since the creation of the Worldwide Web more than 20 years ago. All major researchers estimate there will be tens of billions devic...
Mobile device usage has increased exponentially during the past several years, as consumers rely on handhelds for everything from news and weather to banking and purchases. What can we expect in the next few years? The way in which we interact with our devices will fundamentally change, as businesses leverage Artificial Intelligence. We already see this taking shape as businesses leverage AI for cost savings and customer responsiveness. This trend will continue, as AI is used for more sophistica...
SYS-CON Events announced today that SourceForge has been named “Media Sponsor” of SYS-CON's 21st International Cloud Expo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. SourceForge is the largest, most trusted destination for Open Source Software development, collaboration, discovery and download on the web serving over 32 million viewers, 150 million downloads and over 460,000 active development projects each and every month.