2015-10-14 76 views
0

我正在使用WinForms。在我的WinForms應用程序中,我有一個contextMenuStrip 如果你右鍵單擊圖片框,contextMenuStrip將出現一個你可以點擊的項目列表。如何將「sizeMEToolStripMenuItem_Click」方法稱爲另一種方法。如果聲明與ContextMenuStrip

例如:

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

} 
+1

編輯上下文條,雙擊想要發生點擊事件的菜單項,然後編寫點擊處理程序。 –

+0

當你點擊該項目時,顯然你點擊了它,你需要什麼?你需要'Checked'屬性嗎? –

+0

我重寫了我的代碼,使其更清晰。當用戶點擊事件處理程序時,我將如何使用(DrawImage(pictureBox1.Image,movingPoint) – taji01

回答

1

您可以直接點擊hanlde該項目的事件,並把你的代碼在Click事件處理程序。

打開設計器,選擇您的上下文菜單,選擇sizeMEToolStripMenuItem並雙擊它。這種方式當你點擊sizeMEToolStripMenuItem這個方法運行。

您還可以將sizeMEToolStripMenuItemCheckOnClick屬性設置爲true,並檢查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(); 
    } 
} 

要強制漆事件觸發,調用此方法中單擊事件:

pictureBox1.Invalidate(); 

在油漆事件,如果你需要檢查,如果該項目已被點擊,檢查Checked值:

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

您還可以在代碼中設置Checked屬性值。

+0

我知道這一點,但我需要在其他地方調用此方法。 – taji01

+0

事件處理程序會爲您執行此操作。當您單擊菜單時,將執行此方法的代碼。 –

+0

@ taji01如果您對答案有任何疑問,請告知我。 –