[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2004-10-26
14
Medium Priority
?
1,999 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
Comment
Question by:kacalapy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
14 Comments
 
LVL 8

Expert Comment

by:AaronReams
ID: 12413965
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
ID: 12414171
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
ID: 12414230
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Expert Comment

by:AaronReams
ID: 12414273
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
ID: 12414342
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
ID: 12414414
I think the problem is actually from this line.  You don't need it.

pricelist.Parent = this;

0
 
LVL 8

Expert Comment

by:AaronReams
ID: 12414423
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
ID: 12414571
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
ID: 12414591
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
ID: 12414676
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
ID: 12414723
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
ID: 12414805
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:
AaronReams earned 200 total points
ID: 12414962
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
ID: 12424074
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Make the most of your online learning experience.
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 …
Introduction to Processes

656 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