Click here to Skip to main content
Click here to Skip to main content

Web Browser Context Menu Ctrl: Load Shortcut From Your App In Any Browser

By , 13 May 2012
 

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 :

  1. Determine which of the top 5 browsers (IE, FireFox, Chrome, Opera, Safari)  were installed.
  2. 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.
  3. Allow me to set the URL which would be loaded
  4. Simply start the web browser if there is no URL associated.
  5. 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:

  1. Choose the design view of your Windows form
  2. Go to your Toolbox
  3. Choose the General tab
  4. Right-click in the area where the controls normally appear.
  5. When the context menu appears, select the Choose Items... menu (see following  image). 

  1. The dialog box shown in the next image will appear.
  2. Click the Browse button and navigate to the place where you have the WebBrowserMenu.dll  -- just like you did when you added the reference.  
  3. When you select the DLL it should appear as checked -- see next image. 
  4. 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:

//
// webBrowserContextMenu1.Opening += new CancelEventHandler(webBrowserContextMenu1_Opening);
// 

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:

// void webBrowserContextMenu1_Opening(object sender, CancelEventArgs e) 

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:

// void WebBrowserOpening(object sender, CancelEventArgs e)

Just make sure the eventhandler has the same name:

// webBrowserContextMenu1.Opening += new CancelEventHandler(WebBrowserOpening);

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: 

  1. Click on the control (textbox, datagridview, etc) in Design View.
  2. Go to the Properties dialog and scroll down until you see ContextMenuStrip.
  3. Drop the list, you will see WebBrowserContextMenu1.
  4. 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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

newton.saber
Architect
United States United States
My book, __Learn JavaScript : The Lightning Guide__ has recently published at Amazon.com. You can buy the Kindle version for only $2.99.

You can see more about the book, download sample code, etc. at my web site: UncoverYourLife.com - opens in a new tab (window)

You can also see the book at Amazon.com : Learn JavaScript: The Lightning Guide - opens in a new tab (window))

Follow on   Twitter

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionMy vote of 5 PinmemberZac Greve8-Jun-12 13:00 
GeneralMy vote of 5 Pinmembernewton.saber16-May-12 17:14 
QuestionHow are browsers detected? PinmemberThe JZ16-May-12 1:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130617.1 | Last Updated 13 May 2012
Article Copyright 2012 by newton.saber
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid