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:
- The selection must appear within quotes in the context menu.
- 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.
- 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. - 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
". - 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)
{
string text = textBox1.SelectedText;
contextMenuStrip1.Items[1].Text = "Lookup " + text;
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 (e.ClickedItem.Text.IndexOf("Lookup") != -1)
{
inputBox.Text = (string)contextMenuStrip1.Tag;
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()
.