1

I would like to use a WPF RichTextBox in a WinForms project written with VB
I have created the WinForms project with one form and a button
I then added a new project WPF User Control Library placed a WPF RichTextBox on the WPF form
I added ElementHost interoperability to the WinForm with these Imports

Imports System
Imports System.Windows.Forms
Imports System.Windows.Forms.Integration

From here I am lost some of the SO question are 10 to 7 years old the MS tutorial is not much help
Code from WPF Form

Public Class UserControl1
  ReadOnly rtbWPF As New UserControl
  ElementHost
  wpfwindow.Show
End Class

I did not post the XAML code NOT sure how to do that

So the question what to do next to link the WPF form with the RTB to the WinForms form?
I would like to load data from a SQLite DB into the WPF RichTextBox and save the text entered in the RTB into the DB

Vector
  • 3,066
  • 5
  • 27
  • 54
  • OK, I'll bite. Why can't you just use a WinForms `RichTextBox` control? – jmcilhinney Aug 20 '20 at 02:28
  • @jmcilhinney NO Spelling Control I have written a spelling control in a JavaFX project and it works but the dictionary text files are very lacking I thought about writing the project as a WPF it seems good but the learning curve kind of steep – Vector Aug 20 '20 at 02:33

2 Answers2

1

To host a WPF control in Winforms, you can refer to the following two ways.

First, both of them need to add a ElementHost control into form.

Solution A:

Directly declare the wpf controls (using Windows.Controls)

Dim rtb As Windows.Controls.RichTextBox = New Windows.Controls.RichTextBox()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    rtb.SpellCheck.IsEnabled = True
    ElementHost1.Child = rtb
End Sub

Solution B:

Create a new User Control(WPF) and edit the content in "UserControl1.xaml" as follows.

<Grid>
    <RichTextBox x:Name="richtextbox" Foreground="Black" FontSize="24" Margin="0"></RichTextBox>
    <RichTextBox SpellCheck.IsEnabled="True" />
</Grid>

Then modify the code in 'form1.vb'

Private uc As UserControl1 = New UserControl1()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ElementHost1.Child = uc
End Sub
大陸北方網友
  • 3,696
  • 3
  • 12
  • 37
  • First thanks Where did you find the concept in Solution A? I only search for a few hours as opposed to my usual days Will test code later – Vector Aug 20 '20 at 02:40
  • According to the official document, the value of [ElementHost.Child is of type UIElement](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.integration.elementhost.child?view=netcore-3.1). And [RichTextBox inherits from UIElement](https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.richtextbox?view=netcore-3.1). So we can set `RichTextBox` as `Child` directly. – 大陸北方網友 Aug 20 '20 at 02:54
  • How do I get rid of that set of controls in the title bar HotReload it works great also wondering how to style it I guess I need to use the properties of ElementHost – Vector Aug 20 '20 at 03:55
1

This answer is meant to expand on @KyleWang wonderful answer
One BIG issue with Vectors choice of the WPF RichTextBox is There is no Text property in the WPF RichTextBox control. Here is one way to get all of the text out. That said I would IMHO would suggest using the WPF Plain TextBox control
Vector also commented on how to hide the HotReload in the title bar Tools > Options > Debugging > General > un-check Enable UI Debugging Tools for XAML
OK Code Below hope this is helpful if you decide to use a WPF control in WinForms for spell checking

Public Class frmStart

Dim rtb As Windows.Controls.RichTextBox = New Windows.Controls.RichTextBox()
Dim tb As Windows.Controls.TextBox = New Windows.Controls.TextBox()
Dim str As String = " "

Private Sub frmStart_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ElementHost1.Child = rtb
    rtb.SpellCheck.IsEnabled = True

    ElementHost2.Child = tb
    tb.SpellCheck.IsEnabled = True

    If str.Length < 100 Then
        rtb.VerticalScrollBarVisibility = Windows.Controls.ScrollBarVisibility.Visible
    End If
End Sub

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    str = "Plain WPF TxtBox"
    tb.Text = str
    rtb.AppendText("Heree is som mispelled txt se if the dictioary wrks more nonsense to see the scroll bar's will this word wrapp or is that rapp")
End Sub

Private Sub btnGet_Click(sender As Object, e As EventArgs) Handles btnGet.Click
    Dim elementHost = ElementHost1
    Dim wpfRichText = CType(elementHost.Child, Windows.Controls.RichTextBox)
    Dim range As Windows.Documents.TextRange = New Windows.Documents.TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd)
    Dim allText As String = range.Text
    tbMsg.Text = allText.ToString
End Sub

Private Sub btnGTB_Click(sender As Object, e As EventArgs) Handles btnGTB.Click
    Dim elementHost = ElementHost2
    Dim text = tb.Text
    tbMsg.Text = text.ToString
End Sub
James_Duh
  • 1,321
  • 11
  • 31