Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm making a game, and the play is a picture box so I need it to move around between 9 different forms in order for the game to work, but I don't know how to do it. I tried to do "player.location = form2.location and it keeps me in form 1

What I have tried:

I tried my best but i still can't get it to work
Posted
Updated 7-May-24 5:14am

1 solution

Forms and controls aren't normally shared, and the Location property of any control is relative to the top left corner of the containing control (and Form is derived from Control and is the container for all the form controls).

If you want to actually move a control between two forms, you need to remove it from the containing Form Controls collection, and add it to Controls collection of the new Form:
C#
public static void MoveControlBetweenForms(Control c, Form src, Form dest)
    {
    src.Controls.Remove(c);
    dst.Controls.Add(c);
    }
But ... things may not work as you intended as all the events on the picture box will still be linked to the original form not the new one ...

Me? I'd move the Image from Form to Form and use a separate PictureBox for each instead.
 
Share this answer
 
v2

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