How to open file location from list view item using context menu strip?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: How to open file location from list view item using context menu strip?

  1. #1
    Join Date
    Jan 2011
    Posts
    15

    Question How to open file location from list view item using context menu strip?

    Here is list of things I want to do:

    Display context menu strip only when mouse is over the item and right clicked on it, the code I have for that is this:

    private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
    {
    if (listView1.SelectedItems.Count == 0)
    e.Cancel = true;
    }

    private void contextMenuStrip1_MouseClick(object sender, MouseEventArgs e)
    {
    if (e.Button == MouseButtons.Right)
    {
    Point pt = listView1.PointToScreen(e.Location);
    contextMenuStrip1.Show(pt);
    }
    }
    The problem about this code is longest the item is selected it will display the menu strip, what I don't want is the context menu strip shouldn't show up unless directly right clicked on the item.

    _____________________________________________________________________________________________________________________________

    I have 3 options on the context menu strip, open, open file location and properties. If user right clicks the item and says open, its should ask the user what do you want to open it with using Windows explorer! For the open file location, I came up with this:

    Process.Start("explorer.exe", "I need filepath from listview item");
    That does open the custom location, however since there is going to be lots of files in the list view, I need the program to get the file location it self.

    The implementation I have for the scanning files and displaying them on list view is:

    private void button1_Click(object sender, EventArgs e)
    {

    listView1.Items.Clear();

    string[] directories = new string[] {
    @"C:\Windows",
    @"C:\Users\Emre\AppData\Local\CrashDumps"
    };

    string[] extensions = new string[] { "*" };

    foreach (var dir in directories)
    {
    var dirInfo = new DirectoryInfo(dir);
    foreach (var ext in extensions)
    {
    foreach (var fileInfo in dirInfo.GetFiles(ext))
    {
    listView1.Items.Add(new ListViewItem(new string[] { fileInfo.Name, Path.GetDirectoryName(fileInfo.FullName) + @"\", SizeUnit.FileSizeToString(fileInfo.Length) }));
    }
    }
    }
    }

    Hope that can help you a bit, if you need to know more ask, I also have a code to calculate the size of the file, i didn't insert is because I don't think you will need it really. If anything is not clear plz do ask and thank you very much all helps are appreciated.

  2. #2
    Join Date
    Mar 2012
    Posts
    17

    Re: How to open file location from list view item using context menu strip?

    from what i read. i think you got the problem for
    right click -> show the contextmenustrip and open the folder
    so i tried make a simple program with internal documentation on it.

    try to run it on C# windows form ..

    or u can try the attachment

    hope this helps

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            List<temp> x = new List<temp>();
    
            public Form1()
            {
                InitializeComponent();
            }
            // use this to get the path based on items name
            private String getPath(String name) 
            {
                foreach (var temp in x) 
                {
                    if (temp.name.Equals(name))
                        return temp.path;
                }
    
                return null;// if data not found
            }
    
            private void add(String path) 
            {
                // add data to our collection temp and the list on the form
                x.Add(new temp(path));
                listBox1.Items.Add(x[x.Count-1].name);
            }
    
            private void getFiles(String Folder_Path)
            {
    
                string[] files = Directory.GetFiles(Folder_Path);// get all file on the directory
                string[] folders = Directory.GetDirectories(Folder_Path);// get all folder on the directory
    
                foreach (string folder in folders)
                {
                    getFiles(folder);// search again
                }
    
                foreach (string file in files)
                {
                    add(file);// add
                }
            }
    
            private void listBox1_DragDrop(object sender, DragEventArgs e)
            {
                string[] directoryName = (string[])e.Data.GetData(DataFormats.FileDrop);
                //get all files inside folder
                foreach (string data in directoryName)
                {
                    if (data.LastIndexOf(".") >= 0)// based on extention
                    {
                        add(data);// add
                    }
                    else
                    {
                        getFiles(data);// call a recusive function to search data on folder
                    }
                }
            }
    
            private void listBox1_DragEnter(object sender, DragEventArgs e)
            {
                // nessesary to allow file to be dropped at the component
                if (e.Data.GetDataPresent(DataFormats.FileDrop))
                {
                    e.Effect = DragDropEffects.Copy;
                }
                else
                {
                    e.Effect = DragDropEffects.None;
                }
            }
    
            private void listBox1_MouseUp(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Right && listBox1.Items.Count != 0) 
                {
                    // search the index mouse currently at
                    int temp_index = e.Y/listBox1.ItemHeight;
    
                    if (temp_index < listBox1.Items.Count)
                    {
                        // make the items to automaticly selected
                        listBox1.SelectedIndex = temp_index;
                        
                        // when mouse click and the data is available
                        contextMenuStrip1.Show(this, this.PointToClient(Cursor.Position));
                    }
                }
            }
    
            private void openFolderToolStripMenuItem_Click(object sender, EventArgs e)
            {
                // set to variable the location we wanted to open
                String curr_path = getPath(listBox1.Items[listBox1.SelectedIndex].ToString());
                // take the directory using these
                curr_path = curr_path.Substring(0, curr_path.LastIndexOf("\\"));
                // open the folder
                System.Diagnostics.Process.Start(curr_path);
            }
        }
    
        class temp {
            public String path;
            public String name;
    
            public temp(String path) 
            {
                this.path = path;
                this.name = path.Substring(path.LastIndexOf("\\")+1);// remove path
            }
        }
    
    }
    Attached Files Attached Files

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center