C# 如何通过点击事件在标签中显示图像(记忆游戏)

C# 如何通过点击事件在标签中显示图像(记忆游戏),c#,winforms,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 Sys

我需要做一个记忆游戏,但代码给出了一个错误,当标签上点击,我需要一个侧面的颜色,并显示图像时点击

代码如下:

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;
            }
        }
    }
}

这里有几个问题-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属性中为每个标签提供一个随机且唯一的整数图像索引,然后该索引将在单击处理程序中提供预期的结果。您可能还想用静态数组替换图标成员列表,因为此列表本身将不再被修改。

好吧,仅仅借助显式cast
(int)str
无法将
字符串
转换为
int
,因为
字符串
不是
int
。您可以使用
Convert.ToInt32(“”)
instead这样做:“var Index=Convert.ToInt32(clickedLabel.Tag);”没有工作您似乎没有使用将索引与标签标记关联的
RandomizeImages
方法。相反,您使用的是
AssignPicsToLabels
,它做了一些不同的事情,并且还破坏了
图标列表。@Fabjan这不是VB6,
控件。标记
属性类型是
对象
。索引是在方法RandomizeImages()中定义的。将索引移动到全局空间。试着注释掉ForeColor=color.Black,看看会发生什么。有时,设置image=null,然后设置为图像将更新表单并更正问题。这一行:“Enumerable.Range(0,icons.Count-1).ToList();”现在说:“运算符-不能应用于method group和int类型的操作数”我将数组设置为Image arraySorry,请将.Count更改为.Length现在它是ArrayTanks。但是现在这行代码是:iconLabel.Tag=iconIndices[nIcon++];说明:mscorlib.dllSorry中发生类型为“System.ArgumentOutOfRangeException”的未处理异常,我的坏-I可枚举。范围包括在内。更改为可枚举范围(0,图标长度);(即失去-1)。好的,谢谢,但前景色无法覆盖图像。如果你想很快帮上忙,谢谢
//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++];
        }
    }
}