locked
Adding wpf form to VB windows application RRS feed

  • Question

  • So, I've created a vb app that has multiple forms, but now i want to add a wpf from to it. How can i do that?

    Friday, October 9, 2020 12:28 AM

All replies

  • ok.. i see i can create a user control that can have a wpf object in it.  So say i create a user control that is just a wpf with just a single richtextbox in it.  When i drop that control on my windows form, how can i resize the wpf richtextbox there.
    Friday, October 9, 2020 12:51 AM
  • You can add WPF controls directly to the Form

    A test with a RichTextBox (can also be done with XAML) =>

    Dim grid1 As System.Windows.Controls.Grid = New System.Windows.Controls.Grid()      
    
    Dim richtextBox1 As System.Windows.Controls.RichTextBox = New System.Windows.Controls.RichTextBox() With
    {
        .AcceptsReturn = True,
        .VerticalScrollBarVisibility = Windows.Controls.ScrollBarVisibility.Visible,
        .Width = 250,
        .Height = 150
    }
    grid1.Children.Add(richtextBox1)
    
    Dim myFlowDoc As New Windows.Documents.FlowDocument()
    
    Dim myRun As New Windows.Documents.Run("This is")
    myRun.Foreground = Windows.Media.Brushes.Blue
    Dim myBold As New Windows.Documents.Bold(New Windows.Documents.Run(" a test"))
    myBold.Foreground = Windows.Media.Brushes.Red
    
    Dim myParagraph As New Windows.Documents.Paragraph()
    myParagraph.FontSize = 32
    myParagraph.Inlines.Add(myRun)
    myParagraph.Inlines.Add(myBold)
    
    myFlowDoc.Blocks.Add(myParagraph)
    richtextBox1.Document = myFlowDoc
    
    Dim elementHost1 As System.Windows.Forms.Integration.ElementHost = New System.Windows.Forms.Integration.ElementHost() With
    {
        .Child = grid1,
        .Location = New System.Drawing.Point(10, 10),
        .Size = New System.Drawing.Size(300, 200),
        .BackColor = System.Drawing.Color.Yellow
    }
    Me.Controls.Add(elementHost1)

    Friday, October 9, 2020 5:51 AM
  • Hi LandLord324,

    Did you solve your problem? If your question has been answered then please click the "Mark as Answer" Link at the bottom of the correct post(s), so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 21, 2020 2:23 AM