简体   繁体   中英

How to create web browser control in a separate class?

I am trying to make a class that when I create an instance of it then it will have a web browser control in it that I can interact with. My code works when I create the browser control in the main form but when I try to abstract it to its own class I am not understanding something about how winforms works. I am guessing I need to inherit something or maybe initialize it but I am not sure.

Here is the class code:

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class MyBrowserFuncs
{
    public WebBrowser WB;
    public MyBrowserFuncs()
    {
        WB = new WebBrowser();

        WB.ObjectForScripting = this;

        WB.Navigate("mypage.html");
    }
    public string Test(string msg)
    {
        return WB.Document.InvokeScript
                            ("Test", new String[] { msg });
    }
}

Here is the relevant javascript on mypage.html

function Test(msg)
{
    alert(msg);

    return "success";
}

And here is how I would like to call it in C#

private void btnSomething_Click(object sender, EventArgs e)
{
    var web = new MyBrowserFuncs();

    var text = web.Test("this is a message");
}

I don't get any errors but it doesn't work. It works when using code in main form. I am guessing it might be something to do with InitializeComponent(); but I am not 100% sure how I should be creating and disposing of controls when I don't need them visible and want to abstract them to a class.

What am I doing wrong?

In the test method in the MyBrowserFuncs class, accept a WebBrowser instance as well as the string. This will give you access to it. For example:

public string Test(string msg, WebBrowser WB)
{
    return WB.Document.InvokeScript
                        ("Test", new String[] { msg });
}

Just wanted to edit and maybe explain a bit more. You are trying to make your WebBrowser object inside the class. I would make it inside the main Form1 class, but then if you pass it (the WebBrowser control) as an argument to your WebBrowserFuncs methods, those methods will be able to interact with Form1's controls without a problem.

It works if I add the control back to the calling form so I did this:

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class MyBrowserFuncs
{
    public WebBrowser WB;
    public MyBrowserFuncs(Form f)
    {
        WB = new WebBrowser();
        WB.Visible = false;
        f.Controls.Add(WB);
        WB.ObjectForScripting = this;

        WB.Navigate("mypage.html");
    }
    public string Test(string msg)
    {
        return WB.Document.InvokeScript
                            ("Test", new String[] { msg });
    }
}

Then I called it like this:

private async void btnSomething_Click(object sender, EventArgs e)
{
    var web = new MyBrowserFuncs(this);
    await Task.Delay(1000);
    var text = web.Test("this is a message");
}

I added the delay just to see if it works and it does. I'll refactor that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM