使用数组将包含图片的按钮存储在Windows应用程序中 [英] using arrays to store buttons that contain pictures in windows applications

查看:37
本文介绍了使用数组将包含图片的按钮存储在Windows应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个数组来存储包含图片的按钮,并且在Windows窗体应用程序中单击图片时,图片应随机显示在任何按钮上.

i am trying to create an array to store buttons that have pictures and the pictures should be randomised to appear at any button when it is clicked in a windows forms application.

推荐答案

[使用 [
This[^] might help you out.

You will need to write some code to use[^] Random() and populate random images.


 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        FileInfo[] fileInfo;
        List<image> imgList;

        private void Form1_Load(object sender, EventArgs e)
        {
            DirectoryInfo drInfo = new DirectoryInfo("Images");
            fileInfo = drInfo.GetFiles("*.jpg");
            imgList = new List<image>();

            foreach (var item in fileInfo)
            {
                imgList.Add(Image.FromFile(item.FullName));
            }

        }
        Random rnd = new Random();
        private Image RandomPicButton()
        {
            int rndNumber = rnd.Next(imgList.Count);
            return imgList[rndNumber];
        }

        private void randomCLick(object sender, EventArgs e)
        {
            button9.Image = RandomPicButton();
            button10.Image = RandomPicButton();
            button13.Image = RandomPicButton();
            button14.Image = RandomPicButton();
        }
    }
</image></image>



这项工作.所有按钮都可拥有一个事件.
如果将Random放入RandomPicButton()会返回相同的图片,则很有趣.
我正在学习C#,因此欢迎对我的代码进行注释



This Work. One Event to All buttons.
It is interesting if Random put inside RandomPicButton() it return the same picture.
I learning C#, so comments about my code are welcomed


这篇关于使用数组将包含图片的按钮存储在Windows应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆