0
我想創建一個從PictureBox繼承的自定義PictureBox類。自定義繼承類中的MouseClick事件處理程序
public partial class Custom : PictureBox
{
public Custom()
{
this.Size = new System.Drawing.Size(20, 20);
this.Image = WindowsFormsApplication1.Properties.Resources.icon;
}
private void Custom_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
}
}
}
我還創建了contextMenuStrip,但無法將它指向我的MouseClick事件。如果我嘗試使用
contextMenuStrip1.Show(Cursor.Position);
我得到contextMenuStrip1不會在當前環境中存在的錯誤。如何處理這個事件?
contextMenuStrip1可能是在表單的代碼中定義的,所以它在自定義圖片框中不可用。你必須在這個類中創建一個新的才能使用它。 – user2737037