question

programmercnetaspnet-1305 avatar image
0 Votes"
programmercnetaspnet-1305 asked programmercnetaspnet-1305 commented

How can i refere to runtime created ComboBox in c#

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

dotnet-csharpwindows-formsdotnet-runtime
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered programmercnetaspnet-1305 commented

@programmercnetaspnet-1305

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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you very much for this solution ,it worked perfectly.

0 Votes 0 ·