检查是否单击了正确的标签?

时间:2015-04-25 11:48:21

标签: c# list design-patterns label

开始我不擅长编程,我完全不熟悉它。话虽如此,我试图制作一个游戏,标签图案出现(通过特定标签改变颜色),然后用户必须在显示后点击该特定图案。我已经制作了模式秀,并列入了一个列表。我现在遇到的问题是如何根据已经制作的随机模式检查是否有正确的标签被舔。对不起,如果我的代码看起来很笨拙,但这里(对不起,还有没有纪念):

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


namespace Spil
{
    public partial class Form1 : Form
    {
        Random rnd = new Random();
        Label[] labelArray;
        int turn = 1;
        int lives = 3;
        List<Label> orderList = new List<Label>();

        public Form1()
        {
            InitializeComponent();

            labelArray = new Label []{ label1, label2, label3, label4, label5, label6, label7, label8, label9 };
        }

        private void DisplayOrder()
        {
            for (int i = 0; i < labelArray.Length; i++)
            {
                labelArray[i].BackColor = Color.Blue;
            }

            for (int i = -2; i < turn; i++)
            {
                int chosenNumber = rnd.Next(0, 9);
                labelArray[chosenNumber].BackColor = Color.Green;
                Thread.Sleep(1000);
                labelArray[chosenNumber].BackColor = Color.Blue;
                orderList.Add(labelArray[chosenNumber]);
            }
        }


        private void Click0(object sender, EventArgs e)
        {

        }

        private void Click1(object sender, EventArgs e)
        {

        }

        private void Click2(object sender, EventArgs e)
        {

        }

        private void Click3(object sender, EventArgs e)
        {

        }

        private void Click4(object sender, EventArgs e)
        {

        }

        private void Click5(object sender, EventArgs e)
        {

        }

        private void Click6(object sender, EventArgs e)
        {

        }

        private void Click7(object sender, EventArgs e)
        {

        }

        private void Click8(object sender, EventArgs e)
        {

        }

        private void Click9(object sender, EventArgs e)
        {

        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            System.Timers.Timer t = new System.Timers.Timer(100);

            t.Elapsed += t_Elapsed;

            t.Start();
        }

        void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            ((System.Timers.Timer)sender).Stop();

            DisplayOrder();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您可以为同一个点击事件注册所有标签,并使用sender参数识别点击的标签。

for (int i = 0; i < labelArray.Length; i++)
{
    labelArray[i].BackColor = Color.Blue;
    labelArray[i].Click += label_Click;
}

void label_Click(object sender, EventArgs e)
{
    string name = ((Label)sender).Name;
}

答案 1 :(得分:0)

您需要为每个单独的标签生成Click even,您可以在此处找到包含属性选项卡的事件选项卡(如果您不知道的话)。只需在设计器中舔标签并导航到label_click事件并双击它。

Events tab