Events
Mar 17, 11 PM - Mar 21, 11 PM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
WPF provides many controls with a rich feature set. However, you may sometimes want to use Windows Forms controls on your WPF pages. For example, you may have a substantial investment in existing Windows Forms controls, or you may have a Windows Forms control that provides unique functionality.
This walkthrough shows you how to host a Windows Forms System.Windows.Forms.MaskedTextBox control on a WPF page by using code.
For a complete code listing of the tasks shown in this walkthrough, see Hosting a Windows Forms Control in WPF Sample.
You need Visual Studio to complete this walkthrough.
Create a WPF Application project named HostingWfInWpf
.
Add references to the following assemblies.
WindowsFormsIntegration
System.Windows.Forms
Open MainWindow.xaml in the WPF Designer.
Name the Grid element grid1
.
<Grid Name="grid1">
</Grid>
In Design view or XAML view, select the Window element.
In the Properties window, click the Events tab.
Double-click the Loaded event.
Insert the following code to handle the Loaded event.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Create the interop host control.
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
// Create the MaskedTextBox control.
MaskedTextBox mtbDate = new MaskedTextBox("00/00/0000");
// Assign the MaskedTextBox control as the host control's child.
host.Child = mtbDate;
// Add the interop host control to the Grid
// control's collection of child controls.
this.grid1.Children.Add(host);
}
Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
' Create the interop host control.
Dim host As New System.Windows.Forms.Integration.WindowsFormsHost()
' Create the MaskedTextBox control.
Dim mtbDate As New MaskedTextBox("00/00/0000")
' Assign the MaskedTextBox control as the host control's child.
host.Child = mtbDate
' Add the interop host control to the Grid
' control's collection of child controls.
Me.grid1.Children.Add(host)
End Sub
At the top of the file, add the following Imports
or using
statement.
using System.Windows.Forms;
Imports System.Windows.Forms
Press F5 to build and run the application.
.NET Desktop feedback feedback
.NET Desktop feedback is an open source project. Select a link to provide feedback:
Events
Mar 17, 11 PM - Mar 21, 11 PM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Create a UI in a .NET MAUI app by using XAML - Training
Learn how to design a UI for a .NET MAUI app using XAML.