By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,183 Members | 1,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,183 IT Pros & Developers. It's quick & easy.

ListViewItem

P: n/a
Gav
I am trying to have a ListView to dispay a list of names and want to have an
id stored within the list but not visable.

I have tried to go about doing this by using the ListViewItem and setting
the Text and Tag properties. Problem is when I add the ListViewItem to the
ListView I get the following

ListViewItem: {Test1}
ListViewItem: {Test2}
ListViewItem: {Test3}

Where Test1, Test2 and Test3 are the names I assigned to the Text property.

What amI doing wrong here?

thanks

Gav
Dec 14 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Dom
It doesn't sound like you did anything wrong. From your description,
you did the following;

ListViewItem m = new ListViewItem();
m.Text = "Test1";
m.Tag = new <Class (ID)>;
ListView.Items.Add (m);

And so on.

Did you not want "Test1" to show in the ListView? If not, then set
the Text property to whatever you want to show. Or did I miss
something?

Dom
On Dec 14, 11:12 am, "Gav" <g...@nospam.comwrote:
I am trying to have a ListView to dispay a list of names and want to have an
id stored within the list but not visable.

I have tried to go about doing this by using the ListViewItem and setting
the Text and Tag properties. Problem is when I add the ListViewItem to the
ListView I get the following

ListViewItem: {Test1}
ListViewItem: {Test2}
ListViewItem: {Test3}

Where Test1, Test2 and Test3 are the names I assigned to the Text property.

What amI doing wrong here?

thanks

Gav
Dec 14 '07 #2

P: n/a
Gav

"Dom" <do********@gmail.comwrote in message
news:10**********************************@s19g2000 prg.googlegroups.com...
It doesn't sound like you did anything wrong. From your description,
you did the following;

ListViewItem m = new ListViewItem();
m.Text = "Test1";
m.Tag = new <Class (ID)>;
ListView.Items.Add (m);

And so on.

Did you not want "Test1" to show in the ListView? If not, then set
the Text property to whatever you want to show. Or did I miss
something?

Dom
Yes that is what I am doing, see code below. Record 1&2 are both strings.

lvi = new ListViewItem();

lvi.Text = record[1];

lvi.Tag = record[0];

this.lbx_Members.Items.Add(lvi);

The problem is when the item is displayed in the ListView it appears as
"ListViewItem: {Test1}" rather than "Test1".

Gav
Dec 16 '07 #3

P: n/a
On Sun, 16 Dec 2007 02:24:16 -0800, Gav <ga*@nospam.comwrote:
[...]
lvi = new ListViewItem();
lvi.Text = record[1];
lvi.Tag = record[0];
this.lbx_Members.Items.Add(lvi);

The problem is when the item is displayed in the ListView it appears as
"ListViewItem: {Test1}" rather than "Test1".
Your question is a great example of why it's so important to post complete
code examples. You really have not provided enough details for anyone to
actually answer the question, because none of the declarations or
initialization of your variables is shown.

That said: based on the name you gave the control, "lbx_Members", it seems
likely that the control isn't a ListView, but rather is a ListBox. If
that's true, then of course the ListViewItem isn't going to work as a list
element. ListBox doesn't know anything about it, other than what the
ListViewItem.ToString() method returns.

If that doesn't answer your question, please post a _complete_ question,
including a concise-but-complete example of code that reliably reproduces
your problem.

Pete
Dec 16 '07 #4

P: n/a
Dom
Peter made a good point. If his guess is right, then you shouldn't be
adding a ListViewItem to a ListBox to begin with.

I'll assume you are using a ListBox. Then you need to create a class,
let's say, ListBoxItem. This class will need the following, at a
minimum.

1. Two members, m_ID, and m_String.
2. A constructor that sets m_ID, m_String.
3. A "get" property that returns m_ID.
4. A method that overrides the ToString() method. The ToString ()
method should, in your case, return m_String. In general, it should
return whatever you want to appear in the listbox.

To get the ID associated with an item, you need to downcast the item,
and use the "get" property.

Frankly, I think someone at MS messed up when they created the ListBox
control. It doesn't seem to fit in with the other controls.

HTH, and remember, all bets are off if you are not using a ListBox
control.

Dom

On Dec 16, 5:24 am, "Gav" <g...@nospam.comwrote:
"Dom" <dolivas...@gmail.comwrote in message

news:10**********************************@s19g2000 prg.googlegroups.com...
It doesn't sound like you did anything wrong. From your description,
you did the following;
ListViewItem m = new ListViewItem();
m.Text = "Test1";
m.Tag = new <Class (ID)>;
ListView.Items.Add (m);
And so on.
Did you not want "Test1" to show in the ListView? If not, then set
the Text property to whatever you want to show. Or did I miss
something?
Dom

Yes that is what I am doing, see code below. Record 1&2 are both strings.

lvi = new ListViewItem();

lvi.Text = record[1];

lvi.Tag = record[0];

this.lbx_Members.Items.Add(lvi);

The problem is when the item is displayed in the ListView it appears as
"ListViewItem: {Test1}" rather than "Test1".

Gav
Dec 17 '07 #5

P: n/a
On Mon, 17 Dec 2007 06:44:54 -0800, Dom <do********@gmail.comwrote:
[...]
Frankly, I think someone at MS messed up when they created the ListBox
control. It doesn't seem to fit in with the other controls.
IMHO, the main "mistake" was taking the shortcut route and just wrapping
the existing Windows controls as a way of implementing the .NET controls.

And I use the term "mistake" loosely. While the fact that they did this
does lead to some frustrating situations, especially with respect to
trying to subclass the .NET controls, it's easy to see why Microsoft did
it. The Windows control library has been under development for decades.
Reimplementing the entire library from scratch would be a monumental
effort, in addition to the monumental effort that .NET already is, and of
course it'd wind up with a whole new crop of bugs, bugs that have been
iteratively found and removed from the original Windows control library
over the years.

In this context, it's not hard to see why there's some inconsistency
between the different controls. Wth respect to the native Windows control
library, backward compatibility dictates that older control classes retain
their original design, but newer controls benefit from the experience of
using the older ones. And of course, being just a wrapper on that
library, the .NET controls inherit the same inconsistencies.

Pete
Dec 17 '07 #6

P: n/a
Dom
But even if they wanted to wrap up the old controls, why couldn't we
get something that allowed the following, which (I think) is more in
keeping with the usual approach for controls;

int n = ListBox.Items.Add ();
ListBox.Items[n].Text = <a string>;
ListBox.Items[n].Tag = <an object// this is optional

The original poster probably knows how to use more complicated
controls, but then stumbled when it came to something simple, like the
Listbox.

Dom
On Dec 17, 3:00 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Mon, 17 Dec 2007 06:44:54 -0800, Dom <dolivas...@gmail.comwrote:
[...]
Frankly, I think someone at MS messed up when they created the ListBox
control. It doesn't seem to fit in with the other controls.

IMHO, the main "mistake" was taking the shortcut route and just wrapping
the existing Windows controls as a way of implementing the .NET controls.

And I use the term "mistake" loosely. While the fact that they did this
does lead to some frustrating situations, especially with respect to
trying to subclass the .NET controls, it's easy to see why Microsoft did
it. The Windows control library has been under development for decades.
Reimplementing the entire library from scratch would be a monumental
effort, in addition to the monumental effort that .NET already is, and of
course it'd wind up with a whole new crop of bugs, bugs that have been
iteratively found and removed from the original Windows control library
over the years.

In this context, it's not hard to see why there's some inconsistency
between the different controls. Wth respect to the native Windows control
library, backward compatibility dictates that older control classes retain
their original design, but newer controls benefit from the experience of
using the older ones. And of course, being just a wrapper on that
library, the .NET controls inherit the same inconsistencies.

Pete
Dec 17 '07 #7

P: n/a
On Mon, 17 Dec 2007 12:30:34 -0800, Dom <do********@gmail.comwrote:
But even if they wanted to wrap up the old controls, why couldn't we
get something that allowed the following, which (I think) is more in
keeping with the usual approach for controls;

int n = ListBox.Items.Add ();
ListBox.Items[n].Text = <a string>;
ListBox.Items[n].Tag = <an object// this is optional
I think you're thinking that the above code doesn't do anything using the
old controls. But I believe it does. And inasmuch as the native Windows
control Listbox is different from the native Windows control list view, so
too will the .NET control be different.

I don't have time right now to go review the docs for the native controls,
but if you're interested you could do so yourself. I suspect that if you
do, you'll find the same difference there that you are seeing in .NET.
The difference in .NET likely exists only because it's different in the
underlying control.

Pete
Dec 17 '07 #8

P: n/a
Good 'ol Peter Duniho ....

Just like before, nobody ever puts enough information into their question to
suit you.
Just TRY to answer the question and be nice about it.



"Peter Duniho" wrote:
On Mon, 17 Dec 2007 12:30:34 -0800, Dom <do********@gmail.comwrote:
But even if they wanted to wrap up the old controls, why couldn't we
get something that allowed the following, which (I think) is more in
keeping with the usual approach for controls;

int n = ListBox.Items.Add ();
ListBox.Items[n].Text = <a string>;
ListBox.Items[n].Tag = <an object// this is optional

I think you're thinking that the above code doesn't do anything using the
old controls. But I believe it does. And inasmuch as the native Windows
control Listbox is different from the native Windows control list view, so
too will the .NET control be different.

I don't have time right now to go review the docs for the native controls,
but if you're interested you could do so yourself. I suspect that if you
do, you'll find the same difference there that you are seeing in .NET.
The difference in .NET likely exists only because it's different in the
underlying control.

Pete
Dec 21 '07 #9

P: n/a
On Dec 21, 8:07*am, vbtrying <vbtry...@discussions.microsoft.com>
wrote:
Good 'ol Peter Duniho ....

Just like before, nobody ever puts enough information into their question to
suit you.
Just TRY to answer the question and be nice about it.

"Peter Duniho" wrote:
On Mon, 17 Dec 2007 12:30:34 -0800, Dom <dolivas...@gmail.comwrote:
But even if they wanted to wrap up the old controls, why couldn't we
get something that allowed the following, which (I think) is more in
keeping with the usual approach for controls;
int n = ListBox.Items.Add ();
ListBox.Items[n].Text = <a string>;
ListBox.Items[n].Tag = <an object// this is optional
I think you're thinking that the above code doesn't do anything using the *
old controls. *But I believe it does. *And inasmuch as the native Windows *
control Listbox is different from the native Windows control list view, so *
too will the .NET control be different.
I don't have time right now to go review the docs for the native controls, *
but if you're interested you could do so yourself. *I suspect that if you *
do, you'll find the same difference there that you are seeing in .NET. *
The difference in .NET likely exists only because it's different in the *
underlying control.
Pete- Hide quoted text -

- Show quoted text -
I agree 100%!
I too must have pee'd in his (duniho) cheerios, because when he
responsed to my thread (which, believe it or not, was better advice
than his for the poor sap who needed it), it sounded like his
programmer ego was crushed. I only wish I had the time he does to
respond to as many threads as he does.
Dec 22 '07 #10

P: n/a
He really DOES have an ego problem. Plus - no social life. He spends all his
time on threads and just pisses people off.

His advice should be taken with a grain of salt --- and a stiff scotch!

"Greg" wrote:
On Dec 21, 8:07 am, vbtrying <vbtry...@discussions.microsoft.com>
wrote:
Good 'ol Peter Duniho ....

Just like before, nobody ever puts enough information into their question to
suit you.
Just TRY to answer the question and be nice about it.

"Peter Duniho" wrote:
On Mon, 17 Dec 2007 12:30:34 -0800, Dom <dolivas...@gmail.comwrote:
But even if they wanted to wrap up the old controls, why couldn't we
get something that allowed the following, which (I think) is more in
keeping with the usual approach for controls;
int n = ListBox.Items.Add ();
ListBox.Items[n].Text = <a string>;
ListBox.Items[n].Tag = <an object// this is optional
I think you're thinking that the above code doesn't do anything using the
old controls. But I believe it does. And inasmuch as the native Windows
control Listbox is different from the native Windows control list view, so
too will the .NET control be different.
I don't have time right now to go review the docs for the native controls,
but if you're interested you could do so yourself. I suspect that if you
do, you'll find the same difference there that you are seeing in .NET.
The difference in .NET likely exists only because it's different in the
underlying control.
Pete- Hide quoted text -
- Show quoted text -

I agree 100%!
I too must have pee'd in his (duniho) cheerios, because when he
responsed to my thread (which, believe it or not, was better advice
than his for the poor sap who needed it), it sounded like his
programmer ego was crushed. I only wish I had the time he does to
respond to as many threads as he does.
Dec 22 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.