C# Windows Form ParentForm Problem

  • Thread starter Thread starter Jason Huang
  • Start date Start date
J

Jason Huang

Hi,

In my ASP.Net C# Windows Form project, I have 2 forms, FormParent and
FormChild.
I have a Button1_Click event in FormParent, and the codes are:

private void Button1_Click(object sender, System.EventArgs e)
{
FormChild MyFormChild=new FormChild();
MyFormChild.ParentForm=this;
MyFormChild.Show();
}

However, there's some error message saying the
System.Windows.Forms.ContainerControl.ParentForm is ReadOnly.
What have I done wrong?
Any help will be appreciated.

Jason
 
One thing that come to my mind, perhaps you have already done this, but have
you set the main form to be a MDI Parent?

//Martin
 
Jason Huang said:
private void Button1_Click(object sender, System.EventArgs e)
{
FormChild MyFormChild=new FormChild();
MyFormChild.ParentForm=this;
MyFormChild.Show();
}

However, there's some error message saying the
System.Windows.Forms.ContainerControl.ParentForm is ReadOnly.
What have I done wrong?

The ParentForm properties *is* read only.

What are you trying to do? An MDI container, or a modal dialogue?

It seems like you want a modal dialogue, at least thats what your code
suggests. In which case, you pass the parent form to the ShowDialogue method
on your child form, like:

private void Button1_Click(object sender, System.EventArgs e)
{
FormChild MyFormChild=new FormChild();
MyFormChild.ShowDialogue(this);
}

If you want an MDI type scenario, then you set IsMDIContainer to true on
your parent, and set the MDIParent property to the parent form on the child,
then call show.

private void Button1_Click(object sender, System.EventArgs e)
{
FormChild MyFormChild=new FormChild();
MyFormChild.MDIParent=this;
MyFormChild.Show();
}


--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
 
Thanks Tim.
These 2 forms are just 2 forms in a ASP.Net C# Widnows Form project. They
are not MDI forms.
Actually, the FormParent has a button which will open the FormChild. The
FormChild will pass a value to the FormParent so the FormParent will base on
that value to retrieve a corresponding DataSet, showing in TextBoxes.
These TextBoxes are in FormParent's GroupBox1. I have no problem to pass
the value from FormChild to FormParent. And I can retrieve the DataSet OK(I
use the MessageBox to check if the DataSet is good). BUT, those TextBoxes
in GroupBox1 don't have any data in there.
Any help will be appreciated.

Jason
 
Back
Top