Visual Studio Development Bookmark and Share   
 index > Visual C# Express Edition > Random picture in picturebox
 

Random picture in picturebox

Hi there,

I need some help with this.

When I click on a button shall 4pictures randomly appear in 4 pictureboxes.
I need to create a method for this but I have no idea how I'm supposed to do. The pictures that I use is from the resources folder.

I hope someone can help me.

John

john8555  Sunday, October 18, 2009 8:23 AM
Hi,

Here is a sample code. Hope it helps.

        private void button1_Click(object sender, EventArgs e)
        {
            List<string> pictureNames = new List<string>();
            pictureNames.Add("_1000002314_1");
            pictureNames.Add("_1000002314_2");
            pictureNames.Add("_1000002315_1");
            pictureNames.Add("a");

            List<PictureBox> pictureBoxex = new List<PictureBox>();
            pictureBoxex.Add(pictureBox1);
            pictureBoxex.Add(pictureBox2);
            pictureBoxex.Add(pictureBox3);
            pictureBoxex.Add(pictureBox4);

            ResourceManager rm = WindowsFormsApplication1.Properties.Resources.ResourceManager;
            for (int i = 0; i < pictureBoxex.Count; i++)
            {
                
                int randomPictureIndex=new Random().Next(0,pictureNames.Count);
                string randomPictureName = pictureNames[randomPictureIndex];
                pictureNames.Remove(randomPictureName);
                Image img = rm.GetObject(randomPictureName) as Image;
                pictureBoxex[i].Image = img;
            }
        }
Tamer Oz  Sunday, October 18, 2009 9:21 AM
Hi,

Here is a sample code. Hope it helps.

        private void button1_Click(object sender, EventArgs e)
        {
            List<string> pictureNames = new List<string>();
            pictureNames.Add("_1000002314_1");
            pictureNames.Add("_1000002314_2");
            pictureNames.Add("_1000002315_1");
            pictureNames.Add("a");

            List<PictureBox> pictureBoxex = new List<PictureBox>();
            pictureBoxex.Add(pictureBox1);
            pictureBoxex.Add(pictureBox2);
            pictureBoxex.Add(pictureBox3);
            pictureBoxex.Add(pictureBox4);

            ResourceManager rm = WindowsFormsApplication1.Properties.Resources.ResourceManager;
            for (int i = 0; i < pictureBoxex.Count; i++)
            {
                
                int randomPictureIndex=new Random().Next(0,pictureNames.Count);
                string randomPictureName = pictureNames[randomPictureIndex];
                pictureNames.Remove(randomPictureName);
                Image img = rm.GetObject(randomPictureName) as Image;
                pictureBoxex[i].Image = img;
            }
        }
Tamer Oz  Sunday, October 18, 2009 9:21 AM

You can use google to search for other answers

Custom Search

More Threads

• Few Questions.
• Refering to MS Example "How to: Build a C# Application in 60 Seconds"
• C# Book
• mySQL from C#
• command line arguments in Visual C# 2008 Express edition
• The value for column 'Data6' in table 'TableIEbus' is DBNull
• Please explain how to use all the provided information: MSDN/Search/etc....
• Slow running project by background image
• Bad return value on a method.
• How to open DRM (Digital Right Managment) wmv in C#?