1

我有代码,我可以在其中显示来自计算机的目录和文件。我可以使用 folerbrowser 打开它们。到目前为止一切顺利,但现在我想添加后退和前进按钮,以便我可以看到选择的上一个文件夹或文件并转到 fwd。

    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");
        }


    }
}

向前 。. .

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;

        }

    }
}
4

1 回答 1

0

这样做有点困难,因为您的代码包含很多不必要的 UI 内容。无论如何,我就是这样做的。. .

让我们lvItems成为我们的 ListView,并允许A B C D成为我们的 ListView 中的元素。我们只选择 A, C, D 。. .

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

所以在我们的Next按钮中,我们定义 Click 事件如下。. .

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

在我们的Back按钮中,我们定义 Click 事件如下。. .

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

这是你要找的吗?告诉我进展如何。. .

于 2013-01-09T08:56:24.297 回答