Solved

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

Posted on 2004-10-26
1,717 Views
Last Modified: 2010-02-26
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.
0
Question by:kacalapy
    14 Comments
     
    LVL 8

    Expert Comment

    by:AaronReams
    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
     
    LVL 1

    Author Comment

    by:kacalapy
    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
     
    LVL 1

    Author Comment

    by:kacalapy
    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
     
    LVL 8

    Expert Comment

    by:AaronReams
    Hmmm, strange.

    Is LoginId a string or textbox?

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

    0
     
    LVL 1

    Author Comment

    by:kacalapy
    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
     
    LVL 8

    Expert Comment

    by:AaronReams
    I think the problem is actually from this line.  You don't need it.

    pricelist.Parent = this;

    0
     
    LVL 8

    Expert Comment

    by:AaronReams
    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
     
    LVL 1

    Author Comment

    by:kacalapy
    not sure what you mean. i know c# in web dev. im not great with desktop stuff yet. please explain.
    0
     
    LVL 1

    Author Comment

    by:kacalapy
    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
     
    LVL 8

    Expert Comment

    by:AaronReams
    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
     
    LVL 1

    Author Comment

    by:kacalapy
    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
     
    LVL 8

    Expert Comment

    by:AaronReams
    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
     
    LVL 8

    Accepted Solution

    by:
    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
     
    LVL 1

    Author Comment

    by:kacalapy
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    Title # Comments Views Activity
    Problem to packaging 1 43
    JQuery Date Time picker not showing 29 65
    FizzBuzz challenge 9 51
    copyEndy  challenge 15 25
    This article will show, step by step, how to integrate R code into a R Sweave document
    This is an explanation of a simple data model to help parse a JSON feed
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    933 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now