Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've created an WPF and WinForm Application, what i need to do is open the WinForm from the WPF application. Both are in the same solution but they're diferent projects.

I tried the following:

Dim newWinForm as New MainWindow
newWinForm.show()

I found a possible solution from here: Opening winform from wpf application programmatically

But i dont understand what exactly i have to do. I hope you could help me. Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
725 views
Welcome To Ask or Share your Answers For Others

1 Answer

Generally you need to host your form in a WindowInteropHelper, like following in the WPF window Button.Click event handler:

C#:

private void button1_Click(object sender, RoutedEventArgs e) {
  Form1 form = new Form1();
  WindowInteropHelper wih = new WindowInteropHelper(this);
  wih.Owner = form.Handle;
  form.ShowDialog();
}

VB:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
    Dim form As New Form1()
    Dim wih As New WindowInteropHelper(Me)
    wih.Owner = Form.Handle
    form.ShowDialog()
End Sub

And of course you need to add reference/import of your project and System.Windows.Forms.dll


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...