taji01
taji01

Reputation: 2615

If statement with ContextMenuStrip

I'm using WinForms. Inside my WinForms application I have a contextMenuStrip if you rightclick on the picturebox, contextMenuStrip will appear with a list of items you can click on. How do i call the "sizeMEToolStripMenuItem_Click" method into another method.

example:

enter image description here

 private void sizeMEToolStripMenuItem_Click(object sender, EventArgs e)
 {
    if(sizeMEToolStripMenuItem.isclicked) //.isClicked is somthing i made up
     {
       e.Graphics.DrawImage(pictureBox1.Image, movingPoint); <- This draws and shows image
     }
    else 
    {
      //e.Graphics.DrawImage(pictureBox1.Image, movingPoint); <- Hide this image
    }

 } 


private void pictureBox1_Paint_1(object sender, PaintEventArgs e)
{
 //e.Graphics.DrawImage(pictureBox1.Image, movingPoint);

}

Upvotes: 0

Views: 506

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125277

You can simply hanlde click event of that Item and put your codes in the Click event handler.

Open designer, select your context menu, select sizeMEToolStripMenuItem and double click on it. This way when you click on sizeMEToolStripMenuItem this method runs.

You can also set CheckOnClick property of sizeMEToolStripMenuItem to true and check value of Checked propery.

private void sizeMEToolStripMenuItem_Click(object sender, EventArgs e)
{
    if( sizeMEToolStripMenuItem.Checked )
    {  
        //Do what you need, for example:
        //MessageBox.Show("checked");
        //To force paint event trigger you can uncomment next line:
        //pictureBox1.Invalidate();  
    }
}

To force paint event trigger, call this method in click event:

pictureBox1.Invalidate();

In paint event if you need to check if the item has been clicked, check value of Checked:

private void pictureBox1_Paint_1(object sender, PaintEventArgs e)
{
    if( sizeMEToolStripMenuItem.Checked )
    {  
         //Do somethings
    }     
}

You can also set Checked property value in code.

Upvotes: 1

Related Questions