Moving a picture that is inside a PictureBox to another PictureBox

孤街浪徒 提交于 2019-12-20 04:13:57

问题


How can I move a picture that is inside a picturebox to another picturebox?

I want to use it for moving the pieces of chess game. I have a took picturebox for each place, so I have 64 pictureboxes.


回答1:


You can just assign the Image displayed in one picture box to another picture box. If you then want to remove the image from the original picture box (so that it is not displayed twice), you can set its Image property to null (or, of course, you can assign any other image of your choice). Like so:

//Assign the image in one picture box to another picture box
mySecondPicBox.Image = myFirstPicBox.Image;

//Clear the image from the original picture box
myFirstPicBox.Image = null;


If you want to swap the images displayed in two picture boxes, you need to temporarily store one of the picture objects in a variable. So you can use very similar code, with a slight modification:

//Temporarily store the picture currently displayed in the second picture box
Image secondImage = mySecondPicBox.Image;

//Assign the image from the first picture box to the second picture box
mySecondPicBox.Image = myFirstPicBox.Image;

//Assign the image from the second picture box to the first picture box
myFirstPicBox.Image = secondImage;


来源:https://stackoverflow.com/questions/4243162/moving-a-picture-that-is-inside-a-picturebox-to-another-picturebox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!