Jasper
Jasper

Reputation: 311

Awesomium WebControl trouble with Enable Control in Winform c#

I would use this Browser Framework in my Winform Application c#.

I just Saw the Documentation HERE

So i would use this Method

I just create a new Class and a new Awesomium.Windows.Forms.WebControl Object.

Now if i use it without any particulary method (just the ones to create object and Load the Url Source it works. But when i want use This method :

browser.SetHeaderDefinition("MyHeader", myCol); //myCol is a NameValueCollection 

i recive this error The control is disabled either manually or it has been destroyed.

On the first page that i linked there is wrote :


In addition to its regular meaning, the Enabled property has a special meaning in WebControl: it also indicates if the underlying view is valid and enabled.

A WebControl is considered invalid when it has been destroyed (by either calling Close() or Shutdown()) or was never properly instantiated.

Manually setting the Enabled property to true, will temporarily render the control disabled. .... ....

While disabled (either because the view is destroyed or because you manually set this property) attempting to access members of this control, may cause a InvalidOperationException (see the documentation of each member).

Now i tried to play with the ENABLED property but i still get this error. What i have to do to resolve this problem? I really didn't understand.

   Awesomium.Windows.Forms.WebControl browser = 
                                        new Awesomium.Windows.Forms.WebControl();
                    this.SuspendLayout();
        browser.Location = new System.Drawing.Point(1, 12);
       browser.Name = "webControl1";
       browser.Size = new System.Drawing.Size(624, 442);
     browser.Source = new System.Uri("http://www.google.it", System.UriKind.Absolute);
                    browser.TabIndex = 0;
**** This below is the code that i cant use cause i get the error control
// System.Collections.Specialized.NameValueCollection myCol = 
// new System.Collections.Specialized.NameValueCollection();
//            myCol.Add("Referer", "http://www.yahoo.com");
//            browser.SetHeaderDefinition("MyHeader", myCol);
//            browser.AddHeaderRewriteRule("http://*", "MyHeader"); 

Upvotes: 1

Views: 4473

Answers (1)

Mike Walton
Mike Walton

Reputation: 4386

The issue is that you cannot set the header definition until the control has finished being created. You just need to delay when you're setting the header definition until the control is ready. I'm not a Winforms expert, so there may be a better event to use to determine where a control is in its lifecycle, but here's a working modification of what you posted that just uses the control's Paint event to defer the problematic method calls:

public partial class Form1 : Form
{
    private Awesomium.Windows.Forms.WebControl browser;

    public Form1()
    {
        InitializeComponent();

        browser = new Awesomium.Windows.Forms.WebControl();

        //delay until control is ready
        browser.Paint += browser_Paint;

        Controls.Add(browser);

        browser.Location = new System.Drawing.Point(1, 12);
        browser.Name = "webControl1";
        browser.Size = new System.Drawing.Size(624, 442);
        browser.Source = new System.Uri("http://www.google.it", System.UriKind.Absolute);
        browser.TabIndex = 0;

    }

    void browser_Paint(object sender, PaintEventArgs e)
    {
        browser.Paint -= browser_Paint;

        System.Collections.Specialized.NameValueCollection myCol =
            new System.Collections.Specialized.NameValueCollection();
        myCol.Add("Referer", "http://www.yahoo.com");
        browser.SetHeaderDefinition("MyHeader", myCol);
        browser.AddHeaderRewriteRule("http://*", "MyHeader");
    }
}

Upvotes: 2

Related Questions