title: WebBrowser Control and GetElement by ID
body: <p>I am using Visual C# Winforms to control a WebBrowser object.</p>
  
  <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>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("&lt;html&gt;&lt;body&gt;&lt;div id='question'&gt;&lt;h1&gt;");
  webBrowser1.Document.Write(question);
  webBrowser1.Document.Write("&lt;/h1&gt;&lt;/div&gt;&lt;div id='answer'&gt;&lt;h2&gt;");
  webBrowser1.Document.Write(answer);
  webBrowser1.Document.Write("&lt;/h2&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;");
  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}
  </code></pre>
  
  <p>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?</p>
  
  <p>Update:<br />
  I found something that may be going in the right direction. If I call <code>webBrowser1.Update()</code> at any point after <code>webBrowser1.Navigate("about:blank")</code> 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?</p>