我正在尝试使用c#在旧Windows Forms
中创建一个程序。我设法制作了一个PictureBox
,当程序以resources
的随机图像开始时。我的问题是我有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)
{
}
}
}
答案 0 :(得分:1)
为什么不让函数同时执行这两个操作,也可能想要在索引选择器而不是实际资源本身上进行比较。
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...
}
}
作为一个粗略的想法,我建议清理代码,使函数成为一个新类,使类从字典或其他东西加载图像和文本。
答案 1 :(得分:1)
一个想法可能是将文本与Map<String, Integer> sumPerClass = new HashMap<>();
for(Class cl : classes){
int numNodes = 0;
for(Method m : cl.getMethod){
numNodes += getNodeCount(m);
}
System.out.print("Class = " + cl.getName() + ", Sum = " + numNodes);
sumPerClass.put(cl.getName(), numNodes)
}
或自定义类中的图像耦合:
Tuple
然后,当您填充列表时,可以将两个项目一起添加:
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"));