C# error - Cannot add a top level control to a control

trying to open a form in another form using C#. here is the code that fails:

private void btnPriceList2_Click(object sender, System.EventArgs e)
            {
                  if(txtAuctionId.Text.Trim() != "")
                  {
                        PriceList pricelist = new PriceList(txtAuctionId.Text.Trim());
                        pricelist.Parent = this;
                        pricelist.LoginId = this.LoginId;
                        pricelist.ShowDialog();
                  }
            }


i have a similar function that works for another button, that is:

private void showAddNewLotScreen()
            {

                  if(txtAuctionId.Text.Trim()!="")
                  {
                        LotDetails lot = new LotDetails(null,txtAuctionId.Text.Trim());
                        lot.parent = this;
                        lot.LoginId = this.LoginId;
                        lot.ShowDialog();
                  }
            
            }

the PriceList form is giving me the below error. any ideas???


An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll

Additional information: Cannot add a top level control to a control.
LVL 1
kacalapyAsked:
Who is Participating?
 
AaronReamsCommented:
Here's a link which explains why you cannot add a form to another form by setting the "Parent" equal the other form.

http://themaffeo.com/archive/2004/09/22/158.aspx

Enjoy -Aaron
0
 
AaronReamsCommented:
I noticed that you're passing 2 parameters to LotDetails and only 1 to PriceList.

PriceList pricelist = new PriceList(txtAuctionId.Text.Trim());

LotDetails lot = new LotDetails(null,txtAuctionId.Text.Trim());

You could try switching these to the following format...

PriceList pricelist = new PriceList();
pricelist.Text = txtAuctionId.Text.Trim();

0
 
kacalapyAuthor Commented:
i pass the param because my for is defined as such...

public PriceList(string AuctionId)
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
kacalapyAuthor Commented:
pricelist.LoginId = this.LoginId;

this is the line that makes the error.
if i comment it out, the next line makes the saem error.

looks liek something in the page inernals i am missing here :(
0
 
AaronReamsCommented:
Hmmm, strange.

Is LoginId a string or textbox?

Are you doing some work before the call to InitializeComponent() in pricelist?

0
 
kacalapyAuthor Commented:
LoginId is a string.
nothing here... before the call to InitializeComponent

public PriceList(string AuctionId)
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }
0
 
AaronReamsCommented:
I think the problem is actually from this line.  You don't need it.

pricelist.Parent = this;

0
 
AaronReamsCommented:
If you want a pointer from the calling object pass it into the constructor or set it to member variable later (other than parent).
0
 
kacalapyAuthor Commented:
not sure what you mean. i know c# in web dev. im not great with desktop stuff yet. please explain.
0
 
kacalapyAuthor Commented:
also the same code is working to call another page "lotdetails" i am trying to call pricelist in the same way. its not workign out for me :(
0
 
AaronReamsCommented:
Generally, its not a good idea to set the Parent pointer of control when you don't need to.  

If you want a pointer (reference) to the calling object you can set it yourself.

public class PriceList
{
     public LotDetails refLotDetails = null;
     public PriceList() {}
}

Then when you actually instantiate the PriceList object.

PriceList form = new PriceList();
form.refLotDetails = this;  //assuming you are creating the form from a LotDetails member function

Does that help?

-Good luck,
Aaron


0
 
kacalapyAuthor Commented:
not realy, lol. but i did fint the problem.

stupid error. the PriceList page class didnt have both
  public string LoginId;
  public LotList parent;
defined. I WILL AWARD THE POINTS TO YOU FOR A LINK THAT HAS SOME GOOD EXAMPLES OF THIS TOPIC SO I CAN LEAN SOMETHIGN HERE.
0
 
AaronReamsCommented:
I didn't notice that "parent" was lower case in your example.  "Parent" with a capital "P" is a property of all controls.  I would stay away from using predefined members with different case.

You see the problem wasn't just that you didn't have that defined.  Visual Studio will automatically correct the case with IntelliSense, which results in the runtime error "Cannot add a top level control to a control." rather than a compile time error "Undefined variable".

0
 
kacalapyAuthor Commented:
AaronReams,

thank you mutch. you take the points.

also if you can point me in a direction of makeing good datadriven winforms in c# that allow the user to maek updates in a datagrid and persist them to the database... that would just make my day.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.