11 Replies - 3167 Views - Last Post: 13 March 2012 - 02:06 PM Rate Topic: -----

#1 emreozpalamutcu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-March 12

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

Posted 13 March 2012 - 08:37 AM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: How to open file location from list view item using context menu strip

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8379
  • View blog
  • Posts: 31,147
  • Joined: 12-June 08

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

Posted 13 March 2012 - 08:40 AM

No that wasn't clear..

Side question - why are you not using the 'open file dialog' from the toolbox?
Was This Post Helpful? 0
  • +
  • -

#3 emreozpalamutcu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-March 12

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

Posted 13 March 2012 - 08:42 AM

I want to open file location using windows explorer. I don't want open file dialog because I'm not going to use the file in the program, I just want them to be able to open windows location of the file using windows tools "Explorer.exe".
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4312
  • View blog
  • Posts: 7,467
  • Joined: 08-June 10

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

Posted 13 March 2012 - 10:19 AM

You know, OpenFileDialog doesn't actually open a file. It gives you a path.
Was This Post Helpful? 0
  • +
  • -

#5 emreozpalamutcu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-March 12

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

Posted 13 March 2012 - 10:30 AM

Well I can get the path, I don't need a open file dialog! This is how it works:

the code is:

Process.Start("Explorer.exe", "C:\\"); //That works! The problem is, I want to replace that with:

Process.Start("Explorer.exe", Path.GetDirectoryName(fileInfo.FullName)); // It doesn't recognise the fileInfo, which i used on the button, so I need to link it.
Was This Post Helpful? 0
  • +
  • -

#6 Julyuary  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 106
  • Joined: 20-December 11

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

Posted 13 March 2012 - 12:19 PM

Your details aren't very clear, but from what I gather, you need the directory of fileInfo from the loop in the button click event? Try adding a list to your scope

        List<FileInfo> Files = new List<FileInfo>();


Change fileInfo to FileInfo type instead of var so you can include it in your list
                    foreach (FileInfo fileInfo in dirInfo.GetFiles(ext))
                    {

                        Files.Add(fileInfo);
                        listView1.Items.Add(new ListViewItem(new string[] { fileInfo.Name, Path.GetDirectoryName(fileInfo.FullName) + @"\" }));
                    }


and.. Here you go.
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {

            ListViewItem listviewitem = null;
            foreach (ListViewItem i in listView1.Items)
            {
                if (i.Selected)
                    listviewitem = i;
            }

            FileInfo fileinfo = null;
            foreach (FileInfo F in Files)
            {
                if (F.Name == listviewitem.Text)
                    fileinfo = F;
            }

            Process.Start("explorer.exe", Path.GetDirectoryName(fileinfo.FullName));


        }

There are probably better ways of going about this, and if it's unclear (which it probably is) I'll explain need be.

This post has been edited by Julyuary: 13 March 2012 - 12:20 PM

Was This Post Helpful? 1
  • +
  • -

#7 emreozpalamutcu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-March 12

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

Posted 13 March 2012 - 12:31 PM

Works perfectly, however I had one more problem. How can show the context menu strip only when the user is directly clicks the list view item. What i mean by that is, if the user selected a item with left click and while that item is selected if they click any where on the list view including column it will still show. The code I used was:

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


Was This Post Helpful? 0
  • +
  • -

#8 Julyuary  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 106
  • Joined: 20-December 11

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

Posted 13 March 2012 - 12:36 PM

Try adding this after the form is initialized
listView1.MultiSelect = false;

or wherever you find it most convenient. It shouldn't appear if you right click anywhere else on the form, but I'll post the functions I have from this project that I haven't already.

        public application()
        {
            InitializeComponent();
            this.openToolStripMenuItem.Click += new System.EventHandler(this.openToolStripMenuItem_Click);
            listView1.MultiSelect = false;


        }


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

            }
        }


This post has been edited by Julyuary: 13 March 2012 - 12:46 PM

Was This Post Helpful? 0
  • +
  • -

#9 emreozpalamutcu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-March 12

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

Posted 13 March 2012 - 12:49 PM

You got me wrong, check this picture attachment, I had the item selected and i right clicked the column and context menu strip still showed up all though i was click the column!

I forgot to attach file, sorry!

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#10 Julyuary  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 106
  • Joined: 20-December 11

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

Posted 13 March 2012 - 12:59 PM

You aren't being very clear, but where is the cursor in that image?

This post has been edited by Julyuary: 13 March 2012 - 01:00 PM

Was This Post Helpful? 0
  • +
  • -

#11 emreozpalamutcu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-March 12

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

Posted 13 March 2012 - 01:01 PM

LOL, stupid windows hides the cursor.

It's on the "Folder Path" Column.
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,361
  • Joined: 02-June 10

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

Posted 13 March 2012 - 02:06 PM

Let me also throw in a few tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.


  • I strongly suggest installing VMware or some other virtualization technology on your development PC so you can create a couple virtual computers for testing. This would allow you to debug and test inside: WinXP32, XP64, Vista, Win7x32, Win7x64... etc. without having to actually have 5 physical PC's. Visual Studio will let you send the debug directly into one of these virtual machines so you can watch it operate, check its variables, see the crashes and so on just as if it were debugging on your real machine.

  • This can't be stressed enough in today's world of cell phone messaging:
    Don't use txt/sms/leet/T9 speak like: u no, u r, dnt, wut i m do-n, coz, al gud, b4, ny1, and so on like this guy:

    Spoiler


[/spoiler][/list.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1