winforms 获取System.ArgumentOutOfRangeException,但它没有意义[重复]

6uxekuva  于 2023-10-23  发布在  其他
关注(0)|答案(1)|浏览(118)

此问题已在此处有答案

What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?(5个答案)
上个月关门了。
我正在获取此System.ArgumentOutOfRangeException:'索引超出范围。必须为非负数且小于集合的大小。参数名称:但只有在数据网格视图中,当宽度大于1321时才会出现此错误。when it is like this no errorhere error occures .我的目标是在整个屏幕上适合数据网格视图,但我不能。
代码:

Rectangle originalFormSize;
List<Rectangle> originalControlsRectangle = new List<Rectangle>();

private void Form1_Load(object sender, EventArgs e) // Called when form loaded
{
    originalFormSize = new Rectangle(this.Location, this.Size);

    for (int i = 0; i < Controls.Count; i++)
    {
        originalControlsRectangle.Add(new Rectangle(Controls[i].Location, Controls[i].Size));

    }

    this.WindowState = FormWindowState.Maximized;
}

private void resizeControl(Rectangle r, Control c)
{
    float xRatio = (float)(this.Width) / (float)(originalFormSize.Width);
    float yRatio = (float)(this.Height) / (float)(originalFormSize.Height);

    int newX = (int)(r.Location.X * xRatio);
    int newY = (int)(r.Location.Y * yRatio);

    int newWidth = (int)(r.Width * xRatio);
    int newHeight = (int)(r.Height * yRatio);

    c.Location = new Point(newX, newY);
    c.Size = new Size(newWidth, newHeight);
}

private void Form1_Resize(object sender, EventArgs e) // Called when form resized
{
    for (int i = 0; i < Controls.Count; i++)
    {
        resizeControl(originalControlsRectangle[i], Controls[i]); // Error here

    }
}

错误:

xesrikrc

xesrikrc1#

我重新设计了我的形式与码头和面板,这是不像以前,但至少它的工作。

相关问题