0

I'm trying to make a File Manager with WinForms. I made a navigation bar, two buttons, one to go back and an other to forward the navigation.

enter image description here

I want to go back navigation when I'm in WebBrowser form and I press BACKSPACE key. Apparently this work fine, but when I want to erase a character renaming a file the BACKSPACE key event is called.

How can I check if are selected files in the WebBrowser and apply the go back navigation only if there aren't ?

private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    if (e.KeyCode == Keys.Back)
    {
        //Do only if no selected items in WebBrowser 
        webBrowser1.GoBack(); 
    }
}
flaviussn
  • 1,305
  • 2
  • 15
  • 33

2 Answers2

0

I would probably try initiating a global bool variable called fileSelected = false;

What object are you using to display the files? In other words, what object would a file be selected in the list? I'm assuming a ListBox?

Then, set this to true when a file is selected using the ListBox.SelectedIndexChanged event. More information on handling this event here.

Your if statement would then look something like this:

if (e.KeyCode == Keys.Back && !fileSelected)
{
     //Do only if no selected items in WebBrowser 
     webBrowser1.GoBack(); 
}

A screenshot may be helpful as well. Hope this helps.

Ben Holland
  • 321
  • 1
  • 8
  • I just edit my post with a screenshot. I use a WebBrowser control, and in the screenshot case I want to know if "serimag.png" is selected. Something like SelectedIndex in ListBox. – flaviussn Feb 13 '15 at 10:07
0

Alright, try this instead. It's a little bit of a hack, but I think it'll do what you want it to do (which is detect if any files are selected).

using System.Collections.Specialized;

private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    Clipboard.Clear();
    SendKeys.Send("^C");

    StringCollection sc = Clipboard.GetFileDroplist();
    Clipboard.Clear();

    if(sc.Count == 0) // Meaning no files in the list were copied
    {
        if (e.KeyCode == Keys.Back)
        {
            //Do only if no selected items in WebBrowser 
            webBrowser1.GoBack(); 
        }
    }
}

Here's what this is essentially doing:
1. Immediately clearing the clipboard
2. Sending the command to copy the contents of the selection (whether one or many files is selected)
3. Returning the list of file(s) that are selected into a StringCollection
4. Testing if the StringCollection is empty or not

Like I said, a bit of a hack, but I don't think there's any other way to determine if there is a selection using any other methods.

Ben Holland
  • 321
  • 1
  • 8
  • It doesn't work for me: 'System.Windows.Forms.Clipboard' does not contain a definition for 'GetFileDroplist' – flaviussn Feb 17 '15 at 10:21