Skip to main content Skip to footer

Using Licensed WinForms controls in WPF

Did you know that you can use licensed WinForms controls in WPF applications? It's true. ComponentOne provides many controls in WinForms that aren't available in WPF. For instance, C1PdfDocument and C1XLBook are two components used to import and/or export PDF and Excel documents. These are non-visual components, meaning they are only accessible through code. Since they provide no UI they can easily be integrated into a WPF application without having to use some control host. You can readily create and call upon these components from your WPF code-behind. For example, in this code we declare a C1XLBook and allow the user to import an Excel file at run-time into our WPF application (C1XLBook is part of ComponentOne Excel for .NET).

using C1.C1Excel;

C1XLBook _xlBook = new C1XLBook(); // show dialog to open data file var dlg = new OpenFileDialog(); dlg.DefaultExt = ".xlsx"; dlg.Filter = "Excel Files (.xlsx;.xls)|.xlsx;.xls"; dlg.InitialDirectory = System.IO.Path.GetDirectoryName(Application.ResourceAssembly.Location); if (dlg.ShowDialog() == true) { // load data file try { _xlBook.Load(dlg.FileName); } catch (Exception x) { MessageBox.Show( "Error opening file: " x.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; }

// go do something with the excel book we just loaded

}

From this point we can use the C1XLBook API to work with our Excel file directly through our code. For instance, we can grab values from any cell or change any text and resave it back.

How to configure licensing

WPF supports the use of WinForms without too much trouble. But when it comes to using licensed controls, whether they are from ComponentOne or another vendor, you may run into licensing problems with WPF. This is because the licensing mechanism used by the control is specific to WinForms only. The trick is to add a Windows Form somewhere in your WPF project, and drop an instance of the WinForms control onto it. This way, Visual Studio will pick up on the licensing code and properly generate the licenses.licx file. This is necessary so you get no evaluation messages in your published application. The extra Windows Form you create can sit completely out of view to end-users. For example, in your WPF application select Project - Add New Item. Select Windows Form from the list. Then you will see all WinForms controls in your toolbox, including C1XLBook (if you've installed Studio for WinForms). Drop an instance of the control on your Windows Form. This adds the required licensing so you can use other instances of the controls throughout the WPF portions of your project.

ComponentOne Product Manager Greg Lutz

Greg Lutz

comments powered by Disqus