Windows Explorer-Like Program: extends ListView : ListView « GUI Windows Form « C# / C Sharp






Windows Explorer-Like Program: extends ListView

 

using System;
using System.Diagnostics;          // For Process.Start
using System.Drawing;
using System.IO;
using System.Windows.Forms;

class FileListView : ListView {
    string strDirectory;

    public FileListView() {
        View = View.Details;

        ImageList imglst = new ImageList();
        imglst.Images.Add(new Bitmap(GetType(), "DOC.BMP"));
        imglst.Images.Add(new Bitmap(GetType(), "EXE.BMP"));

        SmallImageList = imglst;
        LargeImageList = imglst;

        Columns.Add("Name", 100, HorizontalAlignment.Left);
        Columns.Add("Size", 100, HorizontalAlignment.Right);
        Columns.Add("Modified", 100, HorizontalAlignment.Left);
        Columns.Add("Attribute", 100, HorizontalAlignment.Left);
    }
    public void ShowFiles(string strDirectory) {
        this.strDirectory = strDirectory;

        Items.Clear();
        DirectoryInfo dirinfo = new DirectoryInfo(strDirectory);
        FileInfo[] afileinfo;

        try {
            afileinfo = dirinfo.GetFiles();
        } catch {
            return;
        }

        foreach (FileInfo fi in afileinfo) {
            ListViewItem lvi = new ListViewItem(fi.Name);

            if (Path.GetExtension(fi.Name).ToUpper() == ".EXE")
                lvi.ImageIndex = 1;
            else
                lvi.ImageIndex = 0;

            lvi.SubItems.Add(fi.Length.ToString("N0"));
            lvi.SubItems.Add(fi.LastWriteTime.ToString());

            string strAttr = "";

            if ((fi.Attributes & FileAttributes.Archive) != 0)
                strAttr += "A";

            if ((fi.Attributes & FileAttributes.Hidden) != 0)
                strAttr += "H";

            if ((fi.Attributes & FileAttributes.ReadOnly) != 0)
                strAttr += "R";

            if ((fi.Attributes & FileAttributes.System) != 0)
                strAttr += "S";

            lvi.SubItems.Add(strAttr);

            Items.Add(lvi);
        }
    }
    protected override void OnItemActivate(EventArgs ea) {
        base.OnItemActivate(ea);

        foreach (ListViewItem lvi in SelectedItems) {
            try {
                Process.Start(Path.Combine(strDirectory, lvi.Text));
            } catch {
                continue;
            }
        }
    }
}


class ExplorerLike : Form {
    FileListView filelist;
    MenuItemView mivChecked;

    public static void Main() {
        Application.Run(new ExplorerLike());
    }
    public ExplorerLike() {
        BackColor = SystemColors.Window;
        ForeColor = SystemColors.WindowText;

        filelist = new FileListView();
        filelist.Parent = this;
        filelist.Dock = DockStyle.Fill;

        Splitter split = new Splitter();
        split.Parent = this;
        split.Dock = DockStyle.Left;
        split.BackColor = SystemColors.Control;

        Menu = new MainMenu();
        Menu.MenuItems.Add("&View");

        string[] astrView = { "Lar&ge Icons", "S&mall Icons", 
                                "&List", "&Details" };
        View[] aview = { View.LargeIcon, View.SmallIcon, 
                           View.List, View.Details };
        EventHandler eh = new EventHandler(MenuOnView);

        for (int i = 0; i < 4; i++) {
            MenuItemView miv = new MenuItemView();
            miv.Text = astrView[i];
            miv.View = aview[i];
            miv.RadioCheck = true;
            miv.Click += eh;

            if (i == 3)         // Default == View.Details
               {
                mivChecked = miv;
                mivChecked.Checked = true;
                filelist.View = mivChecked.View;
            }
            Menu.MenuItems[0].MenuItems.Add(miv);
        }
        Menu.MenuItems[0].MenuItems.Add("-");

        MenuItem mi = new MenuItem("&Refresh",
                            new EventHandler(MenuOnRefresh), Shortcut.F5);
        Menu.MenuItems[0].MenuItems.Add(mi);
    }
    void DirectoryTreeViewOnAfterSelect(object obj, TreeViewEventArgs tvea) {
        filelist.ShowFiles(tvea.Node.FullPath);
    }
    void MenuOnView(object obj, EventArgs ea) {
        mivChecked.Checked = false;
        mivChecked = (MenuItemView)obj;
        mivChecked.Checked = true;

        filelist.View = mivChecked.View;
    }
    void MenuOnRefresh(object obj, EventArgs ea) {
    }
}
class MenuItemView : MenuItem {
    public View View;
}

 








Related examples in the same category

1.Dragging and dropping between ListView
2.ListView Item clicked eventListView Item clicked event
3.Folder Browser based on ListViewFolder Browser based on ListView
4.Set ColumnHeader for ListView
5.Sort a List View by Any ColumnSort a List View by Any Column
6.Add Data to a ListView
7.Extends ListViewItem
8.Use RadioButton to control the ListView display styleUse RadioButton to control the ListView display style
9.Display Directory info in a ListViewDisplay Directory info in a ListView
10.Add ListView column and insert ListView rowsAdd ListView column and insert ListView rows
11.Use ListViewItem to display file information
12.ListView ExampleListView Example
13.ListView Country: image and fontListView Country: image and font
14.Use ListView to display File info: name, size and dateUse ListView to display File info: name, size and date
15.Use ListView to display file name and double click the name to execute that fileUse ListView to display file name and double click the name to execute that file
16.Use ListView to diaplay folder info and double click to enter that directoryUse ListView to diaplay folder info and double click to enter that directory