Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9097963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:14:55+00:00 2026-06-17T00:14:55+00:00

i have the code in which i m displaying directories and files from the

  • 0

i have the code in which i m displaying directories and files from the computer.i can open them by using folerbrowser. so far so good but now i want to add backward and forward button so that i can see the previous folder or file selected and go fwd to.

    public partial class ListView : Form
    {

        public ListView()
        {
            InitializeComponent();
            comboBox1.SelectedIndex = 2;

        }

        private void PopulateListView()
        {
            listView1.Clear();
            //headers listview

            listView1.Columns.Add("File Name", 200);
            listView1.Columns.Add("Size", 80);
            listView1.Columns.Add("Last Accessed", 110);
            ExtensionsHolder.Clear();
            if (folderBrowser.ShowDialog() == DialogResult.OK)
            {
                //int thefile = 0;
                string[] dirData = new string[3];
                string[] filData = new string[3];
                string[] files = Directory.GetFiles(folderBrowser.SelectedPath);
                var folders = Directory.GetDirectories(folderBrowser.SelectedPath)
     .Where(d => !new DirectoryInfo(d).Attributes.HasFlag(FileAttributes.System | FileAttributes.Hidden));


                try
                {
                //THIS IS THE ITERATION TO GET SUBDIRECTORIES
                foreach (string dir in folders)
                {
                    //string name = Path.g;
                    dirData[0] = dir.ToString();
                    dirData[1] = dir.Length.ToString();
                    dirData[2] = File.GetLastAccessTime(dir).ToString();
                    ListViewItem lv = new ListViewItem(dirData, imageList1.Images.Count);
                    lv.Tag = dir;

                    imageList1.Images.Add(IconExtractor.Form1.GetFolderIcon(IconExtractor.IconSize.Small, IconExtractor.FolderType.Closed));

                    listView1.SmallImageList = imageList1;
                    listView1.Items.Add(lv);

                }
                //THIS IS ITERATION FOR FILES OF THE DIRECTORY

                    foreach (string file in files)
                    {

                        FileInfo finfo = new FileInfo(file);
                        FileAttributes fatr = finfo.Attributes;

                        string name = Path.GetFileNameWithoutExtension(file);
                        filData[0] = name;
                        filData[1] = finfo.Length.ToString();
                        filData[2] = File.GetLastAccessTime(file).ToString();
                        ListViewItem lv = new ListViewItem(filData, imageList1.Images.Count - 1);
                        lv.Tag = file;
                        string extension = Path.GetExtension(file);
                        if (!extension.Equals("") && !ExtensionsHolder.Contains(extension))
                        {
                            ExtensionsHolder.Add(extension);
                            imageList1.Images.Add(BlackFox.Win32.Icons.IconFromExtensionShell(extension, BlackFox.Win32.Icons.SystemIconSize.Small));
                        }

                        listView1.SmallImageList = imageList1;

                        listView1.Items.Add(lv);

                    }
                }
                 catch (UnauthorizedAccessException)
            {
               listView1.Items.Add("Access denied");
            } 

            }
        }
        List<string> ExtensionsHolder = new List<string>();
        private void textBox1_TextChanged(object sender, EventArgs e)
{
    string chattextbox=textBox1.Text;
}
        private void button1_Click(object sender, EventArgs e)
        {




            PopulateListView();
            textBox1.Text = folderBrowser.SelectedPath;
        }

        private void listView1_ItemActivate(object sender, EventArgs e)
        {

                try
                {
                    string sPath = listView1.FocusedItem.Tag.ToString();

                     Process.Start(sPath);
        }
        catch(Exception Exc)    {   MessageBox.Show(Exc.ToString());    }    
    }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
          if(comboBox1.SelectedIndex == 0)
         listView1.View = View.LargeIcon;
            if(comboBox1.SelectedIndex==1)
                listView1.View = View.SmallIcon;
            if(comboBox1.SelectedIndex==2)
                listView1.View = View.Details;
            if (comboBox1.SelectedIndex == 3)
                listView1.View = View.List;
            else if (comboBox1.SelectedIndex == 5)
                listView1.View = View.Tile;



        }

        private void Farward_Click(object sender, EventArgs e)
        {
            //Stack fpath = new Stack();
            Stack<string> itemindex = new Stack<string>();
            itemindex.Push(listView1.SelectedIndices.ToString());
            itemindex.Push("two");
            itemindex.Push("three");
            //itemindex.Push("four");
            //itemindex.Push("five");

            // A stack can be enumerated without disturbing its contents. 
            foreach (string number in itemindex)
            {
                Console.WriteLine(number);
            }
            Console.WriteLine("\nPopping '{0}'", itemindex.Pop());
            Console.WriteLine("Peek at next item to destack: {0}",
                itemindex.Peek());
            Console.WriteLine("Popping '{0}'", itemindex.Pop());
        }

        private void Back_Click(object sender, EventArgs e)
        {

            int i = 0;
            for (i = 0; i < listView1.Items.Count; i++)
            {
                    if (i != 0)
                    {
                        listView1.Items[i - 1].SubItems[1].Text = "Selected";
                        // txtPath.Text = listView1.Items[i - 1].Text;

                }

            }
        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listView1.SelectedIndices.Count > 0)
                MessageBox.Show("selected index"+ listView1.SelectedIndices[0]);
            else
            MessageBox.Show("No item selected");
        }


    }
}

Forward . . .

private void Farward_Click(object sender, EventArgs e)
{
    //Stack fpath = new Stack();
    Stack<string> itemindex = new Stack<string>();
    itemindex.Push(listView1.SelectedIndices.ToString());
    itemindex.Push("two");
    itemindex.Push("three");
    //itemindex.Push("four");
    //itemindex.Push("five");

    // A stack can be enumerated without disturbing its contents. 
    foreach (string number in itemindex)
    {
        Console.WriteLine(number);
    }
    Console.WriteLine("\nPopping '{0}'", itemindex.Pop());
    Console.WriteLine("Peek at next item to destack: {0}",
        itemindex.Peek());
    Console.WriteLine("Popping '{0}'", itemindex.Pop());
}

Backward . . .

private void Back_Click(object sender, EventArgs e)
{

    int i = 0;
    for (i = 0; i < listView1.Items.Count; i++)
    {
        if (i != 0)
        {
            listView1.Items[i - 1].SubItems[1].Text = "Selected";
            // txtPath.Text = listView1.Items[i - 1].Text;

        }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T00:14:57+00:00Added an answer on June 17, 2026 at 12:14 am

    It’s kind of hard to do this because your code contains a lot of unecessary UI stuff. Anyway this is how I did it . . .

    Let lvItems be our ListView, and allow A B C D be the elements in our ListView. We select only A, C, D . . .

    lvItems.SelectedIndices.Add(0);
    lvItems.SelectedIndices.Add(2);
    lvItems.SelectedIndices.Add(3);
    

    So in our Next button, we define the Click event as follows . . .

    Stack<string> sItms = new Stack<string>();
    for (int i = 0; i < lvItems.SelectedIndices.Count; ++i)
    {
        int idx = lvItems.SelectedIndices[i];
        string itm = lvItems.Items[idx].ToString();
        sItms.Push(itm);
    }
    
    // Our set contains D, C, A 
    // Peek() will display D
    

    And in our Back button, we define the Click event as follows . . .

    Stack<string> sItms = new Stack<string>();
    for (int i = 0; i < lvItems.SelectedIndices.Count; ++i)
    {
        int idx = lvItems.SelectedIndices[(lvItems.SelectedIndices.Count - 1)-i];
        string itm = lvItems.Items[idx].ToString();
        sItms.Push(itm);
    }
    
    // Our Set Contains A, C, D
    // Peek() will display A
    

    Is this what you’re looking for? Tell me how it goes . . .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have UI which displaying status of long-running operations (downloading some text files from
I have the following bit of code which is responsible for displaying a tooltip.
I have the following code which keeps displaying 0 although rows are being inserted
I have this code which builds an IList<string> : IList<string> databases; using (MySqlConnection _conn
I have a js code which is displaying Youtube videos. The problem is that
I have a code which is working fine and displaying some set of values
I have this code which i am using to present my data in a
I have a problem with updating the value of property from code which is
I have a few Collapsible segments in my Code which i have implemented using
Hi I have a list of buttons which I am displaying using for loop.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.