Dynamically create picture boxes with multiple images in Windows Forms

Manie Verster

Member
Joined
Nov 22, 2023
Messages
17
Programming Experience
10+
Hi there,

I want to create picture boxes programmatically from a data table and then to each of these picture boxes add multiple images from a data table in my code in C#. I got it this far but now using the time ticker I want to loop these images like a slide show. Here is my code so far.

C#:
private PictureBox picBox;
private Timer imageTimer;

public DataTable dtCats = new DataTable();
private DataTable dtPhots = new DataTable();

public List<string> myList = new List<string>();

private int currentIndex = 0;
public bool isClicked = false;

public void DoPics()
{
    int startX = 3;
    int startY = 34;
    int labelX = 3;
    int labelY = 15;
    try
    {
        this.dtCats = DataHelper.RecipeCategories();
        imageTimer.Enabled = true;
        imageTimer.Interval = 1000;
        if (dtCats.Rows.Count > 0)
        {
            int countMe = 0;
            foreach (DataRow row in dtCats.Rows)
            {
                countMe += 1;
                if (countMe == 1) { startX = 3; startY = 34; labelX = 3; labelY = 15; }
                string name = row["RecipeTypeName"].ToString();
                System.Windows.Forms.Label lbl = new System.Windows.Forms.Label
                {
                    Text = name,
                    Location = new System.Drawing.Point(labelX, labelY)
                };
                picBox = new PictureBox
                {
                    Name = name,
                    Location = new System.Drawing.Point(startX, startY),
                    Height = 100,
                    Width = 100,
                    BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
                };
                string category = row["RecipeTypeID"].ToString();
                this.dtPhots = DataHelper.RecipePhotos(category);
                if (dtPhots.Rows.Count > 0)
                {
                    foreach (DataRow ro in dtPhots.Rows)
                    {
                        string photos = ro["RecipePhoto"].ToString();
                        myList.Add(photos);
                        picBox.Image = System.Drawing.Image.FromFile(photos);
                        picBox.SizeMode = PictureBoxSizeMode.Zoom;
                        panelCats.Controls.Add(picBox);
                        picBox.Click += picBox_Click;
                    }
                }
                else
                {
                    picBox.Image = System.Drawing.Image.FromFile("C:\\Projects\\RecipeManager\\RecipeImages\\Recipe Planner.jpg");
                    panelCats.Controls.Add(picBox);
                    picBox.Click += picBox_Click;
                    picBox.SizeMode = PictureBoxSizeMode.Zoom;
                }
                myList.Clear();
                panelCats.Controls.Add(lbl);

                startX += 106;
                labelX += 106;
                if (countMe % 5 == 0) { startY += 126; startX = 3; labelY += 126; labelX = 7; }
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message + ex.StackTrace, "Error Categories", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

private void ShowNextImage()
{
    panelCats.Controls[currentIndex].Visible = false;

    currentIndex++;

    if (currentIndex >= Controls.Count)
    {
        currentIndex = 0;
    }

    panelCats.Controls[currentIndex].Visible = true;
}

private void ImageTimer_Tick(object sender, EventArgs e)
{
    //DoPicsTimer();
    ShowNextImage();
}

The DoPics function creates the controls and it works fine but the timer event obviously does not. Any help will be appreciated.
 
Set a breakpoint on line 97. Is your event handler even being called?
 
And if it is being called, how do you know that your current index maps to a picture box, and not some other control that is your panelCats?
 
And that is why asked my two questions.

The first question about whether your event handler is being called or not is because in the code you presented, you show the timer interval being set and enabled, but your do not show the timer's event handler being set. Since on lines 1-10 you showed some class variables being declared, and those variables look to be something you specifically wrote rather than the WinForms Design generated for you, the probability of you having set the event handler via the Designer goes down dramatically.

C# is not like VB6 or VB.NET which will do automatic wiring of events based on naming convention and duck-typing. You have to explicitly connect the event to the control either through your code; or through the Designer UI which will generate the code to wire up the event.

The second question I asked was because you throw both the picture boxes and the labels into your panelCats panel, but your code to show the next image indexes through all thr controls in thr panel, not just the picture boxes.
 
Last edited:
And that is why asked my two questions.

The first question about whether your event handler is being called or not is because in the code you presented, you show the timer interval being set and enabled, but your do not show the timer's event handler being set. Since on lines 1-10 you showed some class variables being declared, and those variables look to be something you specifically wrote rather than the WinForms Design generated for you, the probability of you having set the event handler via the Designer goes down dramatically.

C# is not like VB6 or VB.NET which will do automatic wiring of events based on naming convention and duck-typing. You have to explicitly connect the event to the control either through your code; or through the Designer UI which will generate the code to wire up the event.

The second question I asked was because you throw both the picture boxes and the labels into your panelCats panel, but your code to show the next image indexes through all thr controls in thr panel, not just the picture boxes.

I have decided that there are just too many things about this code that just are not working out for me and I have a lot to learn before I can think of writing code like this. I want to withdraw this question please.
 

Latest posts

Back
Top Bottom