0

I'm attempting to make a program in the old Windows Forms with c#. I have managed to make a PictureBox and when the program starts with a randomized couple of images from resources. My problem is that I have 4 buttons I want to change label text on depending on what image that is on the screen , but without success.

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

            List<Image> images = new List<Image>();

            images.Add(Properties.Resources.Abessinier);
            images.Add(Properties.Resources.Bengal);
            images.Add(Properties.Resources.American_curl);
            images.Add(Properties.Resources.Balines);
            images.Add(Properties.Resources.brittisk_korthår);

            Random random = new Random();
            pictureBox1.Image = images[random.Next(0, images.Count - 1)];

            if (pictureBox1.Image == Properties.Resources.Abessinier )
            {
                button1.Text = "some text";
                button2.Text = "some text";
                button3.Text = "some text";
                button4.Text = "some text";
            }

            if (pictureBox1.Image == Properties.Resources.Bengal)
            {
                button1.Text = "some other text";
                button2.Text = "some other text";
                button3.Text = "some other text";
                button4.Text = "some other text";
            }


        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {


        }
    }
}
John Mitchell
  • 9,653
  • 9
  • 57
  • 91
Anguiz
  • 7
  • 4
  • I guess the identities the like of `picturebox1.Image == Properties.Resources.Bengal` won't hold - as soon as you assign an image, it will live in the picturebox, have got its own handle and likely is no longer equal to the resource it was loaded from. Better store the result of the randomization in an integer variable, and use that to assign the text (you know that 0 will be Abessinier, 1 will be Bengal and so on) – Cee McSharpface Dec 29 '17 at 20:24
  • Hmm it worked thank you very much – Anguiz Dec 29 '17 at 20:51
  • Note that you will never select the last item - the second argument to the overload of the `Next()` that you're using (which specifies the `maxValue`) is an *exclusive* upper bound. Also, the default lower bound is already `0`, so you might consider doing this instead: `images[random.Next(images.Count)];` – Rufus L Dec 29 '17 at 21:05

2 Answers2

1

Why not make a function to do both things at the same time, also you probably want to do the comparison on the index selector instead of the actual resource itself.

private void SetImage(PictureBox target)
{
     List<Image> images = new List<Image>();

     images.Add(Properties.Resources.Abessinier);
     images.Add(Properties.Resources.Bengal);
     images.Add(Properties.Resources.American_curl);
     images.Add(Properties.Resources.Balines);
     images.Add(Properties.Resources.brittisk_korthår);

     Random random = new Random();
     var selectedIndex = random.Random(0, images.Count - 1);
     target.Image = images[selectedIndex];

     switch(selectedIndex)
     {
         case 0:
               button1.Text = "some text";
               button2.Text = "some text";
               button3.Text = "some text";
               button4.Text = "some text"; 
               break; 
         case 1:
               button1.Text = "some other text";
               button2.Text = "some other text";
               button3.Text = "some other text";
               button4.Text = "some other text"; 
               break;
               // Keep going with additional statements, and include a default too... 
     }
}

As a rough idea, I'd suggest cleaning the code up though, making the function a new class, making the class load the images and text from a dictionary or something.

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77
John Mitchell
  • 9,653
  • 9
  • 57
  • 91
1

One idea might be to couple the text with the image in a Tuple, or a custom class:

class ImageWithText
{
    public Image Image { get; set; }
    public string Text { get; set; }

    public ImageWithText(Image image, string text)
    {
        Image = image;
        Text = text;
    }
}

Then, when you populate your list, you can add both items together:

var images = new List<ImageWithText>();

images.Add(new ImageWithText(Properties.Resources.Abessinier, "Abessinier description"));
images.Add(new ImageWithText(Properties.Resources.Bengal, "Bengal description"));
images.Add(new ImageWithText(Properties.Resources.American_curl, 
    "American_curl description"));
images.Add(new ImageWithText(Properties.Resources.Balines, "Balines description"));
images.Add(new ImageWithText(Properties.Resources.brittisk_korthår, 
    "brittisk_korthår description"));

And finally, when you select your random object from the list, you can assign your properties:

ImageWithText randomItem = images[random.Next(images.Count)];

pictureBox1.Image = randomItem.Image;
button1.Text = randomItem.Text;
button2.Text = randomItem.Text;
button3.Text = randomItem.Text;
button4.Text = randomItem.Text;
Rufus L
  • 36,127
  • 5
  • 30
  • 43