Referring to controls on other forms

  • Thread starter Thread starter Mark Rae
  • Start date Start date
Dinesh,
If you load frmNewApp form with below code in cmdNewApp's click event;

frmNewApp form2 = new frmNewApp();
form2.ShowDialog(this);

you can access controls in frmConfigReg form like below.

//if you know the index id
ComboBox cb = (ComboBox) ((frmConfigReg )Owner).Controls[1];

//OR

ControlCollection cc = (ControlCollection) ((frmConfigReg)Owner).Controls;
foreach(Control c in cc)
{
if (c.Name=="cmbApps")
{
//get values and validate
}
}

hope this is what you want

That has certainly worked - thanks very much. I'm quite surprised, though,
that it is necessary to reference controls in the ControlCollection by their
index rather than their name - can the FindControl method be used here...?

Best,

Mark
 
Hi Mark,

One simple way to accomplish this is to create a constructor for
the frmNewApp form that accepts as a parameter a reference to the
cmbApps object.

Add an internal ComboBox that has a private scope into frmNewApp
(objComboBoxNewToThisForm)


frmNewApp (ref ComboBox objComboReferencedFromMainForm):this()
{
this.objComboBoxNewToThisForm = objComboReferencedFromMainForm;
}

Now you can work with the objComboBoxNewToThisForm as normal -- it
references the ComboBox passed in objReferencedFromMainForm.

When creating a new form in your button click event -- open the
constructor indicated above rather than just new frmNewApp().

Hope this helps.
 
Back
Top