Tagging an array of bitmaps with matching string values

I am making a Scissor Jump Rope game for school. I am stuck on the assignment part where we need to tag bitmaps with the appropriate names using the tag property. I created an array of names and an array of bitmaps.

I'm not sure how to use the tag property for this. Exact instructions:

Create an array of string objects and initialize them to contain the string values ​​"rock", "paper", "scissors", "lizard", "spock". Add code to each of the bitmaps with the corresponding string values. (eg bitmap "properties.resources.rock" should be marked with the string "rock".

private void Form1_Load(object sender, EventArgs e)
{
    string[] names =
    {
        "rock",
        "paper",
        "scissors",
        "lizard" ,
        "spock"
    };

    Bitmap[] bitmaps =
    {
         Properties.Resources.rock,
         Properties.Resources.paper,
         Properties.Resources.scissors,
         Properties.Resources.lizard,
         Properties.Resources.spock,
    };
}

      

I've tried adding rock.Tag = properties.resources.rock

.

I've tried it names[0].tag = properties.resources.rock

.

I have also tried properties.resources.rock.Tag

.

The professor hasn't shown us how to use the tag property yet, so I'm sure I was just missing something obvious. I am new to coding and any help is appreciated.

My complete code is here, although very incomplete.

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 Lab5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] names =
            {
                "rock",
                "paper",
                "scissors",
                "lizard" ,
                "spock"
            };

            Bitmap[] bitmaps =
            {
                Properties.Resources.rock,
                Properties.Resources.paper,
                Properties.Resources.scissors,
                Properties.Resources.lizard,
                Properties.Resources.spock,
            };

            // Following array will not actually be used
            // PictureBox[] pics = new PictureBox[bitmaps.Length];

            for (int i = 0; i < bitmaps.Length; i++)
            {
                PictureBox pic = new PictureBox();
                pic.Image = bitmaps[i];
                pic.Location = new Point(20 + (i * 100), 20);
                pic.SizeMode = PictureBoxSizeMode.AutoSize;
                Controls.Add(pic);
                pic.Click += clickHandler;
            }
        }

        private void displayImages()
        {
            // Move code from form1_load to here 
        }

        //click handler for every picture
        private void clickHandler(object sender, EventArgs e)
        {
            MessageBox.Show("You clicked a picture box");
        }

        private void playAgainButton_Click(object sender, EventArgs e)
        {
            // call display images here
        }
    }
}

      

+3


source to share


1 answer


All winforms controls, including PictureBox

, have a property Tag

that can be set on any object. Presumably your professor wants you to use this to associate images with their respective names.

Add this line to the loop for

where you initialize PictureBoxes

:

    pic.Tag = names[i];

      



Then, in your click handler, you can specify the name of the image that was clicked like this:

private void clickHandler(object sender, EventArgs e)
{
    PictureBox pic = (PictureBox)sender;      // get the control that was clicked on
    string name = (string)pic.Tag;            // retrieve the name from the Tag property
    MessageBox.Show("You clicked " + name);
}

      

+1


source







All Articles