Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Win32

Make a Search Google For Context Menu

2.00/5 (7 votes)
26 Mar 2008CPOL4 min read 1  
A walkthrough for creating a handy Google Search For context menu, like in Firefox.

Image 1

Introduction

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.

Background

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.

googlecontextmenu/contextopening.jpg

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.

googlecontextmenu/contextset.jpg

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}."

C#
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:

C#
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("http://google.com/search?q=" + (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().

License

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