3

I am using windows form Application, all that i have knew have tried, but cannot access Child form Control of a Parent form.

Code that i have tried till now:

this.ParentForm.Controls["PanelContainer"].Visible = false;

and

this.MdiParent.Controls["pnlContainer"].Visible = false;

and

Form myform = btnLogin.FindForm();
myform.Parent.Controls["PanelContainer"].Visible = false;

I have tried setting a public property for the Panel Control:

public Panel PanelContainer
{
    set { pnlContainer = value; }
    get { return pnlContainer; }
}

but all i am getting an exception, "Onject Reference not set to an instance of an object"

EDIT1: Here is the snapshot of My Form:

enter image description here

EDIT2: this is how I am adding the form in ContainerPanel

var login = new Login();
login.TopLevel = false;
login.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
PanelContainer.Controls.Add(login);
login.Show();
Jamshaid K.
  • 3,555
  • 1
  • 27
  • 42
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – adv12 Jun 23 '16 at 20:57
  • That was pretty helpful @adv12, I know what null reference exception is, all i want to know is how to access the Control. – Jamshaid K. Jun 23 '16 at 21:00
  • Is that Login form really an MDI child form? Looks like a centered dialog form over the main form. MDI Child forms don't behave correctly unless they have the standard sizable border, which yours does not. – LarsTech Jun 23 '16 at 21:05
  • Maybe not an Mdi form, I created an object of Form in Load and Added the control in Container Panel. with property values `TopLevel=false` and `FormBorderStyle` to none. @LarsTech – Jamshaid K. Jun 23 '16 at 21:10
  • It makes a big difference in how to solve your problem. Show your form loading code. – LarsTech Jun 23 '16 at 21:11
  • Please check **Edit2** Part. @LarsTech – Jamshaid K. Jun 23 '16 at 21:18
  • Try just `this.Parent.Visible = false;` – LarsTech Jun 23 '16 at 21:26
  • it again throws Null Reference Exception – Jamshaid K. Jun 23 '16 at 21:28
  • I'm guessing we are in the parent form then. Call `PanelContainer.Visible = false;`. Or is pnlContainer? – LarsTech Jun 23 '16 at 21:41
  • No, we are in Child Form, as after debugging, on `this` keyword it says it is Login form, which is child form @LarsTech – Jamshaid K. Jun 23 '16 at 21:44
  • From the login form, `this.Parent.Visible = false;` should work, because you don't have a parent - child form relationship, you just have a control inside another control. You have to show the code where you are trying to set the visible property to false in your Login form code. – LarsTech Jun 23 '16 at 21:50
  • @LarsTech, I think you are letting me go on right track, I haven't properly made a child-parent relationship i think. – Jamshaid K. Jun 23 '16 at 21:54
  • @LarsTech, Finally I have found a way to access Parent Control, but still i am unable to access a panel inside it, but other panels are accessible, I have set them to public but still on one Panel it throws exception, the panel where the child form is added – Jamshaid K. Jun 24 '16 at 06:41
  • 1
    You're talking about stuff we can't see. You have to show more of your code for anyone to help you. – LarsTech Jun 24 '16 at 12:39
  • Yeah you are right, but I cannot post all the code here. you can just imagine of a form having multiple public controls, and on second form it allows to access only one, not other controls – Jamshaid K. Jun 24 '16 at 12:55

3 Answers3

1

If I understand this right, I had the same problem. I was confused by the term "ParentForm" and this other answer really helped explain why I was doing it wrong.

Whats the difference between Parentform and Owner

To allow a top-level form to share a control with a lower-level form:

1.) In form designer, open the main form, select the control to be shared, and set its modifier to "Internal".

2.) When calling the lower-level form, supply "this" as the owner parameter of Show().

LoginForm login = new LoginForm();
login.Show(this);

3.) From the lower-level form, you can now reference the Owner property and cast it back to its class type to access the shared control by name.

((MainForm)Owner).PanelContainer.Visible = false;
Dan S.
  • 81
  • 1
  • 2
  • Just a note: The reference to the ((MainForm)Owner) will not work in the Constructor of the lower level form, since it has not yet been shown from the MainForm. You can perform the ((MainForm)Owner).PanelContainer.Visible = false; line in the Load event. – John Kurtz Nov 12 '20 at 15:52
0

The Controls object off a Control is a collection that is accessible by index.

this.ParentForm.Controls[0].Visible. . .

The name you are referencing would be inside something like:

this.ParentForm.Controls[0].Name
pay
  • 366
  • 3
  • 18
0

make sure that the control in the parent form is set to public. After that, accessing that control is as simple as

ParentForm frmParentForm= (ParentForm)Application.OpenForms["ParentForm"];
frmParentForm.YourControlName
Ozesh
  • 6,536
  • 1
  • 25
  • 23
  • Actually, I am getting the parent form but whenever i add child form to to parent form panel, it removes the parent reference – Jamshaid K. Jun 24 '16 at 05:56