为什么一个comboBox中的项目不能复制到另一个?

时间:2014-05-23 17:52:02

标签: c# dynamic combobox user-controls rtti

我在表单上的tabcontrol上的tabpage页面上有多个组合框。试图循环控制没有效果(参见this)。

所以,我试图从另一个角度去看它:根据他们的名字找到控件。作为一个初始的POC,我只是想通过提供一个在设计时为空的组合框的名称(“cmbxRow0Element1”)来强制它,并将cmbxRow0Element0中的项目分配给它。但这两种尝试都是:

Control ctrl = this.Controls["cmbxRow0Element1"];
ComboBox cmbx = ctrl as ComboBox;
var items = cmbxRow0Element0.Items.OfType<object>().ToArray();
cmbx.Items.Add(items);

......而且这一个:

Control ctrl = this.Controls["cmbxRow0Element1"];
ComboBox cmbx = ctrl as ComboBox;
foreach (Object item in cmbxRow0Element0.Items)
{
    cmbx.Items.Add(item);
}

...导致“ System.NullReferenceException未处理   _HResult = -2147467261   _message =未将对象引用设置为对象的实例。

...在调用cmbx.Items.Add()

为什么???

我希望它最终成为:

string cmbxName;
int cmbxCount = getCountOfComboBoxes();
for (int i = 0; i < cmbxCount; i++)
{
    cmbxName = string.Format("cmbxRow0Element{0}", i);
    Control ctrl = this.Controls[cmbxName];
    ComboBox cmbx = ctrl as ComboBox;
    cmbx.Items.Add("Twain");
    cmbx.Items.Add("Steinbeck");
    cmbx.Items.Add("Saroyan");
    cmbx.Items.Add("Frost");
    cmbx.Items.Add("Hardy");
    cmbx.Items.Add("Stegner");
}

2 个答案:

答案 0 :(得分:2)

因为cmbxRow0Element1不是Form的直接子元素。使用NameOfYourTabControl.Controls["cmbxRow0Element1"],或者更常见的是:

this.Controls.SelectMany(x => x.Controls).First(x => x.Name == "cmbxRow0Element1");

答案 1 :(得分:0)

这是一项正在进行的工作,但它具有功能性:

string cmbxName;
int cmbxCount = getCountOfComboBoxes();
for (int i = 0; i < cmbxCount; i++)
{
    cmbxName = string.Format("cmbxRow0Element{0}", i);
    Control ctrl = this.tabPage1.Controls[cmbxName];
    ComboBox cmbx = ctrl as ComboBox;
    cmbx.Items.Add("Christopher Robbin");
    cmbx.Items.Add("Eeyore");
    cmbx.Items.Add("Kanga");
    cmbx.Items.Add("Owl");
    cmbx.Items.Add("Piglet");
    cmbx.Items.Add("Rabbit");
    cmbx.Items.Add("Roo");
    cmbx.Items.Add("Tigger (T-I-Double Guh-Er)");
    cmbx.Items.Add("Winnie-the-Pooh");
}

最初的问题是这个/表单看不到标签页上的内容;甚至选项卡Control也没有。我必须具体使用特定的标签* Page *才能使用。

更新

这是一个更好的版本 - 仍然可以使用空检查和其他细节:

// Names of comboboxes are of the pattern "cmbxRowNElementN" the first N runs from 0..11, the second from 0..5
// This assume that you really do have twelve tabPages named tabPageRow0...tabPageRow11, and that there are six comboboxes on each tabpage 
const int TABPAGE_COUNT = 12;
const int COMBOXES_PER_TABPAGE = 6;
string tabPageBaseName = "tabPageRow";
List<String> MilneMilieu = new List<string>() { "Christopher Robin", "Eeyore", "Kanga", "Owl", "Piglet", "Rabbit", "Roo", "Tigger (T, I, Double-Guh, Er)", "Winnie-the-Pooh" };
string tabPageName;
string cmbxName;

try
{
    for (int i = 0; i < TABPAGE_COUNT; i++)
    {
        tabPageName = string.Format("{0}{1}", tabPageBaseName, i);
        Control tabpageCtrl = this.tabControl1.Controls[tabPageName];
        TabPage tp = tabpageCtrl as TabPage;

        for (int j = 0; j < COMBOXES_PER_TABPAGE; j++)
        {
            cmbxName = string.Format("cmbxRow{0}Element{1}", i, j);
            Control cmbxCtrl = tp.Controls[cmbxName];
            ComboBox cmbx = cmbxCtrl as ComboBox;
            // While we're at it, hook it to the shared event handler (although doing this violates the "S" in SOLID)
            cmbx.SelectionChangeCommitted += cmbxRow0Element0_SelectionChangeCommitted;
            foreach (var imaginaryFriend in MilneMilieu)
            {
                cmbx.Items.Add(imaginaryFriend);
            }
        }
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}