简体   繁体   English

如何以编程方式复制组合框内容?

[英]How can I copy combobox contents programmatically?

I've got this code: 我有这个代码:

private void FormMain_Shown(object sender, EventArgs e)
{
    ComboBox cmbx;
    foreach (Control C in this.Controls)
    {
        if (C.GetType() == typeof(ComboBox))
        {
            cmbx = C as ComboBox;
            //cmbx.Items.AddRange(cmbxRow0Element0.Items); <= illegal
            object[] obj = new object[cmbxRow0Element0.Items.Count];
            cmbxRow0Element0.Items.CopyTo(obj, 0);
            cmbx.Items.AddRange(obj);
        }
    }
}

...but it doesn't work - I have several combo boxes on a tabPage on a tabControl on a Form, with cmbxRow0Element0 populated with items at design time. ...但它不起作用 - 我在窗体上的tabControl上的tabPage上有几个组合框,cmbxRow0Element0在设计时填充了项目。 The above attempt to copy its items to all the other comboboxes fails, though. 但是,上述将其项目复制到所有其他组合框的尝试失败了。

UPDATE UPDATE

This is the code I now have, and it still doesn't work: 这是我现在的代码,它仍然不起作用:

public FormMain()
{
    InitializeComponent();

    for (int i = 0; i < 10; i++)
    {
        this.cmbxRow0Element0.Items.Add(String.Format("Item {0}", i.ToString()));
    }
    foreach (Control C in this.Controls)
    {
        ComboBox cmbx = null;

        // The & test ensures we're not just finding the source combobox
        if ((C.GetType() == typeof(ComboBox)) & ((C as ComboBox) != this.cmbxRow0Element0))
            cmbx = C as ComboBox;
        if (cmbx != null)
        {
            foreach (Object item in cmbxRow0Element0.Items)
            {
                cmbx.Items.Add(item);
            }
        }

    }
}

Perhaps it's something to do with the comboboxes being on tab pages on the tab control? 也许这与组合框控件上的标签页上的组合框有关?

UPDATE 2 更新2

A breakpoint on the first line below is reached, but the second line is never reached: 达到下面第一行的断点,但永远不会达到第二行:

if ((C.GetType() == typeof(ComboBox)) & ((C as ComboBox) != this.cmbxRow0Element0))
   cmbx = C as ComboBox;

UPDATE 3 更新3

The problem was the "find" was not being specific enough - the particular tabPage has to be specified. 问题是“查找”不够具体 - 必须指定特定的tabPage。 For more details, see this . 有关详细信息,请参阅此处

Try this: 尝试这个:

var items = cmbxRow0Element0.Items.OfType<object>().ToArray();

foreach (ComboBox c in this.Controls.OfType<ComboBox>())
{
     c.Items.AddRange(items);
}

If you are using tabControl as a container, then it means your comboboxes aren't direct child element of your Form.So you need to access Control collection of the container which is the tabControl . 如果您使用tabControl作为容器,则表示您的组合框不是Form的直接子元素。因此,您需要访问容器的Control集合,即tabControl You can do that using this.NameOfYourTabControl.Controls . 你可以使用this.NameOfYourTabControl.Controls来做到这this.NameOfYourTabControl.Controls

A quick test Windows Form app (VS Express 2012) shows this works. 快速测试Windows窗体应用程序(VS Express 2012)显示了这一点。 In a new blank Windows Form app, drop three (or more) ComboBox controls: 在新的空白Windows窗体应用程序中,删除三个(或更多)ComboBox控件:

    public Form1()
    {
        InitializeComponent();
        for (int i = 0; i < 10; i++)
        {
            this.comboBox1.Items.Add(String.Format("Item {0}", i.ToString()));
        }
        foreach (Control C in this.Controls)
        {
            ComboBox cmbx = null;

            // The & test ensures we're not just finding the source combobox
            if ((C.GetType() == typeof(ComboBox)) & ((C as ComboBox) != this.comboBox1))
                cmbx = C as ComboBox;
            if (cmbx != null)
            {
                foreach (Object item in comboBox1.Items)
                {
                    cmbx.Items.Add(item);
                }
            }

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM