0

I am new to C# and want to change the size of some Pictureboxes based on one trackbar Value. If i write the below code refering to a spesific picturebox (e.g. Picturebox1 instead of PB) it works, but i would like to use one Doubleclick event for all Pictureboxes that i Doubleclick.

The below code gives PB = null. I get the selected Pictureboxname but how can i refer to this Picturebox?

'''

  private void PictureBoxesDoubleClick(object sender, EventArgs e)
        {
            //get the selected Picturebox name
            String PictureBoxName = ((PictureBox)sender).Name;

        //This part doesn't work
        PictureBox PB = (PictureBox)this.Controls[PictureBoxName];

        //Resize the Picture box according to the trackBar Value
        PB.Size = new Size(trackBar1.Value, trackBar1.Value);
        PB.Left = (this.ClientSize.Width - pictureBox1.Width) / 2;
        PB.Top = (this.ClientSize.Height - pictureBox1.Height) / 2;
     }

'''

Which pictureBox was selected? C#

Thank you

Torr
  • 1
  • 2

1 Answers1

0

This is the common function which I used for the two picture boxes on their double click and it is working fine. Check if you also had a common function for all the picture boxes in the form.

private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
    //get the selected Picturebox name
    String PictureBoxName = ((PictureBox)sender).Name;

    PictureBox PB = (PictureBox)this.Controls[PictureBoxName];

    //Resize the Picture box according to the trackBar Value

    PB.Size = new Size(trackBar1.Value*10, trackBar1.Value*10);
    PB.Left = (this.ClientSize.Width - pictureBox1.Width) / 2;
    PB.Top = (this.ClientSize.Height - pictureBox1.Height) / 2;

}

Two picture boxes having the same double click event.This is the image before any double click event is fired.

This is when the double click is fired on both the picture boxes.

  • You are right, i changed the picturebox Backcolor and now the change is visibel. I was looking at the image of the picturebox that doesn't change, even if the picturebox is smaller than the image. Do you have a idea why the image is not changing according to the size of the picturebox? – Torr Apr 21 '20 at 10:16
  • Try setting sizemode property of picturebox to zoom. This might help. – Aman singh Parihar Apr 21 '20 at 10:24
  • I found it, i have to change picturebox size mode to "stretchimage". Thank you for your Help. – Torr Apr 21 '20 at 10:59