C# 根据picturebox中的图像更改按钮标签
我正试图用c#在旧的C# 根据picturebox中的图像更改按钮标签,c#,winforms,picturebox,C#,Winforms,Picturebox,我正试图用c#在旧的Windows窗体中制作一个程序。我已经设法制作了一个图片盒,当程序开始时,我会从资源中随机生成两幅图像。我的问题是,我有4个按钮,我想根据屏幕上的图像更改标签文本,但没有成功 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.T
Windows窗体中制作一个程序。我已经设法制作了一个图片盒
,当程序开始时,我会从资源
中随机生成两幅图像。我的问题是,我有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)
{
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间凯特
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
列表图像=新列表();
添加(Properties.Resources.Abessinier);
image.Add(Properties.Resources.Bengal);
Add(Properties.Resources.American_curl);
添加(Properties.Resources.baline);
添加(Properties.Resources.brittisk_korthår);
随机=新随机();
pictureBox1.Image=images[random.Next(0,images.Count-1)];
if(pictureBox1.Image==Properties.Resources.Abessinier)
{
button1.Text=“一些文本”;
按钮2.Text=“一些文本”;
按钮3.Text=“一些文本”;
按钮4.Text=“一些文本”;
}
if(pictureBox1.Image==Properties.Resources.Bengal)
{
button1.Text=“其他一些文本”;
button2.Text=“其他一些文本”;
按钮3.Text=“其他一些文本”;
按钮4.Text=“其他一些文本”;
}
}
私有void Form1\u加载(对象发送方、事件参数e)
{
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
}
}
}
为什么不创建一个函数来同时完成这两件事呢?另外,您可能希望在索引选择器上进行比较,而不是实际的资源本身
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...
}
}
private void SetImage(PictureBox目标)
{
列表图像=新列表();
添加(Properties.Resources.Abessinier);
image.Add(Properties.Resources.Bengal);
Add(Properties.Resources.American_curl);
添加(Properties.Resources.baline);
添加(Properties.Resources.brittisk_korthår);
随机=新随机();
var selectedIndex=random.random(0,images.Count-1);
target.Image=images[selectedIndex];
开关(已选择索引)
{
案例0:
button1.Text=“一些文本”;
按钮2.Text=“一些文本”;
按钮3.Text=“一些文本”;
按钮4.Text=“一些文本”;
打破
案例1:
button1.Text=“其他一些文本”;
button2.Text=“其他一些文本”;
按钮3.Text=“其他一些文本”;
按钮4.Text=“其他一些文本”;
打破
//继续使用其他语句,并包括默认值。。。
}
}
作为一个粗略的想法,我建议清理代码,使函数成为一个新的类,使类从字典或其他地方加载图像和文本 为什么不创建一个函数来同时完成这两件事呢?另外,您可能希望在索引选择器上进行比较,而不是实际的资源本身
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...
}
}
private void SetImage(PictureBox目标)
{
列表图像=新列表();
添加(Properties.Resources.Abessinier);
image.Add(Properties.Resources.Bengal);
Add(Properties.Resources.American_curl);
添加(Properties.Resources.baline);
添加(Properties.Resources.brittisk_korthår);
随机=新随机();
var selectedIndex=random.random(0,images.Count-1);
target.Image=images[selectedIndex];
开关(已选择索引)
{
案例0:
button1.Text=“一些文本”;
按钮2.Text=“一些文本”;
按钮3.Text=“一些文本”;
按钮4.Text=“一些文本”;
打破
案例1:
button1.Text=“其他一些文本”;
button2.Text=“其他一些文本”;
按钮3.Text=“其他一些文本”;
按钮4.Text=“其他一些文本”;
打破
//继续使用其他语句,并包括默认值。。。
}
}
作为一个粗略的想法,我建议清理代码,使函数成为一个新的类,使类从字典或其他地方加载图像和文本 一个想法可能是将文本与图像耦合在元组或自定义类中:
class ImageWithText
{
public Image Image { get; set; }
public string Text { get; set; }
public ImageWithText(Image image, string text)
{
Image = image;
Text = text;
}
}
然后,在填充列表时,可以将两个项目添加到一起:
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"));
一个想法可能是将文本与图像耦合在元组或自定义类中:
class ImageWithText
{
public Image Image { get; set; }
public string Text { get; set; }
public ImageWithText(Image image, string text)
{
Image = image;
Text = text;
}
}
然后,在填充列表时,可以将两个项目添加到一起:
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"));
我猜像picturebox1.Image==Properties.Resources.Bengal
这样的标识将不起作用-只要您分配了一个图像,它就会存在于picturebox中,有自己的句柄,并且可能不再等于它从中加载的资源。最好将随机化的结果存储在一个整数变量中,并使用它来分配文本(您知道0将是一个更大的变量,1将是孟加拉语等等)嗯,它工作了,非常感谢您。请注意,您永远不会选择最后一项—您正在使用的Next()
重载的第二个参数(指定<代码>最大值< /代码>)是一个排他性的上界。默认的下界已经是“代码>0”/代码>,所以你可以考虑这样做:<代码>图像[随机。下一步(图像。计数)];< /Cord> >我猜“代码>图片盒的身份。