Tag property of List View Item

  • Thread starter Thread starter Jax
  • Start date Start date
J

Jax

I understand that it's supposed to contain text, usually.
But as it takes an object can you be really cheeky and put
a very complex object in there instead?
Would any problems come from doing that?

jax
 
hmmm I dunno, it's giving me grief.
Seems to turning it into a value rather then a reference
or something.
I change other references to the same object but it stays
the same???
 
Hi,

Tag is an object reference thus it stores the reference and does more
nothing to it.
What exactly is your problem?
Can you give us an example?
 
Thanks for your attention Miha,

Okay i'll try to keep it brief:

Imagine a form with a listview on the left.
In the background of the form there is an objerct called
theCase of custom type Case.
This contains an arraylist of references to all customer
objects.
The list view on the left represent all the customers, if
you click add customer this happens.

private void btnAddApplicant_Click(object sender,
System.EventArgs e)
{
ready = true;
ctr++;
Customer c = new Customer();
c.PersonalDetails.FirstName = "Name";
c.PersonalDetails.SecondName = "Me" + ctr.ToString();
theCase.Customers.Add(c);
ListViewItem lvi = new ListViewIte
(c.PersonalDetails.FirstName + " " +
c.PersonalDetails.SecondName, 0);
lvi.Tag = (object)c;
lvi.Selected = true;
this.lstCustomers.Items.Add(lvi);
}
So I put the reference into theCase as well as the Tag of
the listview.

If the selected index of the listview changes this happens.

if(this.lstCustomers.SelectedItems != null)
{
if(this.lstCustomers.SelectedItems.Count>0)
{
if(ready)
{
this.txtPersonalC1FirstName.Focus();
SaveCustomer();
theCustomer = (Customer) lstCustomers.SelectedItems[0].Tag;
this.PopulateFromCustomer(theCustomer);
foreach(ListViewItem listItems in lstCustomers.Items)
{
listItems.ImageIndex = 0;
}
lstCustomers.SelectedItems[0].ImageIndex = 1;
if(theCase.Customers.Count<2)
{
this.btnCopyAddresses.Visible = false;
this.btnCopyDependants.Visible = false;
}
else
{
this.btnCopyAddresses.Visible = true;
this.btnCopyDependants.Visible = true;
}
CreateSelfCertTable();
RefreshCopyCombos();
}
CreateSelfCertTable();
RefreshCopyCombos();
}
}
}

Save Customer does this.

private void SaveCustomer()
{
foreach(ListViewItem lvi in this.lstCustomers.Items)
{
if(lvi.ImageIndex == 1)
{
theCustomer = this.SaveCustomerFromForm();
lvi.Text = theCustomer.PersonalDetails.FirstName + " " +
theCustomer.PersonalDetails.SecondName;
lvi.Tag = (object)theCustomer;
break;
}
}
}

Do you remember at the top when I created the objects I
put a refrence in theCase custom type of Case.
Thing is is that I pass this object to my copyCommand form
and it has customers with their inital names ("Name Me1")
rather then the names i've put on since then.

With the load and the save method i have twined in with
the selected item changed event I can see that I am making
changes and these are being registered with the Customer
object as they are loading out all the details corrctly.

Any idea why theCase has outofdate references?
If you've got this far I thank you for your patience. :)
jax
 
HI Jax,

I could be wrong, though (near the end):
theCustomer = this.SaveCustomerFromForm();
lvi.Text = theCustomer.PersonalDetails.FirstName + " " +
theCustomer.PersonalDetails.SecondName;
lvi.Tag = (object)theCustomer;

You are assigning new customer to tag while theCase still has old reference
(to old customer).
Is this what you are missing?

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

Jax said:
Thanks for your attention Miha,

Okay i'll try to keep it brief:

Imagine a form with a listview on the left.
In the background of the form there is an objerct called
theCase of custom type Case.
This contains an arraylist of references to all customer
objects.
The list view on the left represent all the customers, if
you click add customer this happens.

private void btnAddApplicant_Click(object sender,
System.EventArgs e)
{
ready = true;
ctr++;
Customer c = new Customer();
c.PersonalDetails.FirstName = "Name";
c.PersonalDetails.SecondName = "Me" + ctr.ToString();
theCase.Customers.Add(c);
ListViewItem lvi = new ListViewIte
(c.PersonalDetails.FirstName + " " +
c.PersonalDetails.SecondName, 0);
lvi.Tag = (object)c;
lvi.Selected = true;
this.lstCustomers.Items.Add(lvi);
}
So I put the reference into theCase as well as the Tag of
the listview.

If the selected index of the listview changes this happens.

if(this.lstCustomers.SelectedItems != null)
{
if(this.lstCustomers.SelectedItems.Count>0)
{
if(ready)
{
this.txtPersonalC1FirstName.Focus();
SaveCustomer();
theCustomer = (Customer) lstCustomers.SelectedItems[0].Tag;
this.PopulateFromCustomer(theCustomer);
foreach(ListViewItem listItems in lstCustomers.Items)
{
listItems.ImageIndex = 0;
}
lstCustomers.SelectedItems[0].ImageIndex = 1;
if(theCase.Customers.Count<2)
{
this.btnCopyAddresses.Visible = false;
this.btnCopyDependants.Visible = false;
}
else
{
this.btnCopyAddresses.Visible = true;
this.btnCopyDependants.Visible = true;
}
CreateSelfCertTable();
RefreshCopyCombos();
}
CreateSelfCertTable();
RefreshCopyCombos();
}
}
}

Save Customer does this.

private void SaveCustomer()
{
foreach(ListViewItem lvi in this.lstCustomers.Items)
{
if(lvi.ImageIndex == 1)
{
theCustomer = this.SaveCustomerFromForm();
lvi.Text = theCustomer.PersonalDetails.FirstName + " " +
theCustomer.PersonalDetails.SecondName;
lvi.Tag = (object)theCustomer;
break;
}
}
}
 
Jax,

1st) you do not need to cast to an (Object) when you assign a tag.
Everything is an object.

2nd) I've had discrepencies with putting objects on to tags before. The
best way I've found is to assign the tag everytime the object changes.

Hope this helps

Marco

Jax said:
Thanks for your attention Miha,

Okay i'll try to keep it brief:

Imagine a form with a listview on the left.
In the background of the form there is an objerct called
theCase of custom type Case.
This contains an arraylist of references to all customer
objects.
The list view on the left represent all the customers, if
you click add customer this happens.

private void btnAddApplicant_Click(object sender,
System.EventArgs e)
{
ready = true;
ctr++;
Customer c = new Customer();
c.PersonalDetails.FirstName = "Name";
c.PersonalDetails.SecondName = "Me" + ctr.ToString();
theCase.Customers.Add(c);
ListViewItem lvi = new ListViewIte
(c.PersonalDetails.FirstName + " " +
c.PersonalDetails.SecondName, 0);
lvi.Tag = (object)c;
lvi.Selected = true;
this.lstCustomers.Items.Add(lvi);
}
So I put the reference into theCase as well as the Tag of
the listview.

If the selected index of the listview changes this happens.

if(this.lstCustomers.SelectedItems != null)
{
if(this.lstCustomers.SelectedItems.Count>0)
{
if(ready)
{
this.txtPersonalC1FirstName.Focus();
SaveCustomer();
theCustomer = (Customer) lstCustomers.SelectedItems[0].Tag;
this.PopulateFromCustomer(theCustomer);
foreach(ListViewItem listItems in lstCustomers.Items)
{
listItems.ImageIndex = 0;
}
lstCustomers.SelectedItems[0].ImageIndex = 1;
if(theCase.Customers.Count<2)
{
this.btnCopyAddresses.Visible = false;
this.btnCopyDependants.Visible = false;
}
else
{
this.btnCopyAddresses.Visible = true;
this.btnCopyDependants.Visible = true;
}
CreateSelfCertTable();
RefreshCopyCombos();
}
CreateSelfCertTable();
RefreshCopyCombos();
}
}
}

Save Customer does this.

private void SaveCustomer()
{
foreach(ListViewItem lvi in this.lstCustomers.Items)
{
if(lvi.ImageIndex == 1)
{
theCustomer = this.SaveCustomerFromForm();
lvi.Text = theCustomer.PersonalDetails.FirstName + " " +
theCustomer.PersonalDetails.SecondName;
lvi.Tag = (object)theCustomer;
break;
}
}
}

Do you remember at the top when I created the objects I
put a refrence in theCase custom type of Case.
Thing is is that I pass this object to my copyCommand form
and it has customers with their inital names ("Name Me1")
rather then the names i've put on since then.

With the load and the save method i have twined in with
the selected item changed event I can see that I am making
changes and these are being registered with the Customer
object as they are loading out all the details corrctly.

Any idea why theCase has outofdate references?
If you've got this far I thank you for your patience. :)
jax
-----Original Message-----
Hi,

Tag is an object reference thus it stores the reference and does more
nothing to it.
What exactly is your problem?
Can you give us an example?

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com




.
 
Miha,

You are right, you are awesome!
Thankyou soooooo much that's sorted EVERYTHING!
You the man!

a very happy jax.
-----Original Message-----
HI Jax,

I could be wrong, though (near the end):
theCustomer = this.SaveCustomerFromForm();
lvi.Text = theCustomer.PersonalDetails.FirstName + " " +
theCustomer.PersonalDetails.SecondName;
lvi.Tag = (object)theCustomer;

You are assigning new customer to tag while theCase still has old reference
(to old customer).
Is this what you are missing?

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

Thanks for your attention Miha,

Okay i'll try to keep it brief:

Imagine a form with a listview on the left.
In the background of the form there is an objerct called
theCase of custom type Case.
This contains an arraylist of references to all customer
objects.
The list view on the left represent all the customers, if
you click add customer this happens.

private void btnAddApplicant_Click(object sender,
System.EventArgs e)
{
ready = true;
ctr++;
Customer c = new Customer();
c.PersonalDetails.FirstName = "Name";
c.PersonalDetails.SecondName = "Me" + ctr.ToString();
theCase.Customers.Add(c);
ListViewItem lvi = new ListViewIte
(c.PersonalDetails.FirstName + " " +
c.PersonalDetails.SecondName, 0);
lvi.Tag = (object)c;
lvi.Selected = true;
this.lstCustomers.Items.Add(lvi);
}
So I put the reference into theCase as well as the Tag of
the listview.

If the selected index of the listview changes this happens.

if(this.lstCustomers.SelectedItems != null)
{
if(this.lstCustomers.SelectedItems.Count>0)
{
if(ready)
{
this.txtPersonalC1FirstName.Focus();
SaveCustomer();
theCustomer = (Customer) lstCustomers.SelectedItems [0].Tag;
this.PopulateFromCustomer(theCustomer);
foreach(ListViewItem listItems in lstCustomers.Items)
{
listItems.ImageIndex = 0;
}
lstCustomers.SelectedItems[0].ImageIndex = 1;
if(theCase.Customers.Count<2)
{
this.btnCopyAddresses.Visible = false;
this.btnCopyDependants.Visible = false;
}
else
{
this.btnCopyAddresses.Visible = true;
this.btnCopyDependants.Visible = true;
}
CreateSelfCertTable();
RefreshCopyCombos();
}
CreateSelfCertTable();
RefreshCopyCombos();
}
}
}

Save Customer does this.

private void SaveCustomer()
{
foreach(ListViewItem lvi in this.lstCustomers.Items)
{
if(lvi.ImageIndex == 1)
{
theCustomer = this.SaveCustomerFromForm();
lvi.Text = theCustomer.PersonalDetails.FirstName + " " +
theCustomer.PersonalDetails.SecondName;
lvi.Tag = (object)theCustomer;
break;
}
}
}


.
 
Back
Top