0

I need to retrieve the text from a textbox within a custom control. The custom control is part of an ArrayList so there can be multiple custom controls displayed on a Form. How do I access the text from a single textbox within one of the controls in the ArrayList?

The code below shows how I'm creating the dynamic custom control:

    ArrayList assessmentInfo = new ArrayList();

    int length = (int)moduleInfoLevel6.numericUpDownModuleAssessmentNum.Value;
    for (int i = 0; i < length; i++)
    {
        assessmentInfo.Add(new AssessmentInfo());
        System.Drawing.Point p = new System.Drawing.Point(10, 160 + i * 32);
        (assessmentInfo[i] as AssessmentInfo).Location = p;
        (assessmentInfo[i] as AssessmentInfo).Size = new System.Drawing.Size(440, 32);                
        tabPageLevel6.Controls.Add((assessmentInfo[i] as AssessmentInfo));
    }

Here are screenshots showing how the custom control is displayed:

assessmentInfo custom control

https://i.stack.imgur.com/EjbRw.jpg

How the custom control is displayed on the form

https://i.stack.imgur.com/1hm1A.jpg

Plummy194
  • 3
  • 1
  • I have been in the same situation. The approach I had taken was bind the class element with the custom control and you can do that while creating an instance. Once the binding is done, all you have care about is data structure. – mchicago Apr 25 '12 at 16:10

1 Answers1

0

you could add a name to each control

something like:

(assessmentInfo[i] as AssessmentInfo).Name = "assessmentInfo" + i.ToString();

then you could access it in the following way

foreach (object control in tabPageLevel6.Controls)
{
    if (control is AssessmentInfo)
    {
        if ((control as AssessmentInfo).Name == "assessmentInfo1")
            // do something with the control
            MessageBox.Show((control as AssessmentInfo).Name);                    
    }
}
fuchs777
  • 993
  • 1
  • 14
  • 30