Manie Verster
Member
- Joined
- Nov 22, 2023
- Messages
- 22
- 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.
The DoPics function creates the controls and it works fine but the timer event obviously does not. Any help will be appreciated.
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.