我试图比较老虎机的3张图片,问题是我没有收到任何错误,但图片似乎没有得到任何比较。我有图像列表中的图像,它们是随机选择的,但当imagebox1是樱桃时,bettextbox不会= Winner
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap apple = Properties.Resources.Apple;
Bitmap cherries = Properties.Resources.Cherries;
Bitmap orange = Properties.Resources.Orange;
private void spinButton_Click(object sender, EventArgs e)
{
Random rand = new Random();
int pic = rand.Next(0, images.Images.Count);
int pic2 = rand.Next(0, images.Images.Count);
int pic3 = rand.Next(0, images.Images.Count);
pictureBox1.Image = images.Images[(pic)];
pictureBox2.Image = images.Images[(pic2)];
pictureBox3.Image = images.Images[(pic3)];
if (pictureBox1.Image == cherries)
{
betTextBox.Text = "Winner";
}
答案 0 :(得分:0)
我敢打赌,ImageList控件中的Images数组不是对原始对象的引用,因此与==相比不会起作用(虽然图像本身是相同的,但是保持图像的对象不是同样的参考)。
您可以使用数组索引本身作为if表达式。
int cherriesIndex=1;
private void spinButton_Click(object sender, EventArgs e)
{
Random rand = new Random();
int pic = rand.Next(0, images.Images.Count);
int pic2 = rand.Next(0, images.Images.Count);
int pic3 = rand.Next(0, images.Images.Count);
pictureBox1.Image = images.Images[(pic)];
pictureBox2.Image = images.Images[(pic2)];
pictureBox3.Image = images.Images[(pic3)];
if (pic == cherriesIndex)
{
betTextBox.Text = "Winner";
}`
这假设樱桃是图像中的元素1。我的意思是主要用于说明。