I followed the link you sent and used the steps and code below to run the program successfully.
- Create a new Windows Forms project.
- Add the WPF User Control Library project to the solution.
- Create a new WPF user control to the WPF User Control Library project.
- Add a reference to the WPF User Control Library from the Windows Forms project.
- Also add a reference to the WindowsFormsIntegration.dll and PresentationCore.dll assemblies from the Windows Forms project.
- Create an ElementHost object and an instance of the UserControl in the Windows Forms application and set the Child property of the ElementHost control to the UserControl instance. Finally, add the ElementHost object to the Controls collection of the form.
The code of UserControl.xaml (WPF User Control Library project):
<UserControl x:Class="UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfControlLibrary1"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="450">
<Viewbox>
<Grid>
<Ellipse Name="gauge" HorizontalAlignment="Left" Height="400" StrokeThickness="10" Margin="23,25,0,0" Stroke="Black" VerticalAlignment="Top" Width="400">
<Ellipse.Fill>
<RadialGradientBrush>
<GradientStop Color="Black" Offset="1"/>
<GradientStop Color="White"/>
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
<Ellipse HorizontalAlignment="Left" Height="49" StrokeThickness="0" Margin="205,205,0,0" Stroke="Black" VerticalAlignment="Top" Width="45" RenderTransformOrigin="0.274,0.252">
<Ellipse.Fill>
<RadialGradientBrush>
<GradientStop Color="Black" Offset="1"/>
<GradientStop Color="White"/>
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
<Rectangle Name="Needle" HorizontalAlignment="Left" Height="188" Margin="220,71,0,0" VerticalAlignment="Top" Width="13" >
<Rectangle.Fill>
<ImageBrush >
<ImageBrush.ImageSource>
<BitmapImage UriSource="rr.jpg"/>
</ImageBrush.ImageSource>
</ImageBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
</Viewbox>
</UserControl>
The code of UserControl.xaml.vb:
Imports System.Windows.Media
Partial Public Class UserControl1
Inherits System.Windows.Controls.UserControl
Public pointerValue As Double
Public Sub New()
InitializeComponent()
End Sub
Public Sub changevalue()
Dim rotate As RotateTransform = New RotateTransform(pointerValue)
Needle.RenderTransform = rotate
End Sub
End Class
Add a reference of WpfControlLibrary1 to WindowsApp1:
The designer of Form1.vb:
The code of Form1.vb:
Imports System.Windows.Forms.Integration
Imports WpfControlLibrary1
Public Class Form1
Private gauge As UserControl1
Private host As ElementHost
Public Sub New()
InitializeComponent()
gauge = New UserControl1()
host = New ElementHost()
host.Dock = DockStyle.Fill
host.Child = gauge
Me.Controls.Add(host)
End Sub
Private value As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
gauge.changevalue()
Integer.TryParse(valuepointer.Text, value)
gauge.pointerValue = value
End Sub
End Class
The picture of result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread.
[5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html