如何使用单击事件在标签中显示图像(记忆游戏)

时间:2016-06-06 10:53:29

标签: c# winforms

我需要制作记忆游戏,但是当点击标签时代码会出错,我需要将一面着色并在点击时显示图像。

这是代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Memory
{
    public partial class frmMain : Form
    {
        Random random = new Random();

        //images
        List<Image> icons = new List<Image>()
        {
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\apple.png"),
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\apple.png"),
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\bananas.png"),
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\bananas.png"),
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\grapes.png"),
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\grapes.png"),
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\kokosnoot.png"),
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\kokosnoot.png"),
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\lemon.png"),
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\lemon.png"),
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\orange.png"),
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\orange.png"),
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\peach.png"),
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\peach.png"),
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\pear.png"),
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\pear.png"),
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\pepper.png"),
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\pepper.png"),
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\pineapple.png"),
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\pineapple.png"),
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\strawberry.png"),
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\strawberry.png"),
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\watermelon.png"),
        Image.FromFile(@"C:\Users\thomas\Documents\Visual Studio 2015\Projects\Memory\Resources\watermelon.png"),
        };

        //pictures to labels
        public void AssignPicsToLabels()
        {
            foreach (Control control in tableLayoutPanel1.Controls)
            {
                Label iconLabel = control as Label;
                if (iconLabel != null)
                {
                    int randomNumber = random.Next(icons.Count);
                    iconLabel.Image = icons[randomNumber];
                    icons.RemoveAt(randomNumber);
                }
            }
        }

        public frmMain()
        {
            InitializeComponent();
            AssignPicsToLabels();
        }

        //Images randomizing
        private void RandomizeImages()
        {
            Shuffle(icons);
            int Index = 0;

            foreach (Control control in tableLayoutPanel1.Controls)
            {
                var imageLabel = control as Label;
                if (imageLabel == null)
                {
                    continue;
                }

                imageLabel.Tag = Index;
                Index++;
            }
        }

        private static Random rng = new Random();

        private static void Shuffle<T>(List<T> list)
        {
            int count = list.Count;
            while (count > 1)
            {
                count--;
                int L = rng.Next(count + 1);
                T value = list[L];
                list[L] = list[count];
                list[count] = value;
            }
        }

        private void label_Click(object sender, EventArgs e)
        {
            Label clickedLabel = sender as Label;

            if (clickedLabel != null)
            {
                var Index = (int)clickedLabel.Tag;
                clickedLabel.Image = icons[Index];

                clickedLabel.ForeColor = Color.Black;
            }
        }
    }
}

这里出错:

var Index = (int)clickedLabel.Tag

1 个答案:

答案 0 :(得分:0)

这里有一些问题 - Tag属性实际上设置为一个整数(与某些注释相反),但只有在实际调用RandomizeImages的情况下,它似乎并不像洗牌一样实际上是在AssignPicsToLabels中完成的,否则Tag属性将为null,这可能会导致您的错误。

如果您完全删除RandomizeImages并使用以下内容替换AssignPicsToLabels,我认为您将获得所需的行为:

//pictures to labels
public void AssignPicsToLabels()
{
    var iconIndices = Enumerable.Range(0, icons.Count-1).ToList();
    Shuffle(iconIndices);
    int nIcon = 0;

    foreach (Control control in tableLayoutPanel1.Controls)
    {
        Label iconLabel = control as Label;
        if (iconLabel != null)
        {
            // TODO - check for array out of bounds
            iconLabel.Tag = iconIndices[nIcon++];
        }
    }
}

这将为每个标签在Tag属性中提供一个随机且唯一的整数图像索引,然后应该在单击处理程序中给出预期的结果。您可能还希望用静态数组替换图标成员列表,因为此列表将不再自行修改。