Getting and Displaying the Favicon
In case you’re creating a browser in your project and want to get the favicon to display instead of the form icon or tab then it’s just a couple of lines of code that need to be added.
There are several ways to go about this but I’ll show two ways that I’ve found to work well.
The first one looks like
The second looks like
Now this is just to obtain the favicon.ico file from the server and doesn’t really check for errors, which mean that if there is a 404 response then expect to obtain an exception, but that can be handled accordingly. We’ll look into that in a bit.
As to where to place this, well it varies. In case you’re going to use it to replace the form icon then it’s two lines of code
In case you’re going to use it as the tab icon then you need to add the image to an image list and then set the tab image, this takes a bit more lines of code
Personally I prefer to create a new function, or sub as no return value is necessary, to add this code to so that you don’t have to add it to everywhere you may want to get the favicon, even though it’s really just one time that you would usually check for the icon and that would be the documentcompleted function. My block of code looks like
This is the current one I’ve been using but I’ll be looking into making it a bit better using the first method I mentioned earlier on this post as it allows me to check for a 404 response prior to attempting to set the icon or image to anything. In my current block of code if the favicon.ico doesn’t exist it throws an exception and that just isn’t very clean.
The first if in my function is a simple check to see if the site being accessed is using the default port 80 or 443 and if not then it adds it to the URL so that the address is correct. I had to add this because of the internal company sites I’m accessing through the program don’t always use port 80 or 443.
Aside from that, I created a function that is called logDebug and it’s used to create a log for the program, guess that was somewhat self explanatory, where I provide two parameters, the first being the message and the second being the stacktrace which is optional. It’s a simple function that I can copy from project to project and keep the same formatting.