Skype:TextControlSupport
Orders:877-462-4772
TX Text Control - word processing components.
What is this?Syndicate this content

How to use TX Text Control .NET in a WPF application

This source code snippet requires TX Text Control .NET
Author:TX Text Control Support Department
Language:Visual C#
Version:1.0
Released:November 07, 2007
Last modified:January 11, 2008
Requirements:TX Text Control .NET with Visual Studio 2005
Download code:txDotnetInWpf.zip Download [15.95 KB, ZIP]

Microsoft recommends using .NET Windows Forms components in Windows Presentation Foundation (WPF) applications. The .NET Framework provides a special class that can be used to integrate Windows Forms components in WPF application effortlessly. This article provides a step-by-step guide to creating such an application.

To complete this walkthrough you will need the Development Tools for .NET Framework 3.0:

Installation Instructions for the Windows SDK

  1. In Visual Studio 2005, choose File, New Project... to create a new Windows Application (WPF). Choose a name and location for the application and confirm with OK.

  2. Click to enlarge
  3. In the Visual Studio Solution Explorer, add a reference to the WindowsFormsIntegration assembly (WindowsFormsIntegration.dll). The default location for this file is

    %programfiles%\Reference Assemblies\Microsoft\Framework\v3.0\WindowsFormsIntegration.dll.

  4. From the Project menu, choose Add User Control..., name it txUserControl.cs and confirm with Add.

  5. Click to enlarge
  6. Double-click the txUserControl.cs in the Solution Explorer to open the design view of the Windows Forms user control. Drag and drop a TextControl, ButtonBar, RulerBar and StatusBar to the control and connect these controls, as described in the TX Text Control documentation.

    The txUserControl.cs file should look this this:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Drawing;
    5. using System.Data;
    6. using System.Text;
    7. using System.Windows.Forms;
    8.  
    9. namespace txDotnetInWpf
    10. {
    11. public partial class txUserControl : UserControl
    12. {
    13. public TXTextControl.TextControl myTX;
    14.  
    15. public txUserControl()
    16. {
    17. InitializeComponent();
    18. textControl1.CreateControl();
    19. myTX = textControl1;
    20. }
    21.  
    22. private void txUserControl_Load(object sender, EventArgs e)
    23. {
    24. textControl1.ButtonBar = buttonBar1;
    25. textControl1.RulerBar = rulerBar1;
    26. textControl1.VerticalRulerBar = rulerBar2;
    27. textControl1.StatusBar = statusBar1;
    28. }
    29. }
    30. }
  7. Add the following functions to the Window1.xaml.cs file:

    1. private void WindowLoaded(object sender, RoutedEventArgs e)
    2. {
    3. System.Windows.Forms.Integration.WindowsFormsHost host =
    4. new System.Windows.Forms.Integration.WindowsFormsHost();
    5. myWFUserControl TX = new myWFUserControl();
    6. host.Child = TX;
    7. TX.myTX.Text = "Double-click TX Text Control to trigger an event!";
    8. TX.myTX.SelectAll();
    9. TX.myTX.Selection.Bold = true;
    10. TX.myTX.DoubleClick += new EventHandler(myTX_DoubleClick);
    11. this.grid1.Children.Add(host);
    12. }
    13.  
    14. void myTX_DoubleClick(object sender, EventArgs e)
    15. {
    16. System.Windows.MessageBox.Show("TX Text Control .NET in WPF");
    17. }
  8. Press F5 to compile and start the WPF application.

The minimum requirements for this sample application are TX Text Control .NET trial version and Visual Studio .NET 2005 with the WPF extensions.

top

Top 10 Bestselling Product Award 2007Top 25 Publisher Product Award 2007Top 10 Bestselling Product Award 2007 in JapanTop 25 Bestselling Product Award 2006Top 25 Bestselling Publisher Award 2006Reader's Choice Award, dot.net magazin, 3rd place