记忆游戏,PictureBox im1 =发送者为PictureBox;失去它的地址

时间:2016-10-14 08:21:26

标签: c# winforms

我正在尝试在C#中创建一个基本的记忆游戏,我使用发件人作为PictureBox来确定选择哪个图片框。之后我必须检查标签是否相等,这里im2im1失去了地址。如何保存地址以免丢失?

public partial class Form1 : Form
{
    int k = 1;
    PictureBox im1, im2;
    int r1, r2;
    public Form1()
    {
        InitializeComponent();
    }

    private void Click(object sender, EventArgs e)
    {

        if (k == 1)
        {
            PictureBox im1 = sender as PictureBox;`enter code here`
            r1 = Convert.ToInt16(im1.Tag);
            string s = "slike\\sl";
            s = s + r1.ToString() + ".jpg";
            Image i = Image.FromFile(@s);
            im1.Image = i;
            k = 2;


        }
        else
        {
            PictureBox im2 = sender as PictureBox;
            r2 = Convert.ToInt16(im2.Tag);
            string s = "slike\\sl";
            s = s + r2.ToString() + ".jpg";
            Image i = Image.FromFile(@s);
            im2.Image = i;
            k = 0;
        }
        if(k==0) {
            if (r1 == r2)
            {
                Image i = Image.FromFile(@"slike\\pogodjeno.jpg");
                im1.Image = i;
                im2.Image = i;
                im1.Enabled = false;
                im2.Enabled = false;
                k = 1;
            }
            else
            {
                Image i = Image.FromFile(@"slike\\pozadina.jpg");
                im1.Image = i;
                im2.Image = i;
                r1 = 0;
                r2 = 0;
                k = 1;
            }

        }

     }

      private void pictureBox1_Click(object sender, EventArgs e)
      {

      }
     }
}

1 个答案:

答案 0 :(得分:0)

这似乎不对:

PictureBox im1, im2;
private void Click(object sender, EventArgs e)
{
        PictureBox im1 = sender as PictureBox;`enter code here`

你可能想要这个:

private void Click(object sender, EventArgs e)
{
        im1 = sender as PictureBox;`enter code here`