根据图片框中的图像更改按钮标签

时间:2022-04-24 00:24:38

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.

我正在尝试使用c#在旧的Windows窗体中创建一个程序。我已经设法制作了一个PictureBox,当程序以一组来自资源的随机图像开始时。我的问题是我有4个按钮,我想根据屏幕上的图像更改标签文本,但没有成功。

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


        }
    }
}

2 个解决方案

#1


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.

作为一个粗略的想法,我建议清理代码,使函数成为一个新类,使类从字典或其他东西加载图像和文本。

#2


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;

#1


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.

作为一个粗略的想法,我建议清理代码,使函数成为一个新类,使类从字典或其他东西加载图像和文本。

#2


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;