1

I am making a program in which the user needs to load in multiple files. However, in the ListBox I need to show only file names of the files they loaded but still be able to use the files loaded. So I want to hide the full path. This is how I load a file into the ListBox now, but it shows the whole path:

private void browseBttn_Click(object sender, EventArgs e)
{
    OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
    OpenFileDialog1.Multiselect = true;
    OpenFileDialog1.Filter = "DLL Files|*.dll";
    OpenFileDialog1.Title = "Select a Dll File";
    if (OpenFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        dllList.Items.AddRange(OpenFileDialog1.FileNames);
    }
}
Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
Rivasa
  • 6,510
  • 3
  • 35
  • 64
  • Im doing this too, only I save a skeleton ID from Kinect when the button is press. I decided to use [`ComboBoxs`](http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.aspx) because of the [`Add`](http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.objectcollection.add.aspx) Method, but I wonder if I was in the wrong. I can't wait to see how this will turn out... – Liam McInroy Apr 23 '12 at 01:53

2 Answers2

3
// Set a global variable to hold all the selected files result
List<String> fullFileName;

// Browse button handler
    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
        OpenFileDialog1.Multiselect = true;
        OpenFileDialog1.Filter = "DLL Files|*.dll";
        OpenFileDialog1.Title = "Seclect a Dll File";
        if (OpenFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            // put the selected result in the global variable
            fullFileName = new List<String>(OpenFileDialog1.FileNames);

            // add just the names to the listbox
            foreach (string fileName in fullFileName)
            {
                dllList.Items.Add(fileName.Substring(fileName.LastIndexOf(@"\")+1));
            }


        }
    }

    // handle the selected change if you wish and get the full path from the selectedIndex.
    private void dllList_SelectedIndexChanged(object sender, EventArgs e)
    {
        // check to make sure there is a selected item
        if (dllList.SelectedIndex > -1)
        {
            string fullPath = fullFileName[dllList.SelectedIndex];

            // remove the item from the list
            fullFileName.RemoveAt(dllList.SelectedIndex);
            dllList.Items.Remove(dllList.SelectedItem);
        }
    }
Ryan Fiorini
  • 800
  • 5
  • 9
  • one more thing, how would I write a function to remove that item from the listbox, as well as the full path that corrsponds with it? – Rivasa Apr 23 '12 at 23:46
  • 1
    I updated the code to reflect your question. I changed the string array to a List This will allow for an easy remove of the full path. – Ryan Fiorini Apr 24 '12 at 11:23
  • one question though, how exactly do i delete an item, do i use dllList_SelectedIndexChanged or do i interface it some different way? – Rivasa Apr 24 '12 at 19:10
2

You can extract the fileName of the absolute path using the static Class Path in the System.IO namespace

//returns only the filename of an absolute path.

Path.GetFileName("FilePath");
Liam McInroy
  • 4,339
  • 5
  • 32
  • 53
Nudier Mena
  • 3,254
  • 2
  • 22
  • 22