none
Using external CSS file for a page in WebBrowser RRS feed

  • Question

  • I'm a beginner and after googling for 4 hours I have no clue how to accomplish this. I have a CSS file that applies to Wikipedia pages via Stylish extension on my browser. I want the page on the WebBrowser control to use the same CSS file. Is this even possible?

    Thanks for help,

    Timo

    Thursday, June 28, 2018 8:33 AM

All replies

  • Technically speaking having an external CSS file you of course would include it in any web page but having it work without including it is not possible to work on any web page loaded into the web browser. Here is the deal, the WebBrowser control is nowhere near as robust as a normal browser which is why what you ask is not possible.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, June 28, 2018 10:28 AM
    Moderator
  • Could I modify the HTML of the page to include the CSS, and then load the modified HTML document in to the WebBrowser? And if so, how?
    Thursday, June 28, 2018 1:09 PM
  • Could I modify the HTML of the page to include the CSS, and then load the modified HTML document in to the WebBrowser? And if so, how?

    Nothing I know of. If this was not .NET bases but working directly in HTML/JavaScript you could do the following but not in VB.NET.

    $('head').append('<link rel="stylesheet" href="style1.css" type="text/css" />');
    For VB.NET in theory you would need to read the web page, parse for the head section, inject the CSS link, save it then load it.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, June 28, 2018 4:25 PM
    Moderator
  • Absolutely it is possible, unless I misunderstand what you are asking and I probably understand. The WebBrowser control is Internet Explorer; it is neither a stripped-down version nor an equivalent, it is IE. It just lacks the additional UI parts such as menus.

    I need more information about what you need to do. Why can't you just put the link tag in the HTML? I think there is more to the story than you are telling us.



    Sam Hobbs
    SimpleSamples.Info

    Saturday, June 30, 2018 11:52 PM
  • I was an editor for C# Corner for a while. To make my job easier I wrote my own custom editor, perhaps something similar to what you are doing. I took the HTML for an article and put it into a WebBrowser control for editing using my own toolbars and such. I injected a link tag, perhaps in the manner you want to. I want to be sure however that we choose the easiest solution for your requirements.



    Sam Hobbs
    SimpleSamples.Info

    Sunday, July 1, 2018 12:02 AM
  • Here is a little example I will do for you using the bootstrap CDN. Basically, I load a page that has bootstrap classes, but I don't initially include the CSS link. Then with a second button I show you how to inject the CSS stylesheet into the document. You can work with the dom in vb.net nearly the same ways you can with javascript(in vb.net syntax of course). Pretty strait forward. Enjoy.

    Before CSS Injection

    After CSS Injection:

    Code Example:

    Option Strict On
    Option Explicit On
    Option Infer Off
    Public Class Form1
        Private Sub btnLoadWebpage_Click(sender As Object, e As EventArgs) Handles btnLoadWebpage.Click
            Dim doc As String =
    "<!DOCTYPE html>
        <html lang = 'en'>
        <head>
            <title>Style Injection Example</title>
            <meta charset='utf-8'>
            <meta name='viewport' content='width=device-width, initial-scale=1'>    
            <script src='https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>
            <script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js'></script>
        </head>
        <body>
            <div Class='container'>
                <div Class='jumbotron'>
                    <h1>Hello World</h1>      
                    <p>Super CSS injection example</p>
                </div>
                <p>More Content!</p>      
                <p>Yay! So glad I added more content!</p>      
                <div class='p-3 mb-2 bg-primary text-white'>.bg-primary</div>
                <div class='p-3 mb-2 bg-secondary text-white'>.bg-secondary</div>
                <div class='p-3 mb-2 bg-success text-white'>.bg-success</div>
                <div class='p-3 mb-2 bg-danger text-white'>.bg-danger</div>
                <div class='p-3 mb-2 bg-warning text-dark'>.bg-warning</div>
                <div class='p-3 mb-2 bg-info text-white'>.bg-info</div>
                <div class='p-3 mb-2 bg-light text-dark'>.bg-light</div>
                <div class='p-3 mb-2 bg-dark text-white'>.bg-dark</div>
                <div class='p-3 mb-2 bg-white text-dark'>.bg-white</div>
            </div>
        </body>
    </html>"
            WebBrowser1.ScriptErrorsSuppressed = True 'I suppress for ease of example
            WebBrowser1.DocumentText = doc
        End Sub
        Private Sub btnInjectStyle_Click(sender As Object, e As EventArgs) Handles btnInjectStyle.Click
            Dim link As HtmlElement = WebBrowser1.Document.CreateElement("link")
            link.SetAttribute("rel", "stylesheet")
            link.SetAttribute("href", "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css")
            Dim head As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("head")
            For Each h As HtmlElement In head
                h.AppendChild(link)
            Next
        End Sub
    End Class


    Hire Me For This Job!
    Don't forget to vote for Helpful Posts and Mark Answers!
    *This post does not reflect the opinion of Microsoft, or its employees.




    Wednesday, July 4, 2018 3:28 AM
    Moderator