locked
How do i assign one image to all null or not in use pictureboxes ? RRS feed

  • Question

  • I create in my program in the constructor 8 pictureBoxes.

    Then i assign to the pictureBoxes images and animate them in each pictureBox different images in animation/loop.

    But since the array of images have only 5 index only 5 pictureBoxes are showing animatied images.

    The other 3 pictureBoxes are not empty or null but do not display any images and i want to make that this 3 pictureBox are like not in use mode and that they will show/display still image. And if the arrya of images List was only with 7 index then only one pictureBox will show the still image. 

    This is in Form1 bottom how i get the files:

    private void getfiles()
            {
                List<FileInfo> fileList = new List<FileInfo>();
                for (int i = 0; i < BackgroundWorkerConfiguration.urlsDirectories.Count; i++)
                {
                    System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(BackgroundWorkerConfiguration.urlsDirectories[i]);
                    fileList.AddRange(di.GetFiles("*.*", System.IO.SearchOption.AllDirectories)
                                        .Where(x => x.Length > 0).Select(y => y));
                }
                 file_array = fileList.OrderBy(x => x.CreationTime)
                                    .GroupBy(x => x.DirectoryName)
                                    .Select(g => g.Select(x => x.FullName).ToList())
                                    .ToArray();
                timer1.Enabled = true;
            }


    Then i have a class that make the images to animate/loop in the pictureBoxes:

    using System;
    using System.Windows.Forms;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace WeatherMaps
    {
        class AnimatedPictureBox
        {
            //Use this custom PictureBox for convenience
            public class AnimatedPictureBoxs : PictureBox
            {
                List<string> imageFilenames;
                Timer t = new Timer();
                public AnimatedPictureBoxs()
                {
                    AnimateRate = 100; //It's up to you, the smaller, the faster.
                    t.Tick += Tick_Animate;
                }
                public int AnimateRate
                {
                    get { return t.Interval; }
                    set { t.Interval = value; }
                }
                public void Animate(List<string> imageFilenames)
                {
                    this.imageFilenames = imageFilenames;
                    t.Start();
                }
                public void StopAnimate()
                {
                    t.Stop();
                    i = 0;
                }
                int i;
                private void Tick_Animate(object sender, EventArgs e)
                {
                    if (imageFilenames == null) return;
                    Load(imageFilenames[i]);
                    i = (i + 1) % imageFilenames.Count;
                }
            }
        }
    }
    


    In form1 i have a button click that display the images in the pictureBoxes:

    private void sat24ToolStripMenuItem1_Click(object sender, EventArgs e)
            {
                if (file_array.Length > 0)
                {
                    getfiles();
                    for (int i = 0; i < file_array.Length; i++)
                    {
                        if (pbs[i].Image == null)
                        {
                            pbs[i].Image = Properties.Resources.Weather_Michmoret;
                        }
                        else
                        {
                            pbs[i].Animate(file_array[i]);
                        }
                    }
                }
            }

    Now in this case file_array contain 5 indexs in each index there are some files.

    pbs is the pictureBoxes array i create in the constructor.

    Since file_array have only 5 indexs only 5 pictureBoxes(pbs) will display the images in each pictureBox it will show another index of files from file_array.

    pbs[0] will display the files in file_array[0] and so on.

    So there are 3 pictureBoxes(pbs) that are left empty but they are not realy empty or null its just there is nothing to display there.

    I tried to do this in the button click event:

    if (file_array.Length > 0) {

    getfiles(); for (int i = 0; i < file_array.Length; i++) { if (pbs[i].Image == null) { pbs[i].Image = Properties.Resources.Weather_Michmoret; } else { pbs[i].Animate(file_array[i]); } } }


    But then it assign the Weather_Michmoret images to all the pictureBoxes(pbs).

    And i wanted it to display it only in the 3 pictureBoxes that are not in use.

    I also did in Form1 a global mouse enter event for all the pictureBoxes(pbs):

    public void globalPbsMouseEnterEvent(object sender, System.EventArgs e)
            {
                PictureBox p = sender as PictureBox;
                    pb.SizeMode = PictureBoxSizeMode.StretchImage;
                    if (file_array.Length > 0)
                    {
                        for (int i = 0; i < file_array.Length; i++)
                        {
                            if(p == pbs[i])  // Add this line.
                               pb.Animate(file_array[i]);
                        }
                    }
                    pb.Visible = true;
                    pb.BringToFront();
                    leave = true;
                
            }

    And again if the mouse is enter the 3 pictureBoxes that are empty not displaying anything its showing in the big pictureBox(pb) the images in loop like there is something in this 3 pictureBoxes.

    I thought that if file_array have only 5 indexs then the left 3 pictureBoxes are empty but they are not null or empty just not showing anything.

    And i want to assign this 3 pictureBoxes the still image to show that this pictureBoxes are not in use now.

    • Moved by CoolDadTx Monday, November 11, 2013 3:12 PM Winforms related
    Monday, November 11, 2013 2:27 PM

Answers

  • Ok i solved it on my own with this code:

    private void sat24ToolStripMenuItem1_Click(object sender, EventArgs e)
            {
                animateMaps();
                if (file_array.Length > 0)
                {
                    for (int i = 0; i < file_array.Length; i++)
                    {
                        pbs[i].Animate(file_array[i]);
                    }
                    for (int x = 0; x < pbs.Length; x++)
                    {
                        if (x >= file_array.Length && pbs[x].Image == null)
                        {
                            pbs[x].Image = Properties.Resources.Weather_Michmoret;
                        }
                    }
                }
            }

    Tuesday, November 12, 2013 1:43 AM

All replies

  • This way its working almost 100% as i wanted:

    private void sat24ToolStripMenuItem1_Click(object sender, EventArgs e)
            {
                animateMaps();
                if (file_array.Length > 0)
                {
                    for (int x = 0; x < pbs.Length; x++)
                    {
                        if (pbs[x].Image == null)
                        {                        
                                pbs[x].Image = Properties.Resources.Weather_Michmoret;                        
                        }
                    }
                    for (int i = 0; i < file_array.Length; i++)
                    {
                        pbs[i].Animate(file_array[i]);
                    }
                }
            }

    The problem is that for the first 1-2 seconds all the pictureBoxes(pbs) are having the image Weather_Michmoret and then the 5 pictureBoxes atart the animated images and the other 3 pictureBoxes stay with the Weather_Michmoret image.

    But why in the first 1-2 seconds all the pictureBoxes are with Weather_Michmoret ?

    Monday, November 11, 2013 4:31 PM
  • Ok i solved it on my own with this code:

    private void sat24ToolStripMenuItem1_Click(object sender, EventArgs e)
            {
                animateMaps();
                if (file_array.Length > 0)
                {
                    for (int i = 0; i < file_array.Length; i++)
                    {
                        pbs[i].Animate(file_array[i]);
                    }
                    for (int x = 0; x < pbs.Length; x++)
                    {
                        if (x >= file_array.Length && pbs[x].Image == null)
                        {
                            pbs[x].Image = Properties.Resources.Weather_Michmoret;
                        }
                    }
                }
            }

    Tuesday, November 12, 2013 1:43 AM
  • Hi,

    I'm glad to hear that you have solved your issue. Thanks for sharing your experience here. It's very beneficial for other community members who have similar problem to see how you solved your question.

    Friday, November 15, 2013 1:09 AM