Solved

how can i make a previous button here?

Posted on 2016-08-29
4
44 Views
Last Modified: 2016-09-16
So i am making a game in visual c#...the first stage is looking through the photos that are shown in a picturebox. I have written this code so far:

namespace FaceNameGame
{
    public partial class LevelOnePartOne : Form
    {
       
        int m , piccount=0, k;
        Random rd = new Random();
        public string[] picturenames= new string[4];
        public string[] links = new string[3];
        string sourcePath = @"C:\Users\Marina\Desktop\FaceNameGame\images";
        string targetPath = @"C:\Users\Marina\Desktop\FaceNameGame\images\used";
        bool directoryexists = false;
        string[] files = Directory.GetFiles(@"C:\Users\Marina\Desktop\FaceNameGame\images\", "*.jpg", SearchOption.AllDirectories);

        public LevelOnePartOne()
        {
            InitializeComponent();
        }



        private void LevelOnePartOne_Load(object sender, EventArgs e)
        {


            ProceedBtn.Hide();
           
            PreviousBtn.Hide();

           
           

            if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
                directoryexists = true;

            }

            Random rand = new Random();
            string[] files = Directory.GetFiles(@"C:\Users\Marina\Desktop\FaceNameGame\images\", "*.jpg", SearchOption.AllDirectories);


             for (m = 0; m < 3; m++)
            {

               Loop:
                picturenames[m] = files[rand.Next(files.Length)];
                string name1 = picturenames[m].ToString();
                string name2 = name1.Substring(name1.LastIndexOf("\\"));
                string name3 = name2.Remove(name2.Length - 4, 4);
                string name = name3.Remove(0, 1);
                string filename = name2.TrimStart('\\');
               
               
               
                if (!IsDirectoryEmpty(targetPath))
                {


                    if (File.Exists("C:\\Users\\Marina\\Desktop\\FaceNameGame\\images\\used\\" + filename))
                    {

                        goto Loop;  
                       
                     

                    }

                }

                string sourceFile = System.IO.Path.Combine(sourcePath, filename);
                string destFile = System.IO.Path.Combine(targetPath, filename);

               

                System.IO.File.Copy(sourceFile, destFile, true);
               
            }


           

            ShowCurrentImage(0);
                k = 1;


        }

           

           
       

        private void NextBtn_Click(object sender, EventArgs e)
        {
            piccount++;

            if(piccount < 3)
            {

                if (k<3)
                {
                   
                    ShowCurrentImage(k);
                    k++;
                   
                }
               
            }    
           
            if (piccount == 2)
            {
                NextBtn.Hide();
                ProceedBtn.Show();
            }

            if (k > 0)
            {
                PreviousBtn.Show();
            }

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {


        }

        private void PhotoTemplatePicBx_Click(object sender, EventArgs e)
        {

        }

        private void ProceedBtn_Click(object sender, EventArgs e)
        {
            LevelOnePartTwo lvltwoprttwo = new LevelOnePartTwo();
            this.Hide();
            lvltwoprttwo.Show();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                this.Close();

                if (directoryexists)
                {
                    Directory.Delete(targetPath, true);
                }
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void PreviousBtn_Click(object sender, EventArgs e)
        {
           

                if (k > 0 && k < 3)
                {

                    ShowCurrentImage(k);
                    k--;

                }

           

           

        }

        protected void ShowCurrentImage(int picindex)
        {

            string[] filestouse = Directory.GetFiles(@"C:\Users\Marina\Desktop\FaceNameGame\images\used\", "*.jpg", SearchOption.AllDirectories);
            pictureBox1.Load(filestouse[picindex]);
            string nameused1 = pictureBox1.ImageLocation.ToString();
            string nameused2 = nameused1.Substring(nameused1.LastIndexOf("\\"));
            string nameused3 = nameused2.Remove(nameused2.Length - 4, 4);
            string nameused = nameused3.Remove(0, 1);
            namelbl.Text = "Hello,my name is" + " " + nameused;
               

           
        }


        public bool IsDirectoryEmpty(string path)
        {
            string[] dirs = Directory.GetDirectories(path); string[] files = System.IO.Directory.GetFiles(path);
            return dirs.Length == 0 && files.Length == 0;
        }

    }
}



everything works fine except the previous button...any ideas on how i can do it? Thanks in advance for you help...
0
Comment
Question by:Marina K
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
Prakash Samariya earned 500 total points
ID: 41774493
try below code! please check value of "k"
private void PreviousBtn_Click(object sender, EventArgs e)
{
            if (k > 0 && k < 3)
            {
                    k--;
                    ShowCurrentImage(k);
            }
            if (k <=0)
            {
                PreviousBtn.Hide();
            }
}

Open in new window

1
 
LVL 8

Expert Comment

by:Prakash Samariya
ID: 41801251
@Marina K, Did you find the resolution?

Hope you might got some help from my answer!!
0
 

Author Closing Comment

by:Marina K
ID: 41801386
I figured it out now. This answer helped me very much. Thank you
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Needing an event fired when a row is added on a datagridview 4 36
Error when loading the database 16 57
C# HTTP GET method sample code 3 41
SQL Exceptions 3 39
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now