While writing an RSS Reader I wanted an easy way allow the user to load the individual
articles that the RSS feed brought back(web links) via any web browser s/he chose.
However, that would mean I would have to know which browsers were installed.
I decided to create a control based on ContextMenu that would :
- Determine which of the top 5 browsers (IE, FireFox, Chrome, Opera, Safari)
- Only show the choices for the browsers which are actually installed on the
user's machine: ie - if only IE is installed, then the context menu should only
show that choice.
- Allow me to set the URL which would be loaded
- Simply start the web browser if there is no URL associated.
- Make the context menu work in any project -- simply by dropping it on a
form -- and with any control -- simply by setting the control's context menu
to my control.
Ugly Example With the Pretty Menu
Here's a picture of what the menu looks like when the user right
clicks any control that has its contextmenu set to point at it.
The over all example is a bit cluttered, but that's just because I am attempting
to show you that you can use this new WebBrowserMenu with just about any control
(DataGridView, TextBox, GroupBox, even PictureBox...and more).
Using the code
Download the code and build the project or just get the WebBrowserMenu.dll.
That dll contains all the code you need.
Built In .NET 2.0 For A Reason
I built it in .NET 2.0 so it can be used in many projects (2.0, 3.0, 3.5, 4.0).
Windows OS Platforms
I've tested this on Windows XP (Service Pack 2 & 3), Vista 32-bit and Windows
7 64-bit. Others (Windows versions) are unknown, but I believe it will work.
Add A Reference
Go to Visual Studio and open the project you'd like to use the
control in and right-click the [References] folder under Solution Explorer.
Next, browse to the place where you've stored (or built) the WebBrowserMenu.dll
and select it. It is now added as a reference to your project. This
will enable you to use the control in your project.
Add Item To Toolbox
Next, we have to add the item to the Toolbox so we can drag and drop the
control onto your Windows Form.
The easiest way I've found to get Visual Studio (2010) to do that is to:
- Choose the design view of your Windows form
- Go to your Toolbox
- Choose the General tab
- Right-click in the area where the controls normally appear.
- When the context menu appears, select the Choose Items... menu (see following
- The dialog box shown in the next image will appear.
- Click the Browse button and navigate to the place where you have the WebBrowserMenu.dll
-- just like you did when you added the reference.
- When you select the DLL it should appear as checked -- see next image.
- Click the OK button and the WebBrowserContextMenu item should appear with
a little gear icon next to it (shown in previous image).
Drag and Drop the WebBrowserContextMenu Control
Once you have followed those steps you'll be able to drag and drop one of the
WebBrowserContextMenu controls onto any Windows Form in your project.
Make sure you have a Windows Form in Design view and do that now.
Once you drop the control on the form, it will look like this:
Visual Studio automatically names the instance of the control as: webBrowserContextMenu1.
We're ready to add the code to support the menu.
Add Code To Allow You To Set URL To Open
The first thing we need to do is add an event handler that will allow you to
set the value of the URL that will open when the user selects a browser.
Since you want the URL to be set right before the user clicks the menu item we add
the Opening event handler. Open up the constructor of your form and add the
After you hit the dot(.) and type the 'O' you'll see the event appear with intellisense,
so it'll be easy.
Next, it'll prompt you to hit the tab key to add the webBrowserContextMenu1_Opening
method. You can name this method anything as long as it has a signature like:
The Opening eventhandler takes an object and the CancelEvenArgs so as long as
we have a method with that signature you can call it whatever you like:
Just make sure the eventhandler has the same name:
More About Opening Event
The Opening event fires right before the menu opens so this gives us a chance
to set the Url value of the
parameter can allow you to cancel this event, but we won't need
to do that.
Add Switch Statement To Handle Specific Controls
Next, we want to add a switch statement to our Opening event method so that we
can set the Url property of the WebBrowserContextMenu specifically related to the
Read Different Values For Url From Different Calling Controls
This is because we may want to read the .Text value of a TextBox or a specific
cell value from a DataGridView.
void webBrowserContextMenu1_Opening(object sender, CancelEventArgs e)
case "DATAGRIDVIEW1" :
webBrowserContextMenu1.Url = dataGridView1.SelectedCells.Value.ToString();
case "TEXTBOX1" :
webBrowserContextMenu1.Url = textBox1.Text;
webBrowserContextMenu1.Url = "www.daytonartinstitute.org";
What Are We Switching On?
The code in the switch():
gets the SourceControl which was under the mouse pointer when the Context Menu
The code then simply grabs the name, does a comparison and then runs specific
code depending upon which control was firing the ContextMenu. That allows
us to get the value for the URL dynamically.
See It In Action
For example, if you download my sample program and run it, you'll
see that if choose a cell in the DataGridView and then right-click and select
a browser, the browser will load the URL that was in the DataGridView cell.
This would load http://oreilly.com.
And this one would load whatever value (codeproject.com) is
typed into the TextBox:
I Have All 5 Browsers On My Computer: Do You?
Keep in mind that all five browsers show up as a choice on my computer, because
I have all 5 of them installed. If you do not, then you will only see
menu items for those browsers that you have installed.
That is the beauty of this control. It is dynamic and won't confuse your
users, with options they do not have.
One More Thing To Do: Set the Context Menu of Your Calling Control
To make the Context Menu control appear when a user right-clicks a control on
your form you simply set the ContextMenu property on your control. To do that:
- Click on the control (textbox, datagridview, etc) in Design View.
- Go to the Properties dialog and scroll down until you see ContextMenuStrip.
- Drop the list, you will see WebBrowserContextMenu1.
- Select WebBrowserContextMenu1
In my example, you can see that I have chosen to add the
context menu to GroupBox1.
Bad URL, No URL
If the URL is invalid, the browser will attempt to load it and fail.
If the URL is blank the browser will open with its home page (default page).
Points of Interest
I've used this control in a couple of projects. Note that it contains a
Browser class which keeps all the browser info separate. This class has further
uses as I've extended it to read all the browser history from all browsers for a
single repository of history information. I'll be writing more articles on
that in the future. There are lots of applications for that.
If you've enjoyed this article and my code, I hope you'll consider reading my
upcoming book, The Software Development Map, which will release
as a Kindle book on May 31, 2012.
Odd Visual Studio Designer Error
If you see something like the following:
It is simply because you are referencing the control project and the DLL which
contains the control is not built yet, but the form requires it to be built to draw
the design surface.
If that happens just build the entire solution, close the form and open it again
and everything will be fine.
*Important Update* (v220.127.116.11) Please Read First
Iteration 2 of Update -- Most Recent
I have finally figured out the problem (which I first mentioned below see Iteration 1 of Update section below) and have updated the code and uploaded the fix. Here's the deal, for some reason when the code tries to find/register the installed browsers it (in some cases) registers the browsers multiple times. That means it adds a new eventhandler for the ChromeMenu_Click multiple times. It does this for various browsers which it believes are installed numerous times.
I've added a simple boolean check to see if ChromeMenuItem is already initialized (not null) and only add the EventHandler if it has not already been added -- if the ChromeMenuItem hasn't already been created.
The new code looks like the following (from WebBrowserContextMenu.cs) but basically I just added the IF statement checking for the null condition. Also, this is the example for the Chrome browser, but this check is now added for each of the web browsers supported (Big 5 - IE, FF, Chrome, Safari, Opera) by this control.
As I examine this further, I find that the code above is in the InitializeBrowserMenus() method which is only called from the WebBrowserContextMenu Constructor. It is odd that now for some reason this constructor and Initialize method are (in some cases) called twice and this is somehow different on the two OS versions (as mentioned below in Iteration 1 of Update section)
if (ChromeMenuItem == null)
ChromeMenuItem = (ToolStripMenuItem)tsmi.DropDown.Items;
ChromeMenuItem.Click += new EventHandler(ChromeMenu_Click);
ChromeMenuItem.Visible = true;
Iteration 1 of Update
Suddenly, while running another program I've written which implements the browser context menu control in this article, I've found that opening the link via the control actually opens 2 windows in the browser (Chrome - non-default browser for my machine). It is as if the ContextMenu code is firing twice.
My machine is running Windows 7 and it did recently update. I am running an older version of Chrome (Version 26.0.1410.43 m). I have also tested the sample project from this article and it still opens two windows or tabs in Chrome.
I then moved to another machine (Virtual Machine) running Windows Server 2003 R2 (SP2) and running Chrome (Version 28.0.1500.72 m). The sample project running in that environment only opens one tab or window in Chrome.
Something Has Changed In the Way ContextMenu Events Fire
I believe that something has changed in the way the contextmenu events fire. It is as if the OnMenuClick is firing twice. On the same Windows 7 machine where I'm seeing two Chrome windows open, I also have FireFox and it opens two windows also. My default browser on that machine is IE9.x and it only opens one window/tab as expected. What is this craziness?
I set FireFox to be the default browser and this control still opens two tabs/windows when I attempt to open one.
Visual Studio 2012?
The only other update I've made to the machine is adding Visual Studio 2012? Would that affect the way the contextmenu events fire? Not sure. Please leave a comment if you get the same behavior. Thanks.
Initial article written on: May 10, 2012
Release Version 18.104.22.168 : July 17, 2013 This fixes the issue where multiple windows may open upon attempting to open the URL in one of the installed browsers. Please see the last section *Important Update* for more details.