1

私は、コンピューターからディレクトリとファイルを表示するコードを持っています。これまでのところは良いのですが、前のフォルダーまたはファイルが選択されていることを確認して進むことができるように、戻るボタンと進むボタンを追加したいと思います。

    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 要素がたくさん含まれているため、これを行うのはちょっと難しいです。とにかく、これが私がやった方法です。. .

ListViewを、 ListView 内の要素lvItemsを許可します。A B C DA、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 に答える