ListBox issue

S

Shawn

I know that I can use DisplayMember and ValueMember when using a DataSource
on the ListBox, but is it possible to use the same technique when using
ListBox.Items.Add method? In my application I use a OpenFileDialog to let
the user select filenames into the ListBox. The problem is that I don't
wish to show the path + filename in the ListBox, only the filename. But I
need the full path to know where the file is so I can't just chop it off. I
could use DataSource with DisplayMember and ValueMember, but then I won't be
able to remove any of the items in the ListBox, and I have to be able to do
that...

Any help is greatly appreciated.
Shawn
 
H

Herfried K. Wagner [MVP]

* "Shawn said:
I know that I can use DisplayMember and ValueMember when using a DataSource
on the ListBox, but is it possible to use the same technique when using
ListBox.Items.Add method? In my application I use a OpenFileDialog to let
the user select filenames into the ListBox. The problem is that I don't
wish to show the path + filename in the ListBox, only the filename. But I
need the full path to know where the file is so I can't just chop it off. I
could use DataSource with DisplayMember and ValueMember, but then I won't be
able to remove any of the items in the ListBox, and I have to be able to do
that...

Have a look at the code, ignore the text:

<http://www.mvps.org/dotnet/dotnet/code/controls/index.html#bkm10>
 
C

Cor

Herfried,
I have ignored the text, but the question was how without a dataset and the
anwer is with a dataset, that you can find normal on MSDN.
What is the question, and I am intrested too.

Just something in pseudocode
\\\
Dim alistbox as new listbox
dim alistitem as new listitem
listitem.add("Herfried","Austria")
alistbox.items.add(listitem)
///

As far as I know this is easy on a webform, but I could not find any
documentation on a window form for that.

Do you know where to can find it, or was it on that page and could I not
understand that language (not true).
:)
Cor
 
F

Fergus Cooney

Hi Shawn,

What Herfried's example is showing is that you can stick any object into
the ListBox. The ListBox will use the ToString method for display purposes,
but when you access the list in your code, you get the whole object.

You could have a class or structure which holds the full filepath and has
a ToString method returning just the tail end.

Regards,
Fergus
 
S

Shawn

Thanks a lot!

Shawn



Hi Shawn,

What Herfried's example is showing is that you can stick any object into
the ListBox. The ListBox will use the ToString method for display purposes,
but when you access the list in your code, you get the whole object.

You could have a class or structure which holds the full filepath and
has
a ToString method returning just the tail end.

Regards,
Fergus
 
H

Herfried K. Wagner [MVP]

* "Fergus Cooney said:
What Herfried's example is showing is that you can stick any object into
the ListBox. The ListBox will use the ToString method for display purposes,
but when you access the list in your code, you get the whole object.

My sample shows how to do that with a combobox, but it works similar
with a listbox. The code is self-explaining.
 
F

Fergus Cooney

Hi Shawn,

I'm very curious and I'd love the answers to these questions. You <cannot>
answer them wrongly (for this is not about you personally).

1 Did you follow Herfried's link?
2 What where your reactions when you got there?
3 What effect did seeing a load of German text have?
4 Did you examine the code?
5 Did you realise that there were two bits of code to examine?
6 Did it give a hint, make complete sense, or what?

Please answer these - we're talking valuable feedback here. ;-))

Regards,
Fergus
 
H

Herfried K. Wagner [MVP]

* "Fergus Cooney said:
I'm very curious and I'd love the answers to these questions. You <cannot>
answer them wrongly (for this is not about you personally).

1 Did you follow Herfried's link?
2 What where your reactions when you got there?
3 What effect did seeing a load of German text have?
4 Did you examine the code?
5 Did you realise that there were two bits of code to examine?
6 Did it give a hint, make complete sense, or what?

Please answer these - we're talking valuable feedback here. ;-))

ROFLM*O
 
C

Cor

Hi Herfried,
I see everybody gets an answer yes,
:-((
My question was what was as well in the question, can you do it without a
datasource in a windowsform?.

Cor
 
H

Herfried K. Wagner [MVP]

* "Cor said:
My question was what was as well in the question, can you do it without a
datasource in a windowsform?.

What exactly do you want to do?
 
C

Cor

Hi Herfried,
This was the origonal question.
I know that I can use DisplayMember and ValueMember when using a DataSource
on the ListBox, but is it possible to use the same technique when using
ListBox.Items.Add method

And I am curious about it because I did search a long time.

It would be easy if the add was overloaded, but it seems to me not, or that
there was a possibility as on the listbox from the webform (adding
listitems).

Cor
 
S

Shawn

Hi Cor.
What I learned was that I could put almost anything into a ListBox, you can
even add a System.Web.UI.WebControls.ListItem. Like this:
myListItem.Items.Add(New
system.Web.UI.WebControls.ListItem("test.txt","C:\temp\test.txt")) The
first parameter will be the equivalent of DisplayMember and the second will
be the equivalent of ValueMember.

Was this what you were looking for?
Shawn



Hi Herfried,
This was the origonal question.
I know that I can use DisplayMember and ValueMember when using a DataSource
on the ListBox, but is it possible to use the same technique when using
ListBox.Items.Add method

And I am curious about it because I did search a long time.

It would be easy if the add was overloaded, but it seems to me not, or that
there was a possibility as on the listbox from the webform (adding
listitems).

Cor
 
C

Cor

Shawn,
This is about a "winform" listbox. I get the idea it is totaly different
from a "webform", while most controls with the same name are something the
same (absolute not totaly).

So I am curious if that with the listbox is true.

That behaviour with a listbox on a webcontrol, do I find very nice,
therefore I don't totaly not understand why that "easy" way is not on a
window form

Cor
 

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

Top