c# move picturebox from an array of picturebox up when you hover mouse

473 views Asked by At

I'm working on C# windows form. I have an array of picturebox, displayed on the form. The array has the size of 13, and they're all side by side. How can I make it so that when I click on a picturebox, it is moved up by let's say +20 on y. My code to make the picture boxes. The pb1 and p1 are declared above

void print_Deck(int x, int y, double[] a){
        double n;
        for (int i = 0; i < 13; i++)
        {

            pb1[i] = new PictureBox();
            // pb1[1].Image = Properties.Resources.img1;
            pb1[i].Visible = true;
            pb1[i].Location = new Point(0, 0);
            this.Size = new Size(800, 600);
            pb1[i].Size = new Size(46, 65);
            pb1[i].SizeMode = PictureBoxSizeMode.StretchImage;
            pb1[i].Location = new Point(x, y);
            n= a[i];
            im = face(n);
            pb1[i].Image = im;
            this.Controls.Add(pb1[i]);
            x = x + 20;
        }
    }
3

There are 3 answers

5
KrLontoc On BEST ANSWER

You can try adding Click event on your Picturebox then you can try this code on the Click function.

You can manipulate the location by using Top propery.

Picturebox.Top -= 20; // move the picture box upward

or

Picturebox.Top += 20; // move the picture box downward

or use the .Location = New Point(X,Y)

Picturebox.Location = new Point(Picturebox.Location.X, Picturebox.Location.Y + 20);

Here's how you add the EventHandler to your picturebox.

Picturebox.Click += new System.EventHandler(this.Picturebox_ClickFunction);

then create a fucntion with the name Picturebox_ClickFunction

private void Picturebox_ClickFunction(object sender, EventArgs e)
{
     PictureBox pb1 = (PictureBox)sender; // you need to cast(convert) the sende to a picturebox object so you can access the picturebox properties
}

then you can use the code I provided above.

0
Naresh On

You can try PictureBox.Top property with Anchor property or use PictureBox.Location.

0
Tom Gothorp On

You could register the PictureBox's 'Click' event to adjust the 'Margin' property by the required amount