0

I have a user defined class:

public class UserDefined
{
   public winForm svdWinForm;
   .....
}

In other place, in an instance objUserDefined of UserDefined class

{
    ...
    objUserDefined.svdWinForm=anotherWinform;
    ...
    this.Close();
}

How to find the TextBox Controls of svdWinForm within objUserDefined , an instance of winForm?

I used follows. But it is unsuccessful.

foreach (Control x in objUserDefined.svdWinForm.Controls)
{
     if (x is TextBox)
        {
            MessageBox.Show("Find a TextBox control!!");
        }
}

For instance to my question, I want to loop through textbox or other controls and set two objects equal as as following. My question is how to simplify the codes with foreach loops.

/// <summary>
/// Copy a CylinderDimension form to another form
/// </summary>
/// <param name="tgCylinderDimensionsWin">Target form</param>
/// <param name="srCylinderDimensionsWin">Source form</param>
public CopyCylinderDimensionsWin(CylinderDimensionsWin tgCylinderDimensionsWin, CylinderDimensionsWin srCylinderDimensionsWin)
{
    tgCylinderDimensionsWin.rdInnerDiameter.Checked = srCylinderDimensionsWin.rdInnerDiameter.Checked;
    tgCylinderDimensionsWin.rdOuterDiameter.Checked = srCylinderDimensionsWin.rdOuterDiameter.Checked;
    tgCylinderDimensionsWin.txtShellDiameter.Text = srCylinderDimensionsWin.txtShellDiameter.Text;
    tgCylinderDimensionsWin.txtShellLength.Text = srCylinderDimensionsWin.txtShellLength.Text;
    tgCylinderDimensionsWin.txtShellThickness.Text = srCylinderDimensionsWin.txtShellThickness.Text;
    tgCylinderDimensionsWin.txtNumberAdd.Text = srCylinderDimensionsWin.txtNumberAdd.Text;
    tgCylinderDimensionsWin.chTrialLength.Checked = srCylinderDimensionsWin.chTrialLength.Checked;
    tgCylinderDimensionsWin.txtTrialLength.Text = srCylinderDimensionsWin.txtTrialLength.Text;
    tgCylinderDimensionsWin.chExempt.Checked = srCylinderDimensionsWin.chExempt.Checked;
    tgCylinderDimensionsWin.txtTrialLength.Visible = srCylinderDimensionsWin.txtTrialLength.Visible;
    tgCylinderDimensionsWin.lbTrialLength.Visible = srCylinderDimensionsWin.lbTrialLength.Visible;
}
John Wang
  • 59
  • 8
  • 2
    What's the problem? – SLaks Jan 08 '17 at 16:39
  • SLate, the problem is there is no control found from objUserDefined.svdWinForm. Actually, there are 4 TextBox controls on the svdWinForm, an instance of winForm class embedded in UserDefined class. – John Wang Jan 08 '17 at 16:49
  • 1
    They're probably in other panels. – SLaks Jan 08 '17 at 16:52
  • you can search for something like "all winform controls" http://stackoverflow.com/questions/2735190/how-do-i-get-all-controls-of-a-form-in-windows-forms – Slai Jan 08 '17 at 16:54
  • The problem is that winForm is embedded in an userDefined class. objUserDefined is an instance of the userDifined class NOT an stance of winForm. The Controls of winForm (svdWinForm) embedded in the userDefined class cannot be found. It is weird! – John Wang Jan 08 '17 at 17:16
  • User the form's Controls.Find() method. It will properly recurse through the containers on the form. And lowers the considerable risk of finding the wrong control. – Hans Passant Jan 08 '17 at 17:19
  • does winForm class inherit System.Windows.Forms.Form class? – Aminur Rashid Jan 08 '17 at 17:23
  • Yes. the winForm embedded in UserDefined class inherited System.Windows.Forms.Form class. – John Wang Jan 08 '17 at 17:31

1 Answers1

0

I Don't Understand what you want but i hope this code solve your problem.

 public List<TextBox> GetTextbox(Form f)
    {
        List<TextBox> txtboxlist = new List<TextBox>();

        foreach (var Control in f.Controls)
        {
            if (Control.GetType() == typeof(TextBox))
            {
                txtboxlist.Add((TextBox)Control);
            }
        }

        return txtboxlist;
    }