Clone a DataBound Checked List Box

Posted by Buddhi Dananjaya on Stack Overflow See other posts from Stack Overflow or by Buddhi Dananjaya
Published on 2011-01-04T08:49:49Z Indexed on 2011/01/13 5:53 UTC
Read the original article Hit count: 477

Filed under:
|
|
|

Hi

I have a DataBound CheckedListBox, I "check" few items on list box(source), then I need to clone it to new Checked List Box(target). It need to have all the data, with checked state. I have tried with following function. It is properly flowing through this function.

But finally I can see items on target CheckedListBox but none of the items in target is checked.

 private void CloneCheckedListBox(CheckedListBox source, CheckedListBox target)
    {            
        foreach (int checkedItemIndex in source.CheckedIndices)
        {
            target.SetItemChecked(checkedItemIndex, true);
        }
    }

Edit:

I have a User control which I have placed on a TabPage, on that User Control there is a "CheckedListBox", I do need to create a new TabPage with the user entered value on selected(current) TabPage(on User Control)

So, what I have done is, create a new Tab Page, get a Copy of the User Control calling it's "Clone()" method.

In "Clone()" method need to have CheckedListBox cloning feature.

Here is my Cloning Code, which is on User Control...

 public SearchMain Clone()
    {
        SearchMain smClone = new SearchMain();
        smClone.txtManufacturers.Text = this.txtManufacturers.Text;
        smClone.udPriceFrom.Value = this.udPriceFrom.Value;
        smClone.udPriceTo.Value = this.udPriceTo.Value;
        smClone.chkOld.Checked = this.chkOld.Checked;
        smClone.chkPrx.Checked = this.chkPrx.Checked;
        smClone.chkDisc.Checked = this.chkDisc.Checked;
        smClone.chkStock.Checked = this.chkStock.Checked;
        smClone.chkFirstDes.Checked = this.chkFirstDes.Checked;
        smClone.chkFirstPN.Checked = this.chkFirstPN.Checked;
        smClone.txtSuppPN.Text = this.txtSuppPN.Text;
        smClone.txtManuPN.Text = this.txtManuPN.Text;
        smClone.txtManufacturers.Text = this.txtManufacturers.Text;
        smClone.meDesAND.Text = this.meDesAND.Text;
        smClone.meDesOR.Text = this.meDesOR.Text;
        smClone.meDesNOT.Text = this.meDesNOT.Text;
        smClone.lbManufacSelected.Items.AddRange(this.lbManufacSelected.Items);
        smClone.lbSearchWithIn.Items.AddRange(this.lbSearchWithIn.Items);
        **CloneCheckedListBox(this.clbLang, smClone.clbLang);**
       // CloneCheckedListBox(this.clbTypes, smClone.clbTypes);
        return smClone;
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET