ListViewItem

G

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
 
D

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
 
G

Gav

Dom said:
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
 
P

Peter Duniho

[...]
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
 
D

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



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?

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
 
P

Peter Duniho

[...]
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
 
D

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


[...]
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
 
P

Peter Duniho

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
 
V

vbtrying

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 said:
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
 
G

Greg

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 said:
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.
 
V

vbtrying

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 said:
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.



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.
 

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