# [.net] Strange behavior Arranging child controls

This topic is 3455 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello there. Using C#/.NET 3.5, I made a UserControl that houses some child Panels, and has to arrange them. The "container control" has a vertical shape and when controls are added, it should arrange them so that each next control is below the previous. To do that, I put something like this in the overridden OnLayout() method: (I hope the PLUS chars get shown here, they don't in the preview)
			int verticalDistance = 2;
int y = 0;
foreach (Panel p in this.Controls)
{
p.Location = new Point(0, y);
p.Width = this.ClientSize.Width;
y	+=	p.Height + verticalDistance;
}


I have a button on each child panel that is to allow to change its "rank" in the contaning control ("move up"), clicking on it does this:
int index = Controls.GetChildindex( panel );
if (index > 0)
{
Controls.SetChildindex(panel, index-1);
}


Now the strange thing is, if I put more than two panels into the container control, when I click the "MoveUp" button on some, somtimes, the arrangement fails such that the actual Y-position of the topmost control will not be 0 as you saw in the code, but a lot lower, so that there is a lot of free space above the top control, which I obviously don't want. Sometimes panels will even overlap. Maybe it's noteworthy that AutoScale is true for the container control. There is no other code that alters the vertical position of the child panels in my project, and it's as simple as shown. I can't see what's happening, so I guess it must have to do with some behavior of .NET controls I don't know o_O Any ideas? Thanks in advance.

##### Share on other sites
Quote:
 Original post by UnshavenBastardMaybe it's noteworthy that AutoScale is true for the container control.

If you're manually positioning or moving items I would have anything related to sizing and positioning that fires automatically turned off. I'm not sure that this is your problem; however, I have had issues in the past where programmatic manipulation of properties such as the control's location would fail to update related properties that the automatic methods relied upon.

As well: if you have isolated the incident as cleanly as you seem to have a trip through the application with a debugger would probably prove beneficial.

##### Share on other sites
yeah, yeah.

I disabled the autoscrolling stuff and did it all by hand,
now it works fine... *sigh*

another funny thing I had was that if I moved the mousewheel, a control I had on the mainForm would move slightly left/right, (3..4 pixels?), depending on the direction I was "wheeling".
Strange...

##### Share on other sites
Any reason why you aren't using FlowLayoutPanel with FlowDirection=TopDown and WrapContents=false?

##### Share on other sites
Quote:
 Original post by mutexAny reason why you aren't using FlowLayoutPanel with FlowDirection=TopDown and WrapContents=false?

Probably because I wasn't familiar with it ^^ Thanks for the hint.
Whether this integrates with the partially custom behavior of things is another question.
I'll play around with it and see how it does things.