Why can't I add Items to a ComboBox that is accessed using this.tableLayoutPanel1.GetControlFromPosition(i, j)

Michael Bahr 20 Reputation points
2025-01-09T17:36:50.7966667+00:00

I have created ComboBox Controls when my Form Loads and I want to add Items to them later when the user makes specific selections but, I cannot use Items.Add because the compiler reports a Compiler Error CS1061 'Control' does not contain a definition for 'Items'

tableLayoutPanel1 = tblRinks

Code that creates the ComboBox:

for (var r = 2; r <= Globals.tSheets; r++)

{

tblRinks.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));

tblRinks.RowCount++;

tblRinks.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 95F));

tblRinks.Controls.Add(new Label() { Text = "Sheet " + x, Name = "lblSheet" + r, Font = new Font("Microsoft Sans Serif, 9pt, style=Bold center", 9), TextAlign = ContentAlignment.MiddleCenter }, 0, r);

//

tblRinks.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 162F));

tblRinks.Controls.Add(new ComboBox() { Name = "cboRink" + r + "A", Font = new Font("Microsoft Sans Serif, 9pt, style=Bold, align=center", 9) }, 1, r);

//

tblRinks.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 26F));

tblRinks.Controls.Add(new Label() { Text = "VS", Font = new Font("Microsoft Sans Serif, 9pt, style=Bold, align=center", 9), TextAlign = ContentAlignment.MiddleCenter }, 2, r);

//

tblRinks.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 162F));

tblRinks.Controls.Add(new ComboBox() { Name = "cboRink" + r + "B", Font = new Font("Microsoft Sans Serif, 9pt, style=Bold, align=center", 9) }, 3, r);

x++;

}

Code used to access the ComboBoxes after user selects from another ComboBox:

for (int r = 0; r < tblRinks.Controls.Count; r++)

{

Control cbo = tblRinks.Controls[r];

if (cbo is ComboBox && cbo.Name == "cboRink2B")

{

  foreach (string team in Globals.rList)

  {

    string tname = team;

    if (tname != ((Control)sender).Text && tname != cboRink1A.Text)

    {

      mList.Add(tname);

      tblRinks.Controls[r].Enabled = true;

      int idx = tblRinks.Controls[r].Handle.ToInt32();

      //MessageBox.Show(idx.ToString());

      //tblRinks.Controls[r].items.Add(tname);

    }

  }

  cbo.BackColor = Color.LightGoldenrodYellow;

  ((Control)sender).BackColor = Color.White;

  return;

}

}

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,185 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 48,606 Reputation points Microsoft Vendor
    2025-01-10T02:21:57.5666667+00:00

    Hi @Michael Bahr , Welcome to Microsoft Q&A,

    You treat the ComboBox control as a more general Control class, which does not have an Items property. This results in compiler error CS1061.

    In order to correctly add items to a ComboBox, you need to explicitly cast the Control type to the ComboBox type before you can access its Items property.

    for (int r = 0; r < tblRinks.Controls.Count; r++)
    {
        Control control = tblRinks.Controls[r];
        if (control is ComboBox comboBox && comboBox.Name == "cboRink2B")
        {
            foreach (string team in Globals.rList)
            {
                string tname = team;
    
                if (tname != ((Control)sender).Text && tname != cboRink1A.Text)
                {
                    comboBox.Items.Add(tname);
                }
            }
    
            comboBox.BackColor = Color.LightGoldenrodYellow;
            ((Control)sender).BackColor = Color.White;
    
            return;
        }
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.