0

I've been working in WPF and trying to start a Winform from that area. The only solution is to open it as a ShowDialog(). Is this a bug or can we expect any problems in the future? my other program is located in the same solution, but not the same namespace.

WindowsFormsApplication1.Form1 program2 = new WindowsFormsApplication1.Form1();
program2.ShowDialog();

2 Answers2

1

I've just tested this, and it works for me:

public partial class MainWindow : Window
{
    private Form winForm;
    public MainWindow()
    {
        InitializeComponent();
    }

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        winForm = new WinForm1();
        winForm.Show();
    }
}

enter image description here

I believe your problem is most likely due to your program2 going out of scope immediately after you try to call Show on it, which is closing it faster than you can see it. The reason ShowDialog works, is because it is a blocking call, keeps the window in scope and open until after it is closed.

Try declaring program2 as a field within the WPF Window class, instead of as a local variable. That will keep it in scope.

Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
  • i should have mention it but my other program is located in the same solution, but not the same namespace. – Peter Gruppelaar Nov 10 '17 at 15:11
  • That shouldn't affect anything. In my test, both windows were also part of the same project. This should work with them in separate projects, or even when the WinForms window is coming from a referenced assembly. – Bradley Uffner Nov 10 '17 at 15:18
  • It does here, when i use Show() the new program stalls/crashes, there is nothing in this program. working in 64bit... both of them. – Peter Gruppelaar Nov 10 '17 at 15:24
  • The problem you are having must be somewhere else, and not just due to calling `Show` on a WinForms window from WPF. I recreated that in its simplest form, and it works perfectly. I would be happy to take a look at your project for you, if you could give me access somehow. – Bradley Uffner Nov 10 '17 at 15:29
  • hmmm strange. are u sure it is not the WinformHost? i am using Visual basic 2015 .Net 4.6.1. – Peter Gruppelaar Nov 10 '17 at 15:37
  • You never mentioned a `WinFormHost` previously. It is possible that the problem is coming from there, but I would need more details on exactly how it is being used to know for sure. – Bradley Uffner Nov 10 '17 at 15:41
  • It is not comming from there, this is my first application in WPF and you are right something else is causing it, i just build a little program the same way just the button. – Peter Gruppelaar Nov 10 '17 at 15:51
  • My offer to help find the problem if you can send me the code still stands. You can upload the project to github, BitBucket, or something similar, or even zip the project up and share it from google drive or some other service. – Bradley Uffner Nov 10 '17 at 20:18
  • Its CefSharp/Javascript Hybrid. i will try and tacle it my self first, it might be caused by Cefsharp, i will modify the topic and post the solution when i have found the problem, letting others do the work for you never helps in the long run, i have not met any one who did not run in to problems developing applications. – Peter Gruppelaar Nov 10 '17 at 20:42
  • I found the problem, it was Cefsharp related Browser was not ready to launch. – Peter Gruppelaar Nov 12 '17 at 01:14
0

I found the problem... the reason program2.Show() was not working was becouse of Cefsharp during the launch of the second program via javascript the loading progress bar(Javascript) was not done loading. you can identify these problems with Cefsharp by tagging them in to

 if (browser.CanExecuteJavascriptInMainFrame)
                    {
                        WindowsFormsApplication1.Form1 program2 = new WindowsFormsApplication1.Form1();
                        program2.Show();
                    }