Archives

Archives / 2006 / November
  • How to Set the CSS Class on an HtmlElement Instance from C#

    I’m currently using the WebBrowser control in a WinForms application as a simple layout engine, as it’s much faster when rendering texts in front of a background image than WinForms (and I need it by the weekend, so I don’t want to take the risk of using it as a learning project for WPF). For my purposes I only need to manipulate some texts – and the CSS class of specific elements on the page.

    Now let’s assume you have an DIV tag in your HTML like this:

    <div class="someClass">Hello World</div>

    and you want to change the CSS class from your C# code. It’s pretty easy, you just have to get a HtmlElement instance representing the tag and use the SetAttribute method. What took me some time to figure out is the problem that if you call

    myHtmlElement.SetAttribute("class", "anotherClass");

    nothing will happen in the display of your page. The reason for this is that SetAttribute does not actually “set an attribute of a tag”, but a property (representing the attribute) on an object (representing the tag) in the Document Object Model (DOM) – and the property’s name is “className”, not “class”. So the correct code is

    myHtmlElement.SetAttribute("className", "anotherClass");

    (If you're wondering why SetAttribute didn't complain about using the name "class": the DOM was primarily designed to be used from Javascript, which is a language where any object can be extended by simple setting a property. Sounds a bit strange, but it’s also a feature that can be used for seriously cool stuff)

  • Firefox 2.0 : Going Back to 1.5 Style...

    Ok, I really tried, but after working with Firefox 2.0 for a few days I still don’t like the Close buttons on the tabs (I know IE7 has them, too, but that doesn’t make things more usable for me personally). Fortunately it’s simple to go back to 1.5 style:

    • Enter about:config in the address bar
    • Set browser.tabs.closeButton to 3
    • Restart Firefox

    And if you prefer the icons of Firefox 1.5, there’s a theme for you on Mozilla.org.

  • Article "Introduction to GhostDoc" up on DotNetSlackers

    Some time ago Sonu Kapoor, webmaster of dotnetslackers.com, asked me to write an article about my Visual Studio add-in GhostDoc. It took me some time to find an empty spot in my busy schedule, but the article is now finally online. It is an introduction to GhostDoc that may be interesting for those who haven’t tried GhostDoc yet and would like to read a bit more about the features before actually downloading and installing it.