我有一个 DataBound CheckedListBox,我“检查”列表框(源)上的几个项目,然后我需要将它克隆到新的选中列表框(目标)。它需要拥有所有数据,并处于选中状态。我尝试过以下功能。它正确地流过这个函数。
但最后我可以看到目标 CheckedListBox 上的项目,但没有检查目标中的任何项目。
private void CloneCheckedListBox(CheckedListBox source, CheckedListBox target)
{
foreach (int checkedItemIndex in source.CheckedIndices)
{
target.SetItemChecked(checkedItemIndex, true);
}
}
编辑:
我有一个放置在 TabPage 上的用户控件,在该用户控件上有一个“CheckedListBox”,我确实需要创建一个新的 TabPage,用户在 selected(current) TabPage(on User Control) 上输入值
所以,我所做的是,创建一个新的标签页,获取用户控件的副本,调用它的“Clone()”方法。
在“Clone()”方法中需要有 CheckedListBox 的克隆功能。
这是我的克隆代码,位于用户控制...
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;
}