Click here to Skip to main content
15,896,338 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 1-Sep-11 19:39pm
v2
Comments
valza 2-Sep-11 1:55am    
for example, you have windows form. on form is x count buttons. You click on random button(with or without picture) and after clicking pictures randomized changes?

This[^] might help you out.

You will need to write some code to use[^] Random() and populate random images.
 
Share this answer
 
C#
 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>


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
 
Share this answer
 
Comments
lethabo11 2-Sep-11 14:41pm    
can you please try adding comments to your code, i am failing to fully understand it. Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900