Context Menu Strip:
C# context menu strips are
the menus that pop up when the user clicks with the right hand mouse button over a control
or area in a Windows based form. They are called context menus because the menu is usually specific to the object over which the
mouse was clicked. Context menus are also sometimes referred to as Popup Menus
or Shortcut menus.
Now I am going to explain how to add context strip to
our form and how to add events to that menu items using an example.
Example program:
Steps:
1)
Open a windows application.
2)
Drag the Context strip menu from
tool box into form
3)
Double Click on Context Menu Strip and enter the text in the available box on
form as shown below
4) Enter number menu items u want add
for that context menu strip. In this example I am adding 4 items those are 1) add 2) Sub 3) Clear 4) Exit.
5)
Add three text boxes and labels as look like below.
6)
Open the Form Properties (by right
click with mouse and select properties). Select Context Menu Strip Property and
select ContextMenuStrip1 from drop down list.
7)
To write the code for menu items
Double Click on each item and write the following code in event handlers.
private void addToolStripMenuItem_Click(object sender, EventArgs
e)
{
int result =
int.Parse(textBox1.Text) + int.Parse(textBox2.Text);
textBox3.Text = Convert.ToString(result);
}
private void subToolStripMenuItem_Click(object sender, EventArgs
e)
{
int result
= int.Parse(textBox1.Text) - int.Parse(textBox2.Text);
textBox3.Text = Convert.ToString(result);
}
private void xToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
private void exitToolStripMenuItem1_Click(object sender, EventArgs
e)
{
Application.Exit();
}
Reason
for Output:
1)
Entered
100 in text box1
2)
Entered
200 in text box2
3)
Selected
Add from Context Menu Strip.
4)
Result
300 is
displayed in text box3
context menu design in C#
ReplyDelete