How can i refere to runtime created ComboBox in c#

programmer c# .net asp.net 21 Reputation points
2021-05-08T02:28:10.773+00:00

i have a comboBox that is been created at RunTime and i need to refere to it to retreive Data From it but i don't know how
this is the code that i create it with:

 for (int i=0;i<inArabicStudentsNames.Count();i++)
            {


                ComboBox studentsBox = new ComboBox();
                studentsBox.Location = new Point(locationComboBoxX,locationComboBoxY);
                studentsBox.Size = new Size(sizeComboBoxWidth, sizeComboBoxHeigth);
                studentsBox.Name = "students_combo" + i;
                studentsBox.Font =new Font("Times New Roman",10);

                studentsBox.Parent = this;
                listOfCombos.Add(studentsBox);
                Controls.Add(studentsBox);

                for (int j = 0; j < notFoundStudentsfromOtherTable.Count(); j++)
                    studentsBox.Items.Add(notFoundStudentsfromOtherTable[j]);

                locationComboBoxY += 30;


            }

i need to refere to it in this position

 private void button1_Click(object sender, EventArgs e)
        {
           for(int i = 0; i < inArabicStudentsNames.Count(); i++)
            {
                if(! String.IsNullOrEmpty((/* My ComboBox */).Text))
                {
                    //My Code
                }
            }
        }

if anyone could help me please

Developer technologies | Windows Forms
Developer technologies | .NET | .NET Runtime
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Duane Arnold 3,216 Reputation points
    2021-05-08T08:55:49.09+00:00

    @programmer c# .net asp.net

    You need to define the combobox at the top of the class before any methods in the class so the combobox is global on the form class and visible to all methods in the class..

    public class Form1
    {

    public Combobox cmbStudents;

    private void somemethod()
    {
    cmbStudents = new Combobox(),
    // blah blah

    }

    private void somemethod2()
    {
    cmbStudents.Name = "cmbStudents";
    }

    {

    https://doc.castsoftware.com/display/SBX/Controls+naming+convention+-+prefix%2C+case+and+character+set+control

    HTH


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.