I am creating a "rock paper scissors lizard spock" game for school. I am stuck on the part of the assignment where we need to tag the bitmap images with the corresponding names using the tag property. I have created an array of names, and an array of bitmap images.
I am unsure of how to use the tag property to do this. The exact instructions are:
Create an array of string objects, and initialize it to contain the string values "rock", "paper", "scissors", "lizard", "spock" Add code to each of the bitmaps with the corresponding string values. (ex. bitmap "properties.resources.rock" should be tagged 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 names[0].tag = properties.resources.rock.
I've also tried properties.resources.rock.Tag.
The professor hasn't shown us how to use the tag property yet, so I'm sure I'm just missing something obvious. I am new to coding and any help is appreciated.
My full code is here, though it is 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
}
}
}
All winforms controls, including PictureBox, have a Tag property that can be set to any object. Presumably, your professor wants you to use that to link the pictures to their corresponding names.
Add this line to your for loop where you are initializing the PictureBoxes:
pic.Tag = names[i];
Then in your click handler you can show the name of the picture 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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With