开发者

How do I fix the autoscroll from moving my point of origin in a Forms based program?

开发者 https://www.devze.com 2023-02-28 22:38 出处:网络
I have set up a program that adds new rows when the checkbox at the end is checked. My problem is with the autoscroll. When the rows get passed the edge of the window, it creates the next row, but it

I have set up a program that adds new rows when the checkbox at the end is checked. My problem is with the autoscroll. When the rows get passed the edge of the window, it creates the next row, but it looks like it sets the origin to the previous row's starting point.

Here is some of the code:

private void AddRow(object sender, EventArgs e)
    {
        bool check = ((CheckBox)sender).Checked;
        if (check)
        {
            proj[i] = new Label();
            proj[i].Text = "Proj #";
            proj[i].Width = 50;
            proj[i].Location = new Point(10, (i * 22) + 50);
        ...
            split[i] = new CheckBox();
            split[i].Text = "";
            split[i].Location = new Point(430, (i * 22) + 50);
            split[i].CheckedChang开发者_JS百科ed += new EventHandler(AddRow);
        }
        this.Controls.Add(proj[i]);
   }

And here are a couple screenshots:

How do I fix the autoscroll from moving my point of origin in a Forms based program?

How do I fix the autoscroll from moving my point of origin in a Forms based program?

How can I fix this problem?


The problem is with the way that autoscroll behaves. When you scroll down, the autoscroll behavior actually loops through all of your controls and moves them up by adjusting their Top properties. This certainly makes it difficult to add another row. Two simple options:

Option 1: Add each row of controls to its own panel (or, better, create a UserControl containing them). Then add each of these panels to a FlowLayoutPanel or GridLayoutPanel, which will do computation of the coordinates for you.

-OR-

Option 2: Instead of using (i * 22) + 50, use:

if (i == 0)
    proj[i].Location = new Point(10, 50);
else
    proj[i].Location = new Point(10, proj[i-1].Top + 22);

Likewise for all of your other controls.

The first option is better practice, but the second one may be a quicker direct solution. You should also consider using a grid control instead, especially if the number of rows may be large.

0

精彩评论

暂无评论...
验证码 换一张
取 消