gpt4 book ai didi

c# - 单击按钮时图像移动

转载 作者:太空宇宙 更新时间:2023-11-03 21:02:34 29 4
gpt4 key购买 nike

我想通过单击按钮将一个图片框中的图像移动到另一个图片框中,反之亦然,我很确定我的代码在理论上应该可以工作,但由于某种原因它不会执行。任何帮助将不胜感激!!这是我的代码:

    int chicken_move = 0;

private void button_Chicken_Click(object sender, EventArgs e)
{
chicken_move++;
if (chicken_move > 1)
{
chicken_move = 0;
}

switch (chicken_move)
{
case 0:

pictureBox_Micro.Image = pictureBox_Uncooked.Image;
pictureBox_Uncooked.Image = null;

break;

case 1:

pictureBox_Uncooked.Image = pictureBox_Micro.Image;
pictureBox_Micro.Image = null;

break;
}

最佳答案

您需要使用交换技术来更改图片。否则,您将在第一次点击后丢失它。

 public partial class Form1 : Form
{
private bool isFirstOne;
Image forSwap;
public Form1()
{
InitializeComponent();

string path1 = @"C:\Users\...\...\somePic.png";

pictureBox1.Image = Image.FromFile(path1);

forSwap = null;
isFirstOne = false;
}


private void button1_Click(object sender, EventArgs e)
{
switch (isFirstOne)
{
case true:
forSwap = pictureBox2.Image;
pictureBox1.Image = pictureBox2.Image;
pictureBox2.Image = null;
break;

case false:
forSwap = pictureBox1.Image;
pictureBox2.Image = pictureBox1.Image;
pictureBox1.Image = null;
break;
}
isFirstOne = !isFirstOne;
}
}

关于c# - 单击按钮时图像移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43923807/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com