Discussion Locked

Problem positioning controls within a panel

0 helpful
2 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I don't know if this is a bug or if I am doing something wrong, but hopefully
somebody could help me.
I have a panel with autoscroll=true, and I am trying to position controls
within it, however they don't position correctly.
According to MS's documentation Panel.Left "Gets or sets the distance, in
pixels, between the left edge of the control and the left edge of its
container's client area."

Here's the sample code for you to reconstruct it real quick.
Essentially its a form with a panel with autoscroll=true,
a textbox of any size outside of the panel,
and a button with the following click event handler.

int x = 0;
int y = 0;

private void button1_Click(object sender, System.EventArgs e)
{
TextBox f = new TextBox();
f.Size = textBox1.Size;
f.Multiline = true;
panel1.Controls.Add(f);
f.Left = x1;
f.Top = y1;
//f.Location = new Point(x1, y1);

x1 += 100;
y1 += 100;
panel1.Refresh();
}

It should draw textboxes diagonally toward the right, with a scrollbar
eventually appearing. However, if you move the horiz/vert. scrollbars any
bit after creating a few of these diagonal boxes, it jumps a huge gap, then
creates the textbox. What I think should be happening is that it should
create a lot of textboxes diagonally with no gaps between them regardless of
whether or not the scrollbars have been moved at all. I have tried using the
commented out location line above and I have tried setting the positions
before adding it into the panel. Nothing has worked for me so far.

Thanks

2 replies

by g...

did you try with x1 and y1 += 20 (for example) just something less than 100. It may also be a problem with the first location. Try to start at a different point than (0,0).

good luck

by alanjnew

Yes, this is a problem with the panel control - it appears that instead of positioning the controls absolutely, it positions them relative to the current panel position (as per the scroll bars)
The only work-around I have come up with so far is to ensure that, before inserting a control, you position the panel to the top-left. I have done this by using the 'ScrollControlIntoView' method, using the first control. Other than that, as you have pointed out, nothing seems to work.

This discussion is closed to new replies.