克隆 DataBound 选中列表框

发布于 2024-10-09 22:26:49 字数 1978 浏览 0 评论 0原文

我有一个 DataBound CheckedListBox,我“检查”列表框(源)上的几个项目,然后我需要将其克隆到新的选中列表框(目标)。它需要拥有所有数据,并具有检查状态。我尝试过以下功能。它正确地流经此函数。

但最后我可以看到目标 CheckedListBox 上的项目,但目标中的任何项目都没有被检查。

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

编辑:

我有一个放置在 TabPage 上的用户控件,在该用户控件上有一个“CheckedListBox”,我确实需要创建一个新的 TabPage,其中用户在所选(当前)TabPage(在用户控件上)上输入了值

所以,我所做的是,创建一个新的选项卡页,获取用户控件的副本,调用它的“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;
    }

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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏夜暖风 2024-10-16 22:26:49

您可以在这里看到正确的答案。

以编程方式检查 DataBound CheckListBox

You can see correct answere here..

Programatically Checking DataBound CheckListBox

若相惜即相离 2024-10-16 22:26:49

尝试设置

source.DataSource = target.DataSource;
target.DisplayMember = "YourDisplayItem";
target.ValueMember = "YourValueItem";
foreach (int checkedItemIndex in source.CheckedIndices)
{
     target.SetItemChecked(checkedItemIndex, true);
}

try set

source.DataSource = target.DataSource;
target.DisplayMember = "YourDisplayItem";
target.ValueMember = "YourValueItem";
foreach (int checkedItemIndex in source.CheckedIndices)
{
     target.SetItemChecked(checkedItemIndex, true);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文