显示数组中的图像,并在单击时显示图像的名称

时间:2017-01-22 01:56:35

标签: c# arrays random bitmap

enter image description here作为一个先驱:我对C#很新,是的,这是作业。

我正在制作一个程序,将52​​张卡片.bmp存储在一个阵列中,随机分配哪些卡片出现在5个图片框中,然后当用户点击该卡片时,它会显示卡片的名称。我坚持了最后一部分。提前谢谢!

 public partial class cardIdentifier : Form
{
    public cardIdentifier()
    {
        InitializeComponent();
        pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
        pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
        pictureBox4.SizeMode = PictureBoxSizeMode.StretchImage;
        pictureBox5.SizeMode = PictureBoxSizeMode.StretchImage;
        pictureBox1.Image = deck[new Random().Next(0, deck.Length)];
        pictureBox2.Image = deck[new Random(+1).Next(0, deck.Length)];
        pictureBox3.Image = deck[new Random(+5).Next(0, deck.Length)];
        pictureBox4.Image = deck[new Random(+10).Next(0, deck.Length)];
        pictureBox5.Image = deck[new Random(-7).Next(0, deck.Length)];
    }
    //Creating and adding images to the array
    Image[] deck = new Image[52] { Card_Identify.Properties.Resources.clubAce, Card_Identify.Properties.Resources.clubTwo, Card_Identify.Properties.Resources.clubThree, Card_Identify.Properties.Resources.clubFour, Card_Identify.Properties.Resources.clubFive , Card_Identify.Properties.Resources.clubSix , Card_Identify.Properties.Resources.clubSeven , Card_Identify.Properties.Resources.clubEight , Card_Identify.Properties.Resources.clubNine , Card_Identify.Properties.Resources.clubTen , Card_Identify.Properties.Resources.clubJack , Card_Identify.Properties.Resources.clubQueen , Card_Identify.Properties.Resources.clubKing , Card_Identify.Properties.Resources.spadeAce , Card_Identify.Properties.Resources.spadeTwo , Card_Identify.Properties.Resources.spadeThree , Card_Identify.Properties.Resources.spadeFour , Card_Identify.Properties.Resources.spadeFive , Card_Identify.Properties.Resources.spadeSix , Card_Identify.Properties.Resources.spadeSeven , Card_Identify.Properties.Resources.spadeEight , Card_Identify.Properties.Resources.spadeNine , Card_Identify.Properties.Resources.spadeTen , Card_Identify.Properties.Resources.spadeJack , Card_Identify.Properties.Resources.spadeQueen , Card_Identify.Properties.Resources.spadeKing , Card_Identify.Properties.Resources.heartAce , Card_Identify.Properties.Resources.heartTwo , Card_Identify.Properties.Resources.heartThree , Card_Identify.Properties.Resources.heartFour , Card_Identify.Properties.Resources.heartFive , Card_Identify.Properties.Resources.heartSix , Card_Identify.Properties.Resources.heartSeven , Card_Identify.Properties.Resources.heartEight , Card_Identify.Properties.Resources.heartNine , Card_Identify.Properties.Resources.heartTen , Card_Identify.Properties.Resources.heartJack , Card_Identify.Properties.Resources.heartQueen , Card_Identify.Properties.Resources.heartKing , Card_Identify.Properties.Resources.diamondAce , Card_Identify.Properties.Resources.diamondTwo , Card_Identify.Properties.Resources.diamondThree , Card_Identify.Properties.Resources.diamondFour , Card_Identify.Properties.Resources.diamondFive , Card_Identify.Properties.Resources.diamondSix , Card_Identify.Properties.Resources.diamondSeven , Card_Identify.Properties.Resources.diamondEight , Card_Identify.Properties.Resources.diamondNine , Card_Identify.Properties.Resources.diamondTen , Card_Identify.Properties.Resources.diamondJack , Card_Identify.Properties.Resources.diamondQueen , Card_Identify.Properties.Resources.diamondKing  };

//Changes the card shown in picture box
    private void button1_Click(object sender, EventArgs e)
    {
        pictureBox1.Image = deck[new Random().Next(0, deck.Length)];
    }

    private void button2_Click(object sender, EventArgs e)
    {
        pictureBox2.Image = deck[new Random().Next(0, deck.Length)];
    }

    private void button3_Click(object sender, EventArgs e)
    {
        pictureBox3.Image = deck[new Random().Next(0, deck.Length)];
    }

    private void button4_Click(object sender, EventArgs e)
    {
        pictureBox4.Image = deck[new Random().Next(0, deck.Length)];
    }

    private void button5_Click(object sender, EventArgs e)
    {
        pictureBox5.Image = deck[new Random().Next(0, deck.Length)];
    }


    public void pictureBoxes_Click(object sender, EventArgs e)
    {
        //THIS IS WHERE IM STUCK, NEED TO DISPLAY NAME OF CARD WHEN ITS CLICKED!
    }
}

}

2 个答案:

答案 0 :(得分:1)

创建另一个类:

public class Card
{
    public Image CardImage {get; set; }

    public string Name {get; set; }
}

创建这些类的数组,而不是像这样的Image数组。这样可以帮助您将卡片中的图片及其名称保存在一个实例中,以便日后获取:

Card[] deck = new Card[52] { new Card { CardImage = Card_Identify.Properties.Resources.clubAce, Name = "ClubAce" }, // ... the rest of your cards };

创建图片框时,请将Tag属性设置为卡片名称,如下所示:

var card5 = deck[new Random(-7).Next(0, deck.Length)];
pictureBox5.Image = card5.CardImage;
pictureBox5.Tag = card5; // Later on you will read this to get the name

并改变点击方法,如下所示:

public void pictureBoxes_Click(object sender, EventArgs e)
{
    //THIS IS WHERE IM STUCK, NEED TO DISPLAY NAME OF CARD WHEN ITS CLICKED!
    var cardName = ((sender as PictureBox).Tag as Card).Name
}

答案 1 :(得分:0)

您的代码中没有任何地方我看到关系位图/卡片名称。

您可以执行以下操作,一个用于定义卡片的小班:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Card[] _cards;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // build your array of cards
            Card[] cards =
            {
                new Card
                {
                    Path = "ace_heart.png",
                    Name = "Ace Of Hearts"
                }
                // etc ...
            };

            // load their bitmaps
            foreach (var card in cards)
            {
                card.Bitmap = new Bitmap(card.Path);
            }

            // keep a reference
            _cards = cards;
        }

        private int GetRandomCardIndex(int seed)
        {
            var random = new Random(seed);
            var next = random.Next(52);
            return next;
        }

        private void Demo()
        {
            var cardIndex = GetRandomCardIndex(1234);
            var card = _cards[cardIndex];
            var cardName = card.Name; // there you are
            var cardBitmap = card.Bitmap;
        }
    }

    internal class Card
    {
        public Bitmap Bitmap;
        public string Name;
        public string Path;
    }
}

我发现您的卡已放入Resources,在我的示例中,这些是本地文件。最终可以从Resources获取卡片名称,但它可能超出了您的作业范围。

在项目中部署卡片图片:

  • 删除项目中的位图
  • 全部选中它们,然后转到属性窗口
    • Build action = Content
    • Copy to Output Directory = Copy if newer
  • 或者只是复制EXE旁边的所有位图(在bin \ Debug子目录中)

enter image description here