Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7638439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:10:40+00:00 2026-05-31T08:10:40+00:00

I am using Visual C# Winforms to control a WebBrowser object. Specifically I want

  • 0

I am using Visual C# Winforms to control a WebBrowser object.

Specifically I want to use WebBrowser.Document.GetObjectByID(“myid”).Style to set the style of an object that is part of the document loaded into the WebBrowser object.

I want to have button on the WinForm toggle the Style of a heading from “display:none” to “display:block;text-align:middle”.
This is what I am doing:

private void frmView_Load(object sender, EventArgs e)
{
string question = "How many cows?";
string answer = "5 cows";
webBrowser1.Navigate("about:blank");
webBrowser1.Document.OpenNew(false);
webBrowser1.Document.Write("<html><body><div id='question'><h1>");
webBrowser1.Document.Write(question);
webBrowser1.Document.Write("</h1></div><div id='answer'><h2>");
webBrowser1.Document.Write(answer);
webBrowser1.Document.Write("</h2></div></body></html>");
webBrowser1.Refresh();
webBrowser1.Document.GetElementById("answer").Style = "display:none;";
//if I do a breakpoint here, I get the Style = "DISPLAY:NONE"
btnAnswer.Visible = true;
btnNext.Visible = true;
}

private void btnAnswer_Click(object sender, EventArgs e)
{
//if this is the first time ran, doing a breakpoint here will
//show Style to be NULL
webBrowser1.Document.GetElementById("answer").Style = "display:block;text-align:center";
//now the Style is properly set, and will remain that way, even after this function returns}

Note that I am able to control the Style property with the btnAnswer_Click method, but not with the frmView_Load method. I know I can just put the style info into div tag when I create it (this does work), but why doesn’t the code above work?

Update:
I found something that may be going in the right direction. If I call webBrowser1.Update() at any point after webBrowser1.Navigate("about:blank") everything in frmView_Load works. Unfortunately, any attempts to change the document after frmView_Load exits will fail. Not only will they fail, but after frmView_Load returns webBrowser1.Document will be set to NULL somehow.
Can anyone shed some light on how this control is supposed to work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:10:41+00:00Added an answer on May 31, 2026 at 8:10 am

    After reviewing the MS Documentation on the WebBrowser control, I realize that everything the browser does is asynchronous. Having said that, it means that I have to block my control threads after setting properties in order to ensure those properties have been set.

    Basically, my “write” to the document isn’t done just because the “write” function returns. I have to block the control thread till the document reflects those changes if I want to reference the contents of that write. MS recommends a sleep loop. Here’s what my write & block function looks like:

    private void ChangeDocument(string documentText, double timeout)
    {
        DateTime startTime = DateTime.Now;
        double elapsed = 0;
    
        if (webBrowser1.Document == null)
        {
            webBrowser1.Navigate("about:blank");
        }
    
        webBrowser1.Document.OpenNew(false);
    
        while ((webBrowser1.DocumentText != "") && (elapsed < timeout))
        {
            Thread.Sleep(50);
            elapsed = DateTime.Now.Subtract(startTime).TotalMilliseconds;
        }
    
        webBrowser1.Document.Write(documentText);
    
        startTime = DateTime.Now;
        elapsed = 0;
    
        while ((webBrowser1.DocumentText != documentText) && (elapsed < timeout))
        {
            System.Threading.Thread.Sleep(50);
            elapsed = DateTime.Now.Subtract(startTime).TotalMilliseconds;
        }
    }
    

    Thanks for your responses everyone.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Visual Studio 2010 and WPF. I'm creating a new Control that inherits
I'm creating a WinForms user control using Visual C# 2008 Express Edition. Everything was
I have an WinForms application that is deployed using Visual Studio 2008's publish (ClickOnce)
I have a winforms applications that does some reporting using Crystal Reports for Visual
I have a VERY simple C# Winforms project that I made using Visual Studio
We are developing a multilingual Winforms application using visual studio 2008. I am trying
I'm writing a GUI in C#, Visual Studio 2008, using the Designer and WinForms.
Using Visual Studio 2005, I wrote a simple DLL in C that uses the
So I made a Winforms GUI in Visual Studio using C#, but for the
I am using Visual Studio 2008 and all the components that come with it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.