RE: PLS HELP! Problem with CheckedListBox changing Parent



Hi MuZZy,

I performed a test and did see the problem you described.

When we bind a data source to a CheckedListBox, the items in the
CheckedListBox are populated with the data in the data source. If we change
the Parent property of the CheckedListBox, the items in the CheckedListBox
are cleared and re-populated with the data in the data source again. So the
information on which items are checked previously couldn't be preserved.

On the contrary, if the CheckedListBox is filled throught "Items" property,
the items in it needn't re-populating in the case of its Parent property
being changed.

I recommend you to save the indexes of those checked items to an array
before you change the Parent property of the CheckedListBox and restore the
previously checked items later.

The following is a sample. This requires you to add two forms named Form1
and Form2 to a WinForms application project and add a CheckedListBox named
checkedListBox1 and a Button named button1 onto the Form1. When we click
the button1, we change the Parent property of the checkedListBox1 to Form2.

using System.Collections.Generic;

private void button1_Click(object sender, EventArgs e)
{
CheckedListBox.CheckedItemCollection chcol =
this.checkedListBox1.CheckedItems;
List<int> checkedList = new List<int>();
for (int i = 0; i < this.checkedListBox1.CheckedItems.Count;
i++)
{

checkedList.Add(this.checkedListBox1.Items.IndexOf(this.checkedListBox1.Chec
kedItems[i]));
}

Form2 frm = new Form2();

this.checkedListBox1.Parent = frm;

for (int i = 0; i < checkedList.Count; i++)
{
this.checkedListBox1.SetItemChecked(checkedList[i], true);
}

frm.Show();
}



Hope this helps.
If you have anything unclear, please feel free to tell me.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

.