kacalapy
asked on
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.Tex t.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,txtAuction Id.Text.Tr im());
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.
private void btnPriceList2_Click(object
{
if(txtAuctionId.Text.Trim(
{
PriceList pricelist = new PriceList(txtAuctionId.Tex
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,txtAuction
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'
Additional information: Cannot add a top level control to a control.
ASKER
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
//
}
public PriceList(string AuctionId)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
ASKER
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 :(
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 :(
Hmmm, strange.
Is LoginId a string or textbox?
Are you doing some work before the call to InitializeComponent() in pricelist?
Is LoginId a string or textbox?
Are you doing some work before the call to InitializeComponent() in pricelist?
ASKER
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
//
}
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
//
}
I think the problem is actually from this line. You don't need it.
pricelist.Parent = this;
pricelist.Parent = this;
If you want a pointer from the calling object pass it into the constructor or set it to member variable later (other than parent).
ASKER
not sure what you mean. i know c# in web dev. im not great with desktop stuff yet. please explain.
ASKER
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 :(
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
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
ASKER
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.
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.
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".
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".
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
PriceList pricelist = new PriceList(txtAuctionId.Tex
LotDetails lot = new LotDetails(null,txtAuction
You could try switching these to the following format...
PriceList pricelist = new PriceList();
pricelist.Text = txtAuctionId.Text.Trim();