Anguiz
Anguiz

Reputation: 7

make button label change depending on image in picturebox

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)
        {


        }
    }
}

Upvotes: 0

Views: 238

Answers (2)

Rufus L
Rufus L

Reputation: 37020

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;

Upvotes: 1

nyxthulhu
nyxthulhu

Reputation: 9752

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.

Upvotes: 1

Related Questions