如何以编程方式复制组合框内容? [英] How can I copy combobox contents programmatically?

查看:158
本文介绍了如何以编程方式复制组合框内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

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

...工作 - 我有一个tabPage上的几个组合框在表单上的tabControl,cmbxRow0Element0填充项目在设计时。上述尝试将其项目复制到所有其他组合框失败了。



UPDATE



代码我现在有,它仍然不工作:

  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;

//&测试确保我们不只是找到源组合框
if((C.GetType()== typeof(ComboBox))&((C as ComboBox)!= this.cmbxRow0Element0))
cmbx = C as ComboBox;
if(cmbx!= null)
{
foreach(cmbxRow0Element0.Items中的对象项)
{
cmbx.Items.Add(item);
}
}

}
}





UPDATE 2



p>到达第一行的断点,但第二行未到达:

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



UPDATE 3



问题是find不够具体 - 必须指定特定的tabPage。有关详情,请参见

解决方案

快速测试Windows窗体应用程序(VS Express 2012)显示了这一工作。在新的空白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;

//&测试确保我们不只是找到源组合框
if((C.GetType()== typeof(ComboBox))&((C as ComboBox)!= this.comboBox1))
cmbx = C as ComboBox;
if(cmbx!= null)
{
foreach(comboBox1.Items中的对象项)
{
cmbx.Items.Add(item);
}
}

}
}


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. The above attempt to copy its items to all the other comboboxes fails, though.

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

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

The problem was the "find" was not being specific enough - the particular tabPage has to be specified. For more details, see this.

解决方案

A quick test Windows Form app (VS Express 2012) shows this works. In a new blank Windows Form app, drop three (or more) ComboBox controls:

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

        }
    }

这篇关于如何以编程方式复制组合框内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆