- Back to Home »
- Windows Applications »
- ContextMenuStrip
Posted by :
Sudhir Chekuri
Thursday, 18 February 2016
Context menus are appeared when user right-click. They looks like a floating menu. Different context menus can be displayed in a form based on the location where user has clicked. Multiple context menus can be created and they has to be assigned to specific controls in the form.
Default Event: Click which is fired when user click on the item in the contextmenustrip.
Property: For every control including form a propery called contextMenuStrip will be present which is used to assign a context menu strip to it which is displayed when user right click on that control in the run time.
Steps to create context menu strip
Drag and drop a contextmenustrip control to the form which is displayed at the bottom panel of the form but also shows up auto generated textboxes at the top of the form to add items.
You can create the context menu strip items just by adding text in the automatically generated textboxes which are displayed at the top of the form.
Double click on the each item to see the generated event handler of that specific item and you can create event handler for each item to write some code that has to be executed when you click on that item in the runtime.
For every control including Form will have a property called contextMenuStrip where you can add context menu strip name which has to displayed when user right click on that control.
Default Event: Click which is fired when user click on the item in the contextmenustrip.
Property: For every control including form a propery called contextMenuStrip will be present which is used to assign a context menu strip to it which is displayed when user right click on that control in the run time.
Steps to create context menu strip
Drag and drop a contextmenustrip control to the form which is displayed at the bottom panel of the form but also shows up auto generated textboxes at the top of the form to add items.
You can create the context menu strip items just by adding text in the automatically generated textboxes which are displayed at the top of the form.
Double click on the each item to see the generated event handler of that specific item and you can create event handler for each item to write some code that has to be executed when you click on that item in the runtime.
For every control including Form will have a property called contextMenuStrip where you can add context menu strip name which has to displayed when user right click on that control.
Example
code to copy and paste content of a text box by using context menu present on
form
Create
a context menu with 3 items – Copy, Paste and clear
Create a textbox and assign context menu to the form.
string s = string.Empty;
Create a textbox and assign context menu to the form.
string s = string.Empty;
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
s =
textBox1.Text;
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Text = s;
}
private void clearToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}