Moving a picture that is inside a PictureBox to another PictureBox

后端 未结 1 1641
你的背包
你的背包 2021-01-25 12:35

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

相关标签:
1条回答
  • 2021-01-25 12:59

    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;
    
    0 讨论(0)
提交回复
热议问题