Click here to Skip to main content
15,915,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting the below exception while adding controls to panel.

A circular control reference has been made. A control cannot be owned by or parented to itself.

Code:
C#
int yAxis = 0;
for (int i = stkNotificaiton.Count; i > 0; i--)
{ 
    Panel pnl = (Panel)stkNotificaiton.Pop();
    pnl.AutoSize = true;
    pnl.Location = new Point(0, yAxis);
    pnlNotification.Controls.Add(pnl);  // <-------------------Error is showing.
    yAxis = 48 * noofNewNotifications;
}


here stkNotification is a stack.
Please help to solve this problem.
Posted
Updated 3-Apr-12 3:24am
v2
Comments
Shahin Khorshidnia 3-Apr-12 9:26am    
What is pnlNotification?
bhagirathimfs 3-Apr-12 9:29am    
no of new notifications

this will help to positioning the inserted panel.

1 solution

The error message is pretty clear: at some point you have put pnlNotification on the stack and are trying to add it to it's own Controls List.
 
Share this answer
 
Comments
bhagirathimfs 3-Apr-12 9:35am    
@OriginalGriff: ya you are right.Can you please suggest how can i solve this with this logic. Means i want to add to the pnlNotification from stack and put that pnlNotifications again into the stack.
OriginalGriff 3-Apr-12 9:56am    
A better solution would be not to put it on the stack in the first place! :laugh:
In fact, unless it is right at the bottom of the stack, it gets very very untidy trying to put it back on - as you need to get the element underneath it next time round. And if it could be the bottom of the stack, then you have to check for that specifically before you continue. PITA

Seriously better not to put it on there in the first place.
bhagirathimfs 3-Apr-12 10:45am    
My problem:
In my form already 4 messages(notifications) are present in a panel(panel name pnlNotification).A new notification came from database after 10 second.I want to insert that one at the top of the panel dynamically.

My approach: First i put all the notifications present in the pnlNotification into a stack and then the new notifications. Than i retrieving all the notifications from the stack and put that into the pnlNotification panel.

Can you please suggest another way to solve my problem ??? or add anything in my approach.
OriginalGriff 3-Apr-12 11:00am    
So why is pnlNotification on the stack?

The other question is why are you doing that anyway? Don't take the existing items off the Controls collection and then add them back, just change the Location parameter and they will move - you can add the new one in at the end of the list, it doesn't matter. If you are concerned about traversing the list in latest-first (or any other specific) order, then set up a list, or a stack of Notification objects and use that - don't use the Controls collection for that purpose because that ties your code to a particular user interface implementation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900