Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling Child Controls

Status
Not open for further replies.

grimace

Technical User
Feb 14, 2001
7
GB
Hi,
I have a problem referencing the controls in a child class and as I am fairly new to c# I am not sure whether it is actually possible to do what I want to do..phew!! Basically I have a standard dialog that has apply/ok/cancel buttons. It also loads the data from the db depending on what textboxes appear on the form. However I would like to load these textboxes via child classes so that I can have 'StandardDialog' do all the work and have a bunch of custom dialogs that just have textboxes on them. Everything seems ok except that when i search through the controls it only finds the ones on the parent form. Can i reference the ones on the child class? Here is the code.

Code:
class StandardDialog : System.Windows.Forms.Form 
{ 
   public StandardDialog() 
   { 
     InitializeComponent(); 
     LoadData() 
   } 

   private void InitializeComponent() 
   { 
     // Ok,Cancel & Apply buttons here 
   } 

   protected void LoadData() 
   { 
     foreach (Control c in Controls) 
     { 
        //Load data from db 
     } 
   } 
} 


class SiteDialog : StandardDialog 
{ 
   public SiteDialog(): base() 
   { 
      InitializeComponent(); 
   } 

   private void InitializeComponent() 
   { 
     // Textboxes loaded here 
   } 
}

Hope someone can help!!

thanks, Martin
 
You have to pass the control objects from the child to the parent first.

In the child, create a function:

Code:
public ArrayList GiveParentControls()
{
    ArrayList list = new ArrayList();
    list.Add(textbox1);  //an instantiated textbox
    list.Add(textbox2);  //an instantiated textbox
    return list;         //return the arraylist of objects
}

And then in the parent, call the function:

Code:
ArrayList childControls = GiveParentControls();

You can now access all the controls contained in the ArrayList:

Code:
//Get text from first child text box:
//Don't forget to cast the object to a textbox type!
if((TextBox)childControls[0]) != null)
{
    //Do something to the textbox here...
    string myText = ((TextBox)childControls[0]).Text;
}

Now that you have access to the child's textboxes in the parent, you can do anything you want to the textboxes just as if they are actually in the parent.
 
thanks for your reply..i tried it but the parent class didn't recognise the function. With a little help i managed to sort it. Basically the problem was that I was calling the LoadData method in the constructor of the parent class.This was getting called before the child class had been initialised (hence no controls).

To solve i overrided the OnLoad method in the parent class and put the LoadData method call in there.

Thanks again for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top