0

I tried to open one windows form with DotNetBrowser control with specific html contrnent as below code.

After button clicked on html , i need to to hide loaded form and then show the second windows form.

I used c# code as below:

public partial class Form1 : Form
{
    private Browser browser;
    public Form1()
    {
        InitializeComponent();
        browser = BrowserFactory.Create();
        browser.FinishLoadingFrameEvent += delegate(object sender, FinishLoadingEventArgs e)
        {
            if (e.IsMainFrame)
            {
                JSValue value = browser.ExecuteJavaScriptAndReturnValue("window");
                value.AsObject().SetProperty("Account", new Form1());
            }
        };

        browser.LoadHTML(@"<!DOCTYPE html>
                                <html>
                                <head>
                                    <script type='text/javascript'>
                                          function sendFormData() {
                                            var firstNameValue = myForm.elements['firstName'].value;
                                            var lastNameValue = myForm.elements['lastName'].value;
                                            // Invoke the 'save' method of the 'Account' Java object.
                                            Account.Save(firstNameValue, lastNameValue);
                                          }
                                        </script>
                                </head>
                                <body>
                                <form name='myForm'>
                                    First name: <input type='text' name='firstName'/>
                                    <br/>
                                    Last name: <input type='text' name='lastName'/>
                                    <br/>
                                    <input type='button' value='Save' onclick='sendFormData();'/>
                                </form>
                                </body>
                                </html>");


        BrowserView browserView = new WinFormsBrowserView(browser);
        this.Controls.Add((Control)browserView.GetComponent());
    }

    public void Save(String firstName, String lastName)
    {
        string _firstname = firstName;            

        this.Hide();
        SecondForm frm = new SecondForm(firstName);
        frm.ShowDialog();

    }

The problem is, the first form (Which hold the browser control) does not hide and still focus.

Any help will be appreciated.

1 Answers1

0

To perform any UI-related actions on DotNetBrowser events it is necessary to pass execution to the main UI thread. In other case, some methods will throw cross-thread operation exceptions or even fail without any messages.

To hide the form in the .NET callback from JavaScript you need to wrap the call to its Hide method into BeginInvoke(). The modified sample code will look as shown below:

public partial class Form1 : Form
{
    private Browser browser;
    public Form1()
    {
        InitializeComponent();
        browser = BrowserFactory.Create();
        browser.FinishLoadingFrameEvent += delegate (object sender, FinishLoadingEventArgs e)
        {
            if (e.IsMainFrame)
            {
                JSValue value = browser.ExecuteJavaScriptAndReturnValue("window");
                value.AsObject().SetProperty("Account", new Account(this));
            }
        };
        browser.LoadHTML(@"<!DOCTYPE html>
                            <html>
                            <head>
                                <script type='text/javascript'>
                                        function sendFormData() {
                                        var firstNameValue = myForm.elements['firstName'].value;
                                        var lastNameValue = myForm.elements['lastName'].value;
                                        // Invoke the 'save' method of the 'Account' Java object.
                                        Account.Save(firstNameValue, lastNameValue);
                                        }
                                    </script>
                            </head>
                            <body>
                            <form name='myForm'>
                                First name: <input type='text' name='firstName'/>
                                <br/>
                                Last name: <input type='text' name='lastName'/>
                                <br/>
                                <input type='button' value='Save' onclick='sendFormData();'/>
                            </form>
                            </body>
                            </html>");
        BrowserView browserView = new WinFormsBrowserView(browser);
        this.Controls.Add((Control)browserView.GetComponent());
    }
    public class Account
    {
        private Form Form;
        public Account(Form form)
        {
            this.Form = form;
        }
        public void Save(String firstName, String lastName)
        {
            string _firstname = firstName;
            Form.Invoke(new Action(() =>
            {
                Form.Hide();
            }));
            SecondForm frm = new SecondForm(firstName);
            frm.ShowDialog(); 
        }
    }
}
Vladyslav Tsvek
  • 244
  • 1
  • 10