事件完成后,索引超出范围

时间:2017-05-14 23:14:43

标签: c# winforms events

我相信我知道这个问题的原因,但我不确定如何解决它。

我正在使用WinForms和MVP(模型 - 视图 - 演示者)方法。

我有一个有订阅者的事件,该订阅者调用一个方法,该方法打开表单的新实例并在表单上绘制矩形。表单应如下所示:Design

现在要在任何页面上使用它,我计划根据表单的大小调整矩形的大小。我在之前填写的表单上收到了用户的数据。

我的订阅者看起来像这样:

private void ValidateFormBeforeDiagram(object sedner, OptionOneCheckedEventArgs e) {
            try {
                //Create variables
                int pagesUp = e.IsOptionOneChecked ? view.OptionOneAround : view.OptionTwoAround;
                int pagesAcross = e.IsOptionOneChecked ? view.OptionOneAcross : view.OptionTwoAcross;
                float pageWidth = e.IsOptionOneChecked ? view.PageSizeLength : view.PageSizeWidth;
                float pageLength = e.IsOptionOneChecked ? view.PageSizeWidth : view.PageSizeLength;


                ParametersToCreatePrintingDiagram parameters = new ParametersToCreatePrintingDiagram(view.SheetSizeAcross, view.SheetSizeAround,
                    pagesUp, pagesAcross, pageWidth, pageLength);

                //Check if form is open, if so close it

                if (Application.OpenForms.OfType<PrintingDesignForm>().Any()) {
                    Application.OpenForms.OfType<PrintingDesignForm>().First().Close();
                }

                //Create a new instance of the form
                PrintingDesignForm form = new PrintingDesignForm();
                float ratio = 0;

                //If rectangle is larger than the client viewable size then scale the rectangle so it will fit.
                if (parameters.RectangleHeight > form.ClientRectangle.Height || parameters.RectangleWidth > form.ClientRectangle.Height) {
                    var ratioX = form.ClientRectangle.Height / parameters.RectangleHeight;
                    var ratioY = form.ClientRectangle.Width / parameters.RectangleWidth;

                    ratio = Math.Min(ratioX, ratioY);

                    parameters.RectangleHeight = parameters.RectangleHeight * ratio - parameters.RectangleXPosition;
                    parameters.RectangleWidth = parameters.RectangleWidth * ratio - parameters.RectangleYPosition;
                }

                //Draw the main rectangle
                form.Paint += (se, pe) => {
                    var r = new Rectangle(parameters.RectangleXPosition, parameters.RectangleYPosition,
                        (int)Math.Round(parameters.RectangleWidth), (int)Math.Round(parameters.RectangleHeight));

                    var brush = new SolidBrush(Color.FromArgb(255, 255, 204));

                    pe.Graphics.FillRectangle(brush, r);
                    using (var pen = new Pen(brush.Color, 2))
                        pe.Graphics.DrawRectangle(pen, r);
                };

                int leftXInches;
                int topYInches;
                int xInch = 0;
                int yInch = 0;
                List<Rectangle> rectArr = new List<Rectangle>();

                //Calculate the smaller rectangle sizes and positions
                for (int i = 1; i < parameters.PagesUp; i++) {
                    for (int j = 1; j < parameters.PagesAcross; j++) {
                        if (e.IsOptionOneChecked) {
                            leftXInches = (int)Math.Round(parameters.RectangleXPosition
                            + ((view.SheetSizeAcross - (view.PageSizeLength * view.OptionOneAcross) - (view.OptionOneAcross - 1) * view.Bleeds) / 2)
                            + ((j - 1) * parameters.PageWidth)
                            + ((j - 1) * view.Bleeds));

                            topYInches = (int)Math.Round(parameters.RectangleYPosition
                            + ((view.SheetSizeAround - (view.PageSizeLength * view.OptionOneAround) - (view.OptionOneAround - 1) * view.Bleeds) / 2)
                            + ((i - 1) * parameters.PageHeight)
                            + ((i - 1) * view.Bleeds));

                        }
                        else {

                            leftXInches = (int)Math.Round(parameters.RectangleXPosition
                         + ((view.SheetSizeAcross - (view.PageSizeWidth * view.OptionTwoAcross) - (view.OptionTwoAcross - 1) * view.Bleeds) / 2)
                         + ((j - 1) * parameters.PageWidth)
                         + ((j - 1) * view.Bleeds));

                            topYInches = (int)Math.Round(parameters.RectangleYPosition
                         + ((view.SheetSizeAround - (view.PageSizeLength * view.OptionTwoAround) - (view.OptionTwoAround - 1) * view.Bleeds) / 2)
                         + ((i - 1) * parameters.PageHeight)
                         + ((i - 1) * view.Bleeds));
                        }

                        var width = (int)((Math.Round(parameters.PageWidth) * 72) * ratio);
                        var height = (int)((Math.Round(parameters.PageHeight) * 72) * ratio);

                        yInch = (int)Math.Round((topYInches * 72) * ratio);

                        //Increase the xInch every loop as you need to make sure that there is room for other rectangles
                        if (j == 1) {
                            xInch = (int)Math.Round((leftXInches * 72) * ratio);
                        }
                        else {
                            xInch += width + 20;
                        }

                        //Add rectangle with specs ready to be painted onto the form
                        rectArr.Add(new Rectangle(xInch, yInch, width, height));
                    }
                }

                //Paint the smaller rectangles onto the form
                for (int i = 0; i < rectArr.Count; i++) {
                    form.Paint += (se, pe) => {
                        var r = rectArr[i]; //Exception occurs on this line

                        var brush = new SolidBrush(Color.FromArgb(255, 0, 0));

                        pe.Graphics.FillRectangle(brush, r);
                        using (var pen = new Pen(brush.Color, 2))
                            pe.Graphics.DrawRectangle(pen, r);
                    };
                }

                form.Show();
            }
            catch (Exception ex) {
                LogErrorToView(this, new ErrorEventArgs(ex.Message));
            }
        }

我在上面评论过发生异常的地方。之所以发生这种情况,是因为在此课程完成后,发布者事件就完成了。发生这种情况后,另外两个绘制事件被调用,并且由于rectArr不再在范围内而导致错误。

有什么方法可以解决这个问题吗?

编辑:为什么选择downvote?

0 个答案:

没有答案
相关问题