I am programming with C# in Visual Studio 2005.
I am trying to create a form that will display a single image from a set of 5 pictures. Then use previous and next buttons to navigate forwards and backwards through that set of pictures displaying each one. I have created a 2nd form which when loads up displays the first picture using the code :-
pictureBox1.Image = Image.FromFile("C:\\Documents and Settings\\My Documents\\pics\\pic1.gif");
I know that I need to declare and initialise an array to hold all the images in and then use a for loop to display them but I am having trouble working out how to do this as I am not an experienced programmer. If anyone could help me out with this it would be much appreciated!

Navigating through pictures on a form?
Dario Aznar
just to add, if you are using the example then for using the forward and back button, simply increase the listbox selected index, or decrease it but making sure that you do not go over the margins. So make sure that the selectedindex does not go less than 0 and more than the items in the listbox (listbox.Count gives you the number of items in the listbox)
if you were still using an array then you need to hold the file path of the images in the array, then when you click the forward/back button - the solution given in the above paragraph also applies, except you are checking the length of this array. You will also need to store the current position you are on in the array so you can then increase it and get the next value. Example:
ArrayList theList = new ArrayList(); //global
int currentPosition = -1; //global
//somewhere you add the path and image filename into the array
//then when time comes to go forward or back, just add 1 or remove 1 from currentPosition and get the item in the array. Example:
if (currentPosition < theList.Count)
{
currentPosition++;
this.thePictureBox.Image = Image.FromFile(this.theList[currentPosition].ToString());
}
for the sake of simplicity, I would suggest using and implementing the solution in the link
Fulankazu
thats ok.
Well you have initialized the arrays already!
int[] nums = new int[5];
however, its the wrong type. You need to make it into a string array, not an int array since you will be storing the path/filenames correct if so....change "int" to "string":
string[] nums = new string[5];
now to add the filenames/paths into the array:
for(int i = 0; i <nums.Length; i++){
nums[ i ] = "path\filename.jpg"; //remove the spaces between the square brackets, its just for this reply I have seperated them as it shows an emoticon instead
}
and thats it. As for the last question, yes, thats correct. Again, i would advise to use the initial response as its easier to understand/follow and less errors perhaps :-)
RhysDavies
take a look at this:
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=858421&SiteID=1
you can modify it almost easily to your needs, and add the back/forward button to navigate through the listbox
Chort
take a look back at my initial code :-) you need to check to see if the current position is equal to the last index in the array or is equal to 0. you've kind of got it the other way around....
private void button2_Click(object sender, EventArgs e)
{
if (this.currentPosition <= this.Files.Length - 1 )
this.currentPosition++;
this.pictureBox1.Image = Image.FromFile(this.Files[this.currentPosition]);
}
private void button1_Click(object sender, EventArgs e)
{
if (this.currentPosition >= this.Files.Length - 1)
this.currentPosition--;
this.pictureBox1.Image = Image.FromFile(this.Files[this.currentPosition]);
}
clint 2
well where are you obtaining the files from
secondly you didn't quite follow my example :-)
public partial class Form2 : Form
{
string[] nums = new string[5];private int currentPosition = -1;
...
...
private void button2_Click(object sender, EventArgs e)
{
if (this.currentPosition < this.nums.Length - 1)
{
this.currentPosition++;
this.thePictureBox.Image = Image.FromFile(this.nums[this.currentPosition]);
}
}
Reid Westburg
Hi I am trying to put this code together below and come across a few probs. In the for loop how should I add all the different path names/file names so that the array stores each one I have done the first one but unsure how to put any more in.
Also when I add nums++ to the next button event handler and compile it sais Operator '++' cannot be applied to operand of type 'string[]'
namespace images
{
public partial class Form2 : Form
{
string[] nums = new string[5]; public Form2(){
InitializeComponent();
for (int i = 0; i < nums.Length; i++){
nums[ i ] =
"C:\\Documents and Settings\\pics\\pic2.gif";}
}
private void Form2_Load(object sender, EventArgs e){
pictureBox1.Image =
Image.FromFile("C:\\Documents and Settings\\pics\\pic1.gif");}
//next button
private void button2_Click(object sender, EventArgs e){
nums++;
}
}
}
rebeccat
Ok thanks for the help Ive managed to do it now
Just a couple of things, when I click through my next and previous buttons if I get to the last pic then click next I get an error that comes saying I was out of the array range same with previos.How can I change this
Also could you explain to me exactly what the click button code does term by term just so I am clear what each word in it means. One bit of it I dont understand is the -1 bit after Length
Here is my completed code:-
namespace
images{
public partial class Form2 : Form{
string[] Files = System.IO.Directory.GetFiles("C:\\Documents and Settings\\My Documents\\pics", "*.gif"); private int currentPosition = 0; public Form2(){
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e){
pictureBox1.Image =
Image.FromFile("C:\\Documents and Settings\\Matt\\My Documents\\pics\\pic1.gif");}
private void button2_Click(object sender, EventArgs e){
if (this.currentPosition <= this.Files.Length - 1 ) this.currentPosition++; this.pictureBox1.Image = Image.FromFile(this.Files[this.currentPosition]);}
private void button1_Click(object sender, EventArgs e){
if (this.currentPosition <= this.Files.Length - 1) this.currentPosition--; this.pictureBox1.Image = Image.FromFile(this.Files[this.currentPosition]);}
}
}
R1ZWAN
well you need to populate the array with those image paths. Example:
string[] theFiles = System.IO.Directory.GetFiles(path, "*.gif"); //place this globally
private int theCurrentPosition = -1; //globally also
private void button2_Click(object sender, EventArgs e)
{
if (this.theCurrentPosition < this.theFiles.Length - 1)
}
this.theCurrentPosition++; this.thePictureBox.Image = Image.FromFile(this.theFiles[this.theCurrentPosition]);}
and thats it now.
the Files[] will contain the image paths and this is all you need now, you don't need to populate another array with the paths/filenames etc... as this is done for you using the Directory.GetFiles, you specify the path to look into and the filter, gif files in this case, and should retrieve those files and store the names/path of those files in the string[] array, then we simply navigate through them using the button(s)
does this help
twoshipps
OR it if you just want files that start with "pic". This assumes the files have some naming convention.
for (int i = 0; i < nums.Length; i++)
{
nums[ i ] = "C:\\Documents and Settings\\pics\\pic" + i.ToString() + ".gif";
}
David N.4117
Hi thanks for the suggestions. Unfortunatly Im finding it hard to understand how to implement it the way I need to. I definitly need to use an array and also a for loop.
To declare the array I use the code -
int[]nums = new int[5];
as their are 5 pictures I want to display. I am not sure how to initialise this array. Would I use a for loop here like this code -
for(int i = 0; i <nums.Length; i++){
//body of loop but unsure how to implement
}
Would I have to use string concatanation in any part of the code
Finally for the Previous and Next buttons when I double click should I enter nums++ and nums--
Sorry for asking obvious questions btw.
Aaron Sulwer
Hi thanks for the replies btw I do appreciate it! You asked me where Im obtaining my files from well there are 5 pictures stored in on folder which is C:\\Documents and Settings\\My Documents\\pics; the pics inside are pic1.gif,pic2.gif,pic3.gif,pic4.gif and pic5.gif.
I am unsure of what to do for the rest of the for loop
I have noticed an error as well about the this.currentPosition++; part of the code it sais invalid token 'this' in class,struct or interface member declaration.
The code I have done so far is
namespace
images{
public partial class Form2 : Form{
string[] nums = new string[5]; private int currentPosition = -1;
public Form2(){
InitializeComponent();
for (int i = 0; i < nums.Length; i++){
nums
=
"C:\\Documents and Settings\\My Documents\\pics\\pic2.gif";}
}
private void Form2_Load(object sender, EventArgs e){
pictureBox1.Image =
Image.FromFile("C:\\Documents and Settings\\Matt\\My Documents\\Computer Games Programming\\Year 2\\Windows GUI Programming\\imagesPrac\\pics\\hand1.gif");}
private void button2_Click(object sender, EventArgs e){
if (this.currentPosition < this.nums.Length - 1)}
this.currentPosition++; this.thePictureBox.Image = Image.FromFile(this.nums[this.currentPosition]);}
}