Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / August 2006

Tip: Looking for answers? Try searching our database.

PLS HELP! Problem with CheckedListBox changing Parent

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MuZZy - 26 Aug 2006 21:15 GMT
Hi,

I hope someone could help me here: I have a CheckedListBox control (i'm
using .NET 2.0 + VS 2005) which is given a DataSource:

clbTest.DataSource = SomeDataTable;
clbTest.DisplayMember = "SomeDisplayField";
clbTest.ValueMember = "SomeValueField";

Now the control populates items from that table and i can check/uncheck
them.
The problem comes when i change "Parent" property of the control, say
move it to a different form - then all previousely checked items become
unchecked which is my problem. If the CheckedListBox control was filled
thru "Items" property instead of DataSource, that problem doesn't occur.

I am really eager for an answer as it really holds me back and causes
problems in the app.

I would appreciate any ideas/suggestions in this matter!

Thank you in advance,
MuZZy
Linda Liu [MSFT] - 28 Aug 2006 07:38 GMT
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.
MuZZy - 29 Aug 2006 00:47 GMT
Hi Linda,

Thanks for the reply. I am using CheckedListBox as a parent for my
custom control, so this control has no idea about any forms or
whatsoever. I can only catch OnParentChanged event to do stuff.

But, the problem is that it seems like it repopulates items after it
fires ParentChanged because inside an event handler for the event i
still see those items checked.

Any ideas?

Thank you,
Andrey

> Hi MuZZy,
>
[quoted text clipped - 72 lines]
>  
> This posting is provided "AS IS" with no warranties, and confers no rights.
Linda Liu [MSFT] - 29 Aug 2006 04:20 GMT
Hi Andrey,

Yes, you are right. The items is re-populated after the ParentChanged event
is fired. So, we should restore the checked item after the ParentChanged
event is fired.

In your senario, you are using a derived CheckedListBox. It's better to do
the things in the overridden OnParentChanged method. Remember to place the
code for saving the indexes of the checked items before the code
"base.OnParentChanged(e)" and place the code for restoring the previously
checked items after this code.

Below is a sample.

using System.Collections.Generic;
using System.Windows.Forms;

class CheckedListBoxEx:CheckedListBox
   {
       protected override void OnParentChanged(EventArgs e)
       {
          // save the indexes of the checked items before the
ParentChanged event is fired

           CheckedListBox.CheckedItemCollection chcol = this.CheckedItems;
           List<int> checkedList = new List<int>();
           for (int i = 0; i < this.CheckedItems.Count; i++)
           {
               checkedList.Add(this.Items.IndexOf(this.CheckedItems[i]));
           }
           
            base.OnParentChanged(e);

           // restore the previously checked items after the ParentChanged
event is fired

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

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

Sincerely,
Linda Liu
Microsoft Online Community Support
MuZZy - 29 Aug 2006 17:29 GMT
It's not true - even after call to "base.OnParentChanged(e);"  the
checkedlistbox still keeps the items checked. They get unchecked after
it leaves the MyCheckedListBox.OnParentChanged so i can't catch it.

Any ideas?

> Hi Andrey,
>
[quoted text clipped - 45 lines]
> Linda Liu
> Microsoft Online Community Support

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.