need a good idea :)

  • Thread starter Thread starter xxxx
  • Start date Start date
X

xxxx

Hi all,

i need some help here plz :)
i'm building a small application that will dispaly a list of folders
in a listbox, but i only want to display the folder name rather than
the whole path, so i take the substring from the whole path and add it
to the listbox.
the problem is, later, when the user selects to do anything with this
file, i want to get the whole path back(mandatory), but now i only
have the the name and not the path.

the dummy solution i do, is to get the index of the selected index and
then get the path from teh path list at that index, but i don't like
such a solution, i think its not professional nor efficient.

so any ideas??

i'm using VS2003, c# to build a desktop application


thanks for time and help
 
i tried using the listitem, but it was not defined.
i guess its applicable for asp.net applications only, i'm trying to
build a desktop application.
 
as long as i know, this Tag property is related to the listbox itself,
not to an item in the list, am i wrong??
 
i tried using the listitem, but it was not defined.
i guess its applicable for asp.net applications only, i'm trying to
build a desktop application.

You can use the ListItem object from the System.Web.UI.WebControls namespace
in a dektop application - it's just another part of the Framework. I do this
all the time...

MyListItem = new System.Web.UI.WebControls.ListItem();

etc.
 
Hi all,

i need some help here plz :)
i'm building a small application that will dispaly a list of folders
in a listbox, but i only want to display the folder name rather than
the whole path, so i take the substring from the whole path and add it
to the listbox.
the problem is, later, when the user selects to do anything with this
file, i want to get the whole path back(mandatory), but now i only
have the the name and not the path.

the dummy solution i do, is to get the index of the selected index and
then get the path from teh path list at that index, but i don't like
such a solution, i think its not professional nor efficient.

so any ideas??

i'm using VS2003, c# to build a desktop application

thanks for time and help

The Tag property is on all controls. It is not used by the framework,
it is meant to be used for user data. So when you populate your
ListView, you may do it like this:
ListViewItem lvi = new ListViewItem();
lvi.Text = Path.GetFileName(fullPath);
lvi.Tag = fullPath;
listView1.Items.Add(lvi);

-but it is good style to create a small class for your purpose similar
to this:
private class FolderListViewTag {
public string Path;

public override string ToString() {
return Path.GetFileName(Path);
}
}
-and then put instances of this one into the Tag property. Then,
later, when you find out you need more data, or you need to alter the
string representation of each ListViewItem or you need to make it
configurable, or whatever, then it will require a minimal change.

Good luck :o)
 
thanks for the great help,

but when i try out this piece of code, i don't get my string in the
listbox, instead i get something like ListViewItem:{Webapplication1}
instead of WebApplication1 :(
 
In windows forms you can put any object into a list box, with the
result of ToString() displayed as the list box text.

So create a wrapper class for your file name, holding both the full
path and the file name:
public class Wrapper
{
private string _path;
private string _fileName;

public Wrapper(string path)
{
_path = path;
_fileName = System.IO.Path.GetFileName(_path);
}

public string Path
{
get
{
return _path;
}
}

public override string ToString()
{
return _fileName;
}
}

Then add objects of this type to the listbox:
myListBox.Items.Add(new Wrapper(@"C:\path\to\file"));

Then you the selected item will be of your type, e.g.
Wrapper selected = myListBox.SelectedItem as Wrapper;
if(selected != null)
{
MessageBox.Show(selected.Path + " was selected");
}

hth
Terry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top