If 语句与 ContextMenuStrip

If statement with ContextMenuStrip

我正在使用 WinForms。在我的 WinForms 应用程序中,我有一个 contextMenuStrip 如果您右键单击图片框,contextMenuStrip 将出现,其中包含您可以单击的项目列表。我如何将 "sizeMEToolStripMenuItem_Click" 方法调用到另一个方法中。

示例:

 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);

}

您可以简单地处理该项目的点击事件并将您的代码放入点击事件处理程序中。

打开设计器,select 您的上下文菜单,select sizeMEToolStripMenuItem 并双击它。这样,当您单击 sizeMEToolStripMenuItem 时,此方法就会运行。

您还可以将 sizeMEToolStripMenuItemCheckOnClick 属性 设置为 true 并检查 Checked 属性.

的值
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();  
    }
}

要强制触发绘画事件,请在点击事件中调用此方法:

pictureBox1.Invalidate();

在绘画事件中如果你需要检查项目是否被点击,检查Checked的值:

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

您也可以在代码中设置Checked 属性值。