0

how to keep two different events for right click as well as left click? i want zoomIn for left click and zoomOut for right click i have written my code in the following manner if any mistakes or errors please help me

i mean two different functions or events for each right click as well as left click and here goes my program

 private void pictureBox1_MouseClick_1(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right )
        {
            double zoomLevel = 1.1;
            System.Drawing.Rectangle screenSize = new System.Drawing.Rectangle();

            screenSize.Width = SystemInformation.VirtualScreen.Width ;
            screenSize.Height = SystemInformation.VirtualScreen.Height;

            //int zoomFactor = 10;

            Image img = pictureBox1.Image;
            Bitmap bitMapImg = new Bitmap(img);


            if (bitMapImg.Width < screenSize.Width && bitMapImg.Height < screenSize.Height)
            {
                Size newSize = new Size((int)(bitMapImg.Width / zoomLevel), (int)(bitMapImg.Height / zoomLevel));
                Bitmap bmp = new Bitmap(bitMapImg, newSize);

                pictureBox1.Image = (Image)bmp;
                pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
            }
        }
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {

            double zoomLevel = 1.1;
            System.Drawing.Rectangle screenSize = new System.Drawing.Rectangle();

            screenSize.Width = SystemInformation.VirtualScreen.Width * 10;
            screenSize.Height = SystemInformation.VirtualScreen.Height * 10;

            //int zoomFactor = 10;

            Image img = pictureBox1.Image;
            Bitmap bitMapImg = new Bitmap(img);


            if (bitMapImg.Width < screenSize.Width && bitMapImg.Height < screenSize.Height)
            {
                Size newSize = new Size((int)(bitMapImg.Width * zoomLevel), (int)(bitMapImg.Height * zoomLevel));
                Bitmap bmp = new Bitmap(bitMapImg, newSize);

                pictureBox1.Image = (Image)bmp;
                pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
            }

    }
Mahesh Mahi
  • 21
  • 1
  • 6

3 Answers3

3

Your best bet is one event-handler and a switch:

switch(e.Button) {
    case whatever.Left: LeftMouseClick(e); break;
    case whatever.Right: RightMouseClick(e); break;
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

Use only one event handler pictureBox1_Click and use an if statement to decide what to do :

private void pictureBox1_MouseClick_1(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Right )
    {
       DoRightClickStuff();
    }
    else if (e.Button == System.Windows.Forms.MouseButtons.Left )
    {
        DoLeftClickStuff();
    }
 }
quantdev
  • 23,517
  • 5
  • 55
  • 88
0

You will have to use the MouseUp or MouseDown event instead of the Click event to capture right click.

Try like this

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            MessageBox.Show("Left");
        }
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            MessageBox.Show("Right");
        }    
    }
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115