Introduction
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)
were installed.
- 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
image).

- 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
following code:
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 WebBrowserContextMenu control.
The
CancelEventArgs 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
calling control.
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)
{
switch (webBrowserContextMenu1.SourceControl.Name.ToUpper())
{
case "DATAGRIDVIEW1" :
{
webBrowserContextMenu1.Url = dataGridView1.SelectedCells[0].Value.ToString();
break;
}
case "TEXTBOX1" :
{
webBrowserContextMenu1.Url = textBox1.Text;
break;
}
case "PICTUREBOX1":
{
webBrowserContextMenu1.Url = "www.daytonartinstitute.org";
break;
}
}
}
What Are We Switching On?
The code in the switch():
webBrowserContextMenu1.SourceControl
gets the SourceControl which was under the mouse pointer when the Context Menu
fired.
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.
Resolution
If that happens just build the entire solution, close the form and open it again
and everything will be fine.
History
Initial article written on: May 10, 2012