Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when I right-click on a picturebox, by using context menu items i am showing the labels like saveImageAs.
problem: when i right click on a picture box, it shows saveImageAs, when i click saveImageAs i will hit

private void saveImageAsToolStripMenuItem_Click(object sender, EventArgs e)
{
//what should i use instead of click to hit form_Mouseclick

pictureBox1.Click += form_MouseClick;
pictureBox2.Click += form_MouseClick;

}

here what should i use instead of pictureBox1.click to hit form_MouseClick

If anyone could help I would be most greatful.

private void saveImageAsToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.Click += form_MouseClick;
pictureBox2.Click += form_MouseClick;

}

private void form_MouseClick(object sender, MouseEventArgs e)
{
PictureBox pb = sender as PictureBox;
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Images|*.png;*.bmp;*.jpg";
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string filepath = System.IO.Path.GetExtension(sfd.FileName);
}
if (pb != null && sfd.FileName != null)
{
Image im = pb.Image;
SaveImage(im, sfd.FileName);
}
}

private static void SaveImage(Image im, string destPath)
{
im.Save(destPath, System.Drawing.Imaging.ImageFormat.Png);
}

What I have tried:

I tried

private void saveImageAsToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.MouseClick+= form_MouseClick;
pictureBox2.MouseClick+= form_MouseClick;

}
Posted
Updated 17-Apr-17 19:47pm

1 solution

You must check in your MouseClick-Method which MouseButton was pressed/used.
For this you can use e.Button :
C#
if (e.Button == Windows.Forms.MouseButtons.Right) {  }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900