wpfvb.netwinformselementhost

wpf richtextbox in winforms application


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


Solution

  • 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