Click here to Skip to main content
13,459,040 members
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 26 Mar 2008

Make a "Search Google For" Context Menu

, 26 Mar 2008
Rate this:
Please Sign up or sign in to vote.
A walkthrough for creating a handy Google Search For context menu, like in Firefox.


This article introduces the basics of making a context menu in C# .NET, but also delves into some detail about how you can dynamically generate text in the context menu. You won't need all of this, but I think it has value even if you don't. If you want a "Google For" context menu in your program, this article will give you a few hints.


When I am designing a program, I always code the important, back-end code first. This is fine, but at some point I realize I need to make the interface nicer (or even create an interface in the first place). And, once I add the amenities, such as buttons, menus, preference dialogs, etc., I wonder how I lived without them. Having a nice interface makes testing a program more enjoyable--I am not even talking about how a user might feel. For so long I neglected user interfaces and just focused on algorithm design, and logic, but I am coming to realize that user interfaces make programs so much nicer. I guess it is true that I don't browse the web on the command line--I use Safari (WebKit), which is quite nice looking. And I use Vista, which has all kinds of translucency and drop shadows. So--I like nice interfaces, so I should put some energy into making nice interfaces.

My program has a bookmark system, some menu items, a status bar, and an input box that instantly (upon key press) updates the definition text box. The program is fun to play around with for a bit. What I added today was a way for the user to right-click on a word in the definition box, and look up that word in a context menu. This wasn't a hard thing to do, but it is something that would be much easier to do in a critical situation if you have already done in a test project. Some of the requirements for this feature:

  1. The selection must appear within quotes in the context menu.
  2. The selected text must be sanitized (cleared of useless characters) before being displayed in the context menu.

Using the code

So, here's what I did.

  1. Create a new form element in the designer--a context menu strip. Go to the ToolBox on the right, while in the Designer for your form, and double-click ContextMenu. This will put a representation of the context menu in the tray.
  2. Type in any static context menu items in the context menu (it will appear on the top left). In the first screenshot, the static text would be "Copy".
  3. For the ContextMenu, add event handlers for the following three events: Opened, Opening, and ItemClicked. The best way to do this is to select the ContextMenu in the designer's tray at the bottom, and then go to the Properties pane, and then click on the lightning bolt. There, you can see the events you can make. Scroll down to the ItemClicked, Opening, and Opened events, and in the space next to the Id, double-click.

In the image above, I clicked on the ContextMenu item in the "tray", and then in Properties (on the right), I selected the lightning bolt, and double-clicked on the space next to the word Opening.

Make sure to hook up the context menu to the form control you want it to appear on! Click on the form you want the context menu for, and click on it. Then, in the Properties pane, select your new ContextMenu to the right of where it says ContextMenuStrip.


You may need to modify the text that is selected before you put it in the context menu's display. If you need to, create a nice member function with a regular expression or just some plain input-checking code..

So, where do you generate the dynamic text--in this example, "Lookup {string}" or, in a different program, this might be "Google For {string}."

void contextMenuStrip1_Opened(object sender, EventArgs e)
    // get the text from the textbox. then, set the second item in the context menu
    // to the string "Lookup {string}". You could also use the text "Google For".
    string text = textBox1.SelectedText;
    // you will want to write a method to "clean up" the text variable here,
    // but you can figure out that easily on your own.
    contextMenuStrip1.Items[1].Text = "Lookup " + text;
    // save our lookup string for use later.
    contextMenuStrip1.Tag = text;
Finally, we want to actually do something when the user clicks on the ContextMenu! We will use the ItemClicked event handler. If it doesn't exist already, double click on the entry in Properties under ItemClicked. This is an example of the EventHandler you could use. There are many ways to implement these things. Here is one way:
void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    // if we clicked on the "Lookup" item (you could use "Google" or something instead
    if (e.ClickedItem.Text.IndexOf("Lookup") != -1)
        // you can set another text box with the Tag
        inputBox.Text = (string)contextMenuStrip1.Tag;
        // or you can start IE, Firefox, or Safari (Windows) and search Google!
        // you can replace this with your national Google, or even another engine.
        Process.Start("" + (string)contextMenuStrip1.Tag);

This will create the effect in the first screenshot. "Google For" context items, to your heart's content! A web browser of the user's preference will pop up if you use Process.Start().


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


About the Author

Samuel Allen
Web Developer Dot Net Perls
United States United States
I am a freelance software developer, with knowledge and experience with many .NET technologies, and also older technologies such as C and Perl. My site Dot Net Perls is fairly popular, at least from my perspective, and have generated thousands of hits. My web apps have been featured on I love writing about technologies, almost as much as I love programming and studying them. I am a big proponent of open source and think it is the way forward for programming. I want to show people the joy of programming and writing about programming and technical topics.

You may also be interested in...


Comments and Discussions

QuestionMaterialMe Pin
Member 109278836-Jul-14 10:23
memberMember 109278836-Jul-14 10:23 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180323.1 | Last Updated 26 Mar 2008
Article Copyright 2008 by Samuel Allen
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid