473,396 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ListViewItem

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
10 3494
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: LV | last post by:
Hello, I would like to manually set one of my list view items as selected. When this item is set, I would like for a method to execute. I have a delegate on the list view for SelectedIndexChanged....
0
by: afatdog | last post by:
I want to change the ListViewItem's StateImageIndex to change. private void Button_xx_Click(object sender, System.EventArgs e) { ListViewItem.StateImageIndex++; } But the ListViewItem's...
2
by: Max Adams | last post by:
Question about using delegates with a string paramater to return a ListViewItem object... All, I have a thread and I want this thread to post messages to the main GUI thread using a delegate....
6
by: grs | last post by:
The following is a code example from the Microsoft MSDN. My question is on the following three lines of code: ListViewItem item1 = new ListViewItem("item1",0); ListViewItem item2 = new...
0
by: Kluch | last post by:
I am trying to select and focus single ListViewItem and can't seem to do so, here is my code: IEnumerator * itemList = mLsvMyList->Items->GetEnumerator(); while (itemList->MoveNext()) {...
1
by: Alan T | last post by:
I have a listview defined a coloumn at design time. And the code I use to add a listviewitem: ListViewItem lvi = new ListViewItem(user.Name); lvi.Tag = (Object)user.Id; ...
2
by: Kela | last post by:
An interesting problem: I have a ListView with LabelEdit set to TRUE. When I change the label, I want to make some decisions as to whether the ListViewItem (that's just been edited) should stay in...
0
by: garyusenet | last post by:
I am trying to create a form that will display a list of open internet explorer windows and allow the user to choose one of them. So far I have created an arraylist which contains all of the open...
13
by: deciacco | last post by:
How can I have access to the items collection of a listview control on my form from a background thread? I know I need delegates to update the listview control and I have those calls in the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.