W
wannabe geek
I have an form that displays a slideshow of pictures. It uses a
List<PictureFile> to store the slideshow photos, filenames, and options
whether or not a photo should be included in a list to export later. When I
make one or more items of List<PictureFile> included, the GoToNextPicture and
GoToPreviousPicture methods do not work correctly and whenever they attempt
to move to an included picture, they jump around. I suspected something in
List<T>.ElementAt and switched to List<T>[index] but had no luck.
My code: (I trimmed out some irrelevant methods and also lines of code)
//FormMain.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
namespace slideshow
{
public partial class FormMain : Form
{
List<PictureFile> pictures;
PictureFile curFile;
int curFileIndex;
private void GoToNextPicture()
{
if (pictures.Count != 0)
{
curFileIndex += 1;
if (curFileIndex == pictures.Count)
{
curFileIndex = 0;
}
curFile = pictures[curFileIndex];
}
DisplayPicture();
}
private void GoToPreviousPicture()
{
if (pictures.Count != 0)
{
curFileIndex -= 1;
if (curFileIndex == -1)
{
curFileIndex = pictures.Count - 1;
}
curFile = pictures[curFileIndex];
}
DisplayPicture();
}
private void GoToFirstPicture()
{
if (pictures.Count != 0)
{
curFile = pictures[0];
}
DisplayPicture();
}
private void DisplayPicture()
{
if (pictures.Count != 0)
{
pboxDisplay.Image = curFile.Image;
rbInclude.Checked = curFile.Include;
rbInclude.Enabled = true;
rbDontInclude.Checked = !curFile.Include;
rbDontInclude.Enabled = true;
}
else
{
pboxDisplay.Image = null;
rbInclude.Checked = false;
rbInclude.Enabled = false;
rbDontInclude.Checked = false;
rbDontInclude.Enabled = false;
}
if (timer.Enabled)
{
timer.Stop();
timer.Start();
}
}
private void rbInclude_CheckedChanged(object sender, EventArgs e)
{
curFile.Include = rbInclude.Checked;
GoToNextPicture();
}
}
}
//PictureFile.cs
using System.IO;
using System.Drawing;
namespace slideshow
{
class PictureFile
{
FileInfo file;
bool include = false;
Image image;
public PictureFile(string filename, Image image)
{
file = new FileInfo(filename);
this.image = image;
}
public bool Include
{
get
{
return include;
}
set
{
include = value;
}
}
public FileInfo File
{
get
{
return file;
}
set
{
file = value;
}
}
public Image Image
{
get
{
return image;
}
set
{
image = value;
}
}
}
}
Does anyone have an answer to this?
Ask if you have questions.
List<PictureFile> to store the slideshow photos, filenames, and options
whether or not a photo should be included in a list to export later. When I
make one or more items of List<PictureFile> included, the GoToNextPicture and
GoToPreviousPicture methods do not work correctly and whenever they attempt
to move to an included picture, they jump around. I suspected something in
List<T>.ElementAt and switched to List<T>[index] but had no luck.
My code: (I trimmed out some irrelevant methods and also lines of code)
//FormMain.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
namespace slideshow
{
public partial class FormMain : Form
{
List<PictureFile> pictures;
PictureFile curFile;
int curFileIndex;
private void GoToNextPicture()
{
if (pictures.Count != 0)
{
curFileIndex += 1;
if (curFileIndex == pictures.Count)
{
curFileIndex = 0;
}
curFile = pictures[curFileIndex];
}
DisplayPicture();
}
private void GoToPreviousPicture()
{
if (pictures.Count != 0)
{
curFileIndex -= 1;
if (curFileIndex == -1)
{
curFileIndex = pictures.Count - 1;
}
curFile = pictures[curFileIndex];
}
DisplayPicture();
}
private void GoToFirstPicture()
{
if (pictures.Count != 0)
{
curFile = pictures[0];
}
DisplayPicture();
}
private void DisplayPicture()
{
if (pictures.Count != 0)
{
pboxDisplay.Image = curFile.Image;
rbInclude.Checked = curFile.Include;
rbInclude.Enabled = true;
rbDontInclude.Checked = !curFile.Include;
rbDontInclude.Enabled = true;
}
else
{
pboxDisplay.Image = null;
rbInclude.Checked = false;
rbInclude.Enabled = false;
rbDontInclude.Checked = false;
rbDontInclude.Enabled = false;
}
if (timer.Enabled)
{
timer.Stop();
timer.Start();
}
}
private void rbInclude_CheckedChanged(object sender, EventArgs e)
{
curFile.Include = rbInclude.Checked;
GoToNextPicture();
}
}
}
//PictureFile.cs
using System.IO;
using System.Drawing;
namespace slideshow
{
class PictureFile
{
FileInfo file;
bool include = false;
Image image;
public PictureFile(string filename, Image image)
{
file = new FileInfo(filename);
this.image = image;
}
public bool Include
{
get
{
return include;
}
set
{
include = value;
}
}
public FileInfo File
{
get
{
return file;
}
set
{
file = value;
}
}
public Image Image
{
get
{
return image;
}
set
{
image = value;
}
}
}
}
Does anyone have an answer to this?
Ask if you have questions.