Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Tutorials » Using Windows Forms Controls in WPF

Using Windows Forms Controls in WPF

In this tutorial, you will learn how to use existing Windows Forms controls in a WPF application. I have used Visual Studio 2008 to create this tutorial.

Author Rank:
Technologies: .NET 1.0/1.1, Windows Forms, XAML,Visual C# .NET
Total downloads : 165
Total page views :  12136
Rating :
 4.67/5
This article has been rated :  3 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
WinFormInWPF.zip
 
Become a Sponsor


Related EbooksTop Videos

In this tutorial, you will learn how we can use existing Windows Forms controls in a WPF application. I have used Visual Studio 2008 to create this tutorial.

Note: If you do not have Visual Studio 2008, you may want to download Visual Studio 2008 Express versions here:  http://www.microsoft.com/express/default.aspx . All Express versions are free.    

Step 1: Create a WPF Application using Visual Studio 2008

First create a WPF application using Visual Studio 2008. Click on File >> New >> Project menu and select WPF Application from Templates as shown in Figure 1.

Figure 1. Create a WPF Application

Step 1: Add Reference to Windows FormsIntegration and Windows Forms namespaces

In Solution Explorer, right click on References node and select Add Reference menu item. On Browse tab, go to "C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0" folder and select WindowsFormsIntegration.dll file and click OK button. If you have .NET 3.5 installed, you should also see v3.5. You need to select the current version of DLL you are working with. See Figure 2.

Figure 2. Adding WindowsFormsIntegration reference

Select Add Reference again and this time on .NET tab, select System.Windows.Forms and click OK. See Figure 3.


Figure 3. Adding System.Windows.Forms reference

This action adds assemblies to your application (copies to the Bin folder) you need to work with Windows Forms controls in your WPF application.

Next, go to your XAML file and add the following namespace and assembly reference within the Windows tag.

xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

The final Windows tag looks like this:

<Window x:Class="WinFormInWPF.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

    Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">

    <Grid>

 

    </Grid>

</Window>

Step 2: Add Windows Forms control to XAML

There are two ways to create a Windows Forms control in XAML. Either we can create a Windows Forms control in our XAML code using XAML syntaxes or we can create at runtime by using Windows Forms classes.

First, let's see how we can create a Windows Forms control in XAML.

Say, you want to use Windows Forms' ListBox control in your WPF application, you simply have to add the below code inside your Grid tag in XAML. The WindowsFormsHost tag is used to host Windows Forms control and wf:ListBox tag represents the Windows Forms ListBox. Similarly, you can add any Windows Forms control to your WPF application.

    <Grid>

        <WindowsFormsHost>

            <wf:ListBox x:Name="lstBox"/>

        </WindowsFormsHost>

 

    </Grid>

 

Step 3: Add Windows Forms control support to code

After that you need to go Window1.xaml.cs file to import these namespaces so we can use Windows Forms and related classes.

using System.Windows.Forms;

using System.Windows.Forms.Integration;

 

Step 4: Using Windows Forms control

Now, we can use Windows Forms control in our .cs file as in previous Windows Forms applications. If you double click on the Window in your XAML designer, it will add Window_Loaded event handler to the .cs file.

In the following code, I access lstBox control on Windows_Loaded event handler and calls Items.Add method a few times to add items to the ListBox.

private void Window_Loaded(object sender, RoutedEventArgs e)

{

            lstBox.Items.Add("Mahesh Chand");

            lstBox.Items.Add("XAML Title");

            lstBox.Items.Add("JJ Kohls");
}

Step 5: Build and Run

Now build and run your application. The output is Figure 4.

Figure 4. Windows Forms' ListBox in a WPF Application

Tip 1: Using multiple Windows Forms Controls 

If you wish to use multiple Windows Forms controls in a WPF application, each control should be inside a WindowsFormsHost tag.

  <WindowsFormsHost>

            <wf:ListBox x:Name="lstBox"/>

  </WindowsFormsHost>

  <WindowsFormsHost>

            <wf:DataGridView x:Name="DbGrid" />

  </WindowsFormsHost>

Tip 2: Using Windows Forms Controls Dynamically

In previous steps, we created a ListBox control in XAML. You may also create the control dynamically using Windows Forms classes. The following code creates a WindowsFormsHost object, a ListBox control and adds the ListBox control to the host. In the end, the host s added to the XAML Grid.

private void Window_Loaded(object sender, RoutedEventArgs e)

{

            WindowsFormsHost host = new WindowsFormsHost();

            //Create a Windows Forms ListBox control

            System.Windows.Forms.ListBox lstBox = new System.Windows.Forms.ListBox();

            lstBox.Items.Add("Mahesh Chand");

            lstBox.Items.Add("XAML Title");

            lstBox.Items.Add("JJ Kohls");

 

            // Add the Windows Forms ListBox to the host control

            host.Child = lstBox;

            // Add the host control to the WPF element that you want to parent the control,

            // in this case it's a Grid

            this.Grid1.Children.Add(host);

 

   }

Summary

In this article, I discussed how we can use a Windows Forms' ListBox controls in WPF applications. You may use same approach to use any Windows Forms control in WPF such as DataGrid or DataGridView controls.  


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Mahesh Chand
Mahesh is a software developer with over 13 years of experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle. If you are looking for a Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
WinFormInWPF.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Dundas Dashboard
Become a Sponsor
 Comments
Thank you by jimmy On January 16, 2009
Hi Mahesh, This is a great article and saved my day as I am working on few controls that are winform controls. Jimmy
Reply | Email | Delete | Modify | 
Re: Thank you by Mahesh On January 16, 2009

I am glad Jimmy.

Don't forget to share your tips with us if you find something interesting :)

Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved