public void TableLayoutColumnStyleCollection_Remove_ColumnStyle_Success() { using var toolStrip = new ToolStrip { LayoutStyle = ToolStripLayoutStyle.Table }; TableLayoutSettings settings = Assert.IsType <TableLayoutSettings>(toolStrip.LayoutSettings); TableLayoutColumnStyleCollection collection = settings.ColumnStyles; var style = new ColumnStyle(); collection.Add(style); collection.Remove(style); Assert.Empty(collection); collection.Add(style); Assert.Equal(style, Assert.Single(collection)); }
public void TableLayoutColumnStyleCollection_Item_GetNotColumnStyle_ThrowsInvalidCastException() { using var toolStrip = new ToolStrip { LayoutStyle = ToolStripLayoutStyle.Table }; TableLayoutSettings settings = Assert.IsType <TableLayoutSettings>(toolStrip.LayoutSettings); TableLayoutColumnStyleCollection collection = settings.ColumnStyles; Assert.Throws <InvalidCastException>(() => collection.Add(new RowStyle())); Assert.Throws <InvalidCastException>(() => collection[0]); }
public void TableLayoutColumnStyleCollection_Add_RowStyle_ThrowsInvalidCastException() { using var toolStrip = new ToolStrip { LayoutStyle = ToolStripLayoutStyle.Table }; TableLayoutSettings settings = Assert.IsType <TableLayoutSettings>(toolStrip.LayoutSettings); TableLayoutColumnStyleCollection collection = settings.ColumnStyles; var style = new RowStyle(); Assert.Throws <InvalidCastException>(() => collection.Add(style)); Assert.Equal(style, Assert.Single(collection)); }
public void TableLayoutColumnStyleCollection_Contains_ColumnStyle_ReturnsExpected() { using var toolStrip = new ToolStrip { LayoutStyle = ToolStripLayoutStyle.Table }; TableLayoutSettings settings = Assert.IsType <TableLayoutSettings>(toolStrip.LayoutSettings); TableLayoutColumnStyleCollection collection = settings.ColumnStyles; var style = new ColumnStyle(); collection.Add(style); Assert.True(collection.Contains(style)); Assert.False(collection.Contains(new ColumnStyle())); Assert.False(collection.Contains(null)); }
public void TableLayoutColumnStyleCollection_IndexOf_Invoke_ReturnsExpected() { using var toolStrip = new ToolStrip { LayoutStyle = ToolStripLayoutStyle.Table }; TableLayoutSettings settings = Assert.IsType <TableLayoutSettings>(toolStrip.LayoutSettings); TableLayoutColumnStyleCollection collection = settings.ColumnStyles; var style = new ColumnStyle(); collection.Add(style); Assert.Equal(0, collection.IndexOf(style)); Assert.Equal(-1, collection.IndexOf(new ColumnStyle())); Assert.Equal(-1, collection.IndexOf(null)); }
public void TableLayoutColumnStyleCollection_Item_SetColumnStyle_GetReturnsExpected() { using var toolStrip = new ToolStrip { LayoutStyle = ToolStripLayoutStyle.Table }; TableLayoutSettings settings = Assert.IsType <TableLayoutSettings>(toolStrip.LayoutSettings); TableLayoutColumnStyleCollection collection = settings.ColumnStyles; collection.Add(new ColumnStyle()); var style = new ColumnStyle(); collection[0] = style; Assert.Single(collection); Assert.Equal(style, collection[0]); }
private void CalculateColumnRowSizes (TableLayoutPanel panel, int columns, int rows) { TableLayoutSettings settings = panel.LayoutSettings; panel.column_widths = new int[panel.actual_positions.GetLength (0)]; panel.row_heights = new int[panel.actual_positions.GetLength (1)]; int border_width = TableLayoutPanel.GetCellBorderWidth (panel.CellBorderStyle); Rectangle parentDisplayRectangle = panel.DisplayRectangle; TableLayoutColumnStyleCollection col_styles = new TableLayoutColumnStyleCollection (panel); foreach (ColumnStyle cs in settings.ColumnStyles) col_styles.Add( new ColumnStyle(cs.SizeType, cs.Width)); TableLayoutRowStyleCollection row_styles = new TableLayoutRowStyleCollection (panel); foreach (RowStyle rs in settings.RowStyles) row_styles.Add (new RowStyle (rs.SizeType, rs.Height)); // If we have more columns than columnstyles, temporarily add enough columnstyles if (columns > col_styles.Count) { for (int i = col_styles.Count; i < columns; i++) col_styles.Add(new ColumnStyle()); } // Same for rows.. if (rows > row_styles.Count) { for (int i = row_styles.Count; i < rows; i++) row_styles.Add (new RowStyle ()); } while (row_styles.Count > rows) row_styles.RemoveAt (row_styles.Count - 1); while (col_styles.Count > columns) col_styles.RemoveAt (col_styles.Count - 1); // Figure up all the column widths int total_width = parentDisplayRectangle.Width - (border_width * (columns + 1)); int index = 0; // First assign all the Absolute sized columns.. foreach (ColumnStyle cs in col_styles) { if (cs.SizeType == SizeType.Absolute) { panel.column_widths[index] = (int)cs.Width; total_width -= (int)cs.Width; } index++; } index = 0; // Next, assign all the AutoSize columns.. foreach (ColumnStyle cs in col_styles) { if (cs.SizeType == SizeType.AutoSize) { int max_width = 0; // Find the widest control in the column for (int i = 0; i < rows; i ++) { Control c = panel.actual_positions[index, i]; if (c != null && c != dummy_control && c.VisibleInternal) { if (settings.GetColumnSpan (c) > 1) continue; if (c.AutoSize) max_width = Math.Max (max_width, c.PreferredSize.Width + c.Margin.Horizontal); else max_width = Math.Max (max_width, c.ExplicitBounds.Width + c.Margin.Horizontal); if (c.Width + c.Margin.Left + c.Margin.Right > max_width) max_width = c.Width + c.Margin.Left + c.Margin.Right; } } panel.column_widths[index] = max_width; total_width -= max_width; } index++; } index = 0; float total_percent = 0; // Finally, assign the remaining space to Percent columns.. if (total_width > 0) { int percent_width = total_width; // Find the total percent (not always 100%) foreach (ColumnStyle cs in col_styles) { if (cs.SizeType == SizeType.Percent) total_percent += cs.Width; } // Divy up the space.. foreach (ColumnStyle cs in col_styles) { if (cs.SizeType == SizeType.Percent) { panel.column_widths[index] = (int)((cs.Width / total_percent) * percent_width); total_width -= panel.column_widths[index]; } index++; } } if (total_width > 0) panel.column_widths[col_styles.Count - 1] += total_width; // Figure up all the row heights int total_height = parentDisplayRectangle.Height - (border_width * (rows + 1)); index = 0; // First assign all the Absolute sized rows.. foreach (RowStyle rs in row_styles) { if (rs.SizeType == SizeType.Absolute) { panel.row_heights[index] = (int)rs.Height; total_height -= (int)rs.Height; } index++; } index = 0; // Next, assign all the AutoSize rows.. foreach (RowStyle rs in row_styles) { if (rs.SizeType == SizeType.AutoSize) { int max_height = 0; // Find the tallest control in the row for (int i = 0; i < columns; i++) { Control c = panel.actual_positions[i, index]; if (c != null && c != dummy_control && c.VisibleInternal) { if (settings.GetRowSpan (c) > 1) continue; if (c.AutoSize) max_height = Math.Max (max_height, c.PreferredSize.Height + c.Margin.Vertical); else max_height = Math.Max (max_height, c.ExplicitBounds.Height + c.Margin.Vertical); if (c.Height + c.Margin.Top + c.Margin.Bottom > max_height) max_height = c.Height + c.Margin.Top + c.Margin.Bottom; } } panel.row_heights[index] = max_height; total_height -= max_height; } index++; } index = 0; total_percent = 0; // Finally, assign the remaining space to Percent columns.. if (total_height > 0) { int percent_height = total_height; // Find the total percent (not always 100%) foreach (RowStyle rs in row_styles) { if (rs.SizeType == SizeType.Percent) total_percent += rs.Height; } // Divy up the space.. foreach (RowStyle rs in row_styles) { if (rs.SizeType == SizeType.Percent) { panel.row_heights[index] = (int)((rs.Height / total_percent) * percent_height); total_height -= panel.row_heights[index]; } index++; } } if (total_height > 0) panel.row_heights[row_styles.Count - 1] += total_height; }
private void CalculateColumnRowSizes(TableLayoutPanel panel, int columns, int rows) { TableLayoutSettings settings = panel.LayoutSettings; panel.column_widths = new int[panel.actual_positions.GetLength(0)]; panel.row_heights = new int[panel.actual_positions.GetLength(1)]; int border_width = TableLayoutPanel.GetCellBorderWidth(panel.CellBorderStyle); Rectangle_ parentDisplayRectangle = panel.DisplayRectangle; TableLayoutColumnStyleCollection col_styles = new TableLayoutColumnStyleCollection(panel); foreach (ColumnStyle cs in settings.ColumnStyles) { col_styles.Add(new ColumnStyle(cs.SizeType, cs.Width)); } TableLayoutRowStyleCollection row_styles = new TableLayoutRowStyleCollection(panel); foreach (RowStyle rs in settings.RowStyles) { row_styles.Add(new RowStyle(rs.SizeType, rs.Height)); } // If we have more columns than columnstyles, temporarily add enough columnstyles if (columns > col_styles.Count) { for (int i = col_styles.Count; i < columns; i++) { col_styles.Add(new ColumnStyle()); } } // Same for rows.. if (rows > row_styles.Count) { for (int i = row_styles.Count; i < rows; i++) { row_styles.Add(new RowStyle()); } } while (row_styles.Count > rows) { row_styles.RemoveAt(row_styles.Count - 1); } while (col_styles.Count > columns) { col_styles.RemoveAt(col_styles.Count - 1); } // Find the largest column-span/row-span values. int max_colspan = 0, max_rowspan = 0; foreach (Control c in panel.Controls) { max_colspan = Math.Max(max_colspan, settings.GetColumnSpan(c)); max_rowspan = Math.Max(max_rowspan, settings.GetRowSpan(c)); } // Figure up all the column widths int total_width = parentDisplayRectangle.Width - (border_width * (columns + 1)); int index = 0; // First assign all the Absolute sized columns.. foreach (ColumnStyle cs in col_styles) { if (cs.SizeType == SizeType.Absolute) { panel.column_widths[index] = (int)cs.Width; total_width -= (int)cs.Width; } index++; } // Next, assign all the AutoSize columns to the width of their widest // control. If the table-layout is auto-sized, then make sure that // no column with Percent styling clips its contents. // (per http://msdn.microsoft.com/en-us/library/ms171690.aspx) for (int colspan = 0; colspan < max_colspan; ++colspan) { for (index = colspan; index < col_styles.Count - colspan; ++index) { ColumnStyle cs = col_styles[index]; if (cs.SizeType == SizeType.AutoSize || (panel.AutoSize && cs.SizeType == SizeType.Percent)) { int max_width = panel.column_widths[index]; // Find the widest control in the column for (int i = 0; i < rows; i++) { Control c = panel.actual_positions[index - colspan, i]; if (c != null && c != dummy_control && c.VisibleInternal) { // Skip any controls not being sized in this pass. if (settings.GetColumnSpan(c) != colspan + 1) { continue; } // Calculate the maximum control width. if (c.AutoSize) { max_width = Math.Max(max_width, c.PreferredSize.Width + c.Margin.Horizontal); } else { max_width = Math.Max(max_width, c.ExplicitBounds.Width + c.Margin.Horizontal); } max_width = Math.Max(max_width, c.Width + c.Margin.Left + c.Margin.Right); } } // Subtract the width of prior columns, if any. for (int i = Math.Max(index - colspan, 0); i < index; ++i) { max_width -= panel.column_widths[i]; } // If necessary, increase this column's width. if (max_width > panel.column_widths[index]) { max_width -= panel.column_widths[index]; panel.column_widths[index] += max_width; total_width -= max_width; } } } } index = 0; float total_percent = 0; // Finally, assign the remaining space to Percent columns, if any. if (total_width > 0) { int percent_width = total_width; // Find the total percent (not always 100%) foreach (ColumnStyle cs in col_styles) { if (cs.SizeType == SizeType.Percent) { total_percent += cs.Width; } } // Divvy up the space.. foreach (ColumnStyle cs in col_styles) { if (cs.SizeType == SizeType.Percent) { int width_change = (int)(((cs.Width / total_percent) * percent_width) - panel.column_widths[index]); if (width_change > 0) { panel.column_widths[index] += width_change; total_width -= width_change; } } index++; } } if (total_width > 0) { // Find the last column that isn't an Absolute SizeType, and give it // all this free space. (Absolute sized columns need to retain their // absolute width if at all possible!) int col = col_styles.Count - 1; for (; col >= 0; --col) { if (col_styles[col].SizeType != SizeType.Absolute) { break; } } if (col < 0) { col = col_styles.Count - 1; } panel.column_widths[col] += total_width; } // Figure up all the row heights int total_height = parentDisplayRectangle.Height - (border_width * (rows + 1)); index = 0; // First assign all the Absolute sized rows.. foreach (RowStyle rs in row_styles) { if (rs.SizeType == SizeType.Absolute) { panel.row_heights[index] = (int)rs.Height; total_height -= (int)rs.Height; } index++; } index = 0; // Next, assign all the AutoSize rows to the height of their tallest // control. If the table-layout is auto-sized, then make sure that // no row with Percent styling clips its contents. // (per http://msdn.microsoft.com/en-us/library/ms171690.aspx) for (int rowspan = 0; rowspan < max_rowspan; ++rowspan) { for (index = rowspan; index < row_styles.Count - rowspan; ++index) { RowStyle rs = row_styles[index]; if (rs.SizeType == SizeType.AutoSize || (panel.AutoSize && rs.SizeType == SizeType.Percent)) { int max_height = panel.row_heights[index]; // Find the tallest control in the row for (int i = 0; i < columns; i++) { Control c = panel.actual_positions[i, index - rowspan]; if (c != null && c != dummy_control && c.VisibleInternal) { // Skip any controls not being sized in this pass. if (settings.GetRowSpan(c) != rowspan + 1) { continue; } // Calculate the maximum control height. if (c.AutoSize) { max_height = Math.Max(max_height, c.PreferredSize.Height + c.Margin.Vertical); } else { max_height = Math.Max(max_height, c.ExplicitBounds.Height + c.Margin.Vertical); } max_height = Math.Max(max_height, c.Height + c.Margin.Top + c.Margin.Bottom); } } // Subtract the height of prior rows, if any. for (int i = Math.Max(index - rowspan, 0); i < index; ++i) { max_height -= panel.row_heights[i]; } // If necessary, increase this row's height. if (max_height > panel.row_heights[index]) { max_height -= panel.row_heights[index]; panel.row_heights[index] += max_height; total_height -= max_height; } } } } index = 0; total_percent = 0; // Finally, assign the remaining space to Percent rows, if any. if (total_height > 0) { int percent_height = total_height; // Find the total percent (not always 100%) foreach (RowStyle rs in row_styles) { if (rs.SizeType == SizeType.Percent) { total_percent += rs.Height; } } // Divvy up the space.. foreach (RowStyle rs in row_styles) { if (rs.SizeType == SizeType.Percent) { int height_change = (int)(((rs.Height / total_percent) * percent_height) - panel.row_heights[index]); if (height_change > 0) { panel.row_heights[index] += height_change; total_height -= height_change; } } index++; } } if (total_height > 0) { // Find the last row that isn't an Absolute SizeType, and give it // all this free space. (Absolute sized rows need to retain their // absolute height if at all possible!) int row = row_styles.Count - 1; for (; row >= 0; --row) { if (row_styles[row].SizeType != SizeType.Absolute) { break; } } if (row < 0) { row = row_styles.Count - 1; } panel.row_heights[row] += total_height; } }
private void CalculateColumnRowSizes(TableLayoutPanel panel, int columns, int rows) { TableLayoutSettings settings = panel.LayoutSettings; panel.column_widths = new int[panel.actual_positions.GetLength(0)]; panel.row_heights = new int[panel.actual_positions.GetLength(1)]; int border_width = TableLayoutPanel.GetCellBorderWidth(panel.CellBorderStyle); Rectangle parentDisplayRectangle = panel.DisplayRectangle; TableLayoutColumnStyleCollection col_styles = new TableLayoutColumnStyleCollection(panel); foreach (ColumnStyle cs in settings.ColumnStyles) { col_styles.Add(new ColumnStyle(cs.SizeType, cs.Width)); } TableLayoutRowStyleCollection row_styles = new TableLayoutRowStyleCollection(panel); foreach (RowStyle rs in settings.RowStyles) { row_styles.Add(new RowStyle(rs.SizeType, rs.Height)); } // If we have more columns than columnstyles, temporarily add enough columnstyles if (columns > col_styles.Count) { for (int i = col_styles.Count; i < columns; i++) { col_styles.Add(new ColumnStyle()); } } // Same for rows.. if (rows > row_styles.Count) { for (int i = row_styles.Count; i < rows; i++) { row_styles.Add(new RowStyle()); } } while (row_styles.Count > rows) { row_styles.RemoveAt(row_styles.Count - 1); } while (col_styles.Count > columns) { col_styles.RemoveAt(col_styles.Count - 1); } // Figure up all the column widths int total_width = parentDisplayRectangle.Width - (border_width * (columns + 1)); int index = 0; // First assign all the Absolute sized columns.. foreach (ColumnStyle cs in col_styles) { if (cs.SizeType == SizeType.Absolute) { panel.column_widths[index] = (int)cs.Width; total_width -= (int)cs.Width; } index++; } index = 0; // Next, assign all the AutoSize columns.. foreach (ColumnStyle cs in col_styles) { if (cs.SizeType == SizeType.AutoSize) { int max_width = 0; // Find the widest control in the column for (int i = 0; i < rows; i++) { Control c = panel.actual_positions[index, i]; if (c != null && c != dummy_control && c.VisibleInternal) { if (settings.GetColumnSpan(c) > 1) { continue; } if (c.AutoSize) { max_width = Math.Max(max_width, c.PreferredSize.Width + c.Margin.Horizontal); } else { max_width = Math.Max(max_width, c.ExplicitBounds.Width + c.Margin.Horizontal); } if (c.Width + c.Margin.Left + c.Margin.Right > max_width) { max_width = c.Width + c.Margin.Left + c.Margin.Right; } } } panel.column_widths[index] = max_width; total_width -= max_width; } index++; } index = 0; float total_percent = 0; // Finally, assign the remaining space to Percent columns.. if (total_width > 0) { int percent_width = total_width; // Find the total percent (not always 100%) foreach (ColumnStyle cs in col_styles) { if (cs.SizeType == SizeType.Percent) { total_percent += cs.Width; } } // Divy up the space.. foreach (ColumnStyle cs in col_styles) { if (cs.SizeType == SizeType.Percent) { panel.column_widths[index] = (int)((cs.Width / total_percent) * percent_width); total_width -= panel.column_widths[index]; } index++; } } if (total_width > 0) { panel.column_widths[col_styles.Count - 1] += total_width; } // Figure up all the row heights int total_height = parentDisplayRectangle.Height - (border_width * (rows + 1)); index = 0; // First assign all the Absolute sized rows.. foreach (RowStyle rs in row_styles) { if (rs.SizeType == SizeType.Absolute) { panel.row_heights[index] = (int)rs.Height; total_height -= (int)rs.Height; } index++; } index = 0; // Next, assign all the AutoSize rows.. foreach (RowStyle rs in row_styles) { if (rs.SizeType == SizeType.AutoSize) { int max_height = 0; // Find the tallest control in the row for (int i = 0; i < columns; i++) { Control c = panel.actual_positions[i, index]; if (c != null && c != dummy_control && c.VisibleInternal) { if (settings.GetRowSpan(c) > 1) { continue; } if (c.AutoSize) { max_height = Math.Max(max_height, c.PreferredSize.Height + c.Margin.Vertical); } else { max_height = Math.Max(max_height, c.ExplicitBounds.Height + c.Margin.Vertical); } if (c.Height + c.Margin.Top + c.Margin.Bottom > max_height) { max_height = c.Height + c.Margin.Top + c.Margin.Bottom; } } } panel.row_heights[index] = max_height; total_height -= max_height; } index++; } index = 0; total_percent = 0; // Finally, assign the remaining space to Percent columns.. if (total_height > 0) { int percent_height = total_height; // Find the total percent (not always 100%) foreach (RowStyle rs in row_styles) { if (rs.SizeType == SizeType.Percent) { total_percent += rs.Height; } } // Divy up the space.. foreach (RowStyle rs in row_styles) { if (rs.SizeType == SizeType.Percent) { panel.row_heights[index] = (int)((rs.Height / total_percent) * percent_height); total_height -= panel.row_heights[index]; } index++; } } if (total_height > 0) { panel.row_heights[row_styles.Count - 1] += total_height; } }
private void btnGenerateMatrixSLAE_Click(object sender, EventArgs e) { //генерируем матрицу СЛАУ MatrixA = new NumericUpDown[CntVariables, CntVariables]; ResultB = new NumericUpDown[CntVariables]; //создаем контрол табличного размещения //ширина и высота равны ширине и высоте основной панели TableLayoutPanel tableLayoutPanel = new TableLayoutPanel() { Width = panelMatrixSLAE.Width, Height = panelMatrixSLAE.Height, Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom, AutoScroll = true, CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset }; //Стили колонок и строк TableLayoutColumnStyleCollection csc = tableLayoutPanel.ColumnStyles; TableLayoutRowStyleCollection rsc = tableLayoutPanel.RowStyles; tableLayoutPanel.ColumnCount = CntVariables + 1; //кол-во колонок = размер матрицы + столбец для результатов tableLayoutPanel.RowCount = CntVariables + 2; //кол-во строк = размер матрицы + строка для заголовка + строка для кнопки расчета //создаем и вставляем заголовок для матрицы Label labelMatrixA = new Label() { Text = "Матрица А", AutoSize = true, Dock = DockStyle.Fill, TextAlign = ContentAlignment.TopCenter }; tableLayoutPanel.Controls.Add(labelMatrixA, 0, 0); tableLayoutPanel.SetColumnSpan(labelMatrixA, CntVariables); //для заголовка стили размеров автоматические csc.Add(new ColumnStyle(SizeType.Percent, 100f / (CntVariables + 1))); rsc.Add(new ColumnStyle(SizeType.AutoSize)); int tabIndex = 0;//проставляем TabIndex //создаем контролы для матрицы СЛАУ for (int r = 0; r < CntVariables; r++) { csc.Add(new ColumnStyle(SizeType.Percent, 100f / (CntVariables + 1))); for (int c = 0; c < CntVariables; c++) { MatrixA[r, c] = CreateNumericUpDown(tabIndex++); MatrixA[r, c].Name = string.Format(matrixItemName, r, c); if (r != c) // Для быстрого заполнения симметричной матрицы добавляем событие отзеркаливания значения при его изменении { MatrixA[r, c].ValueChanged += NumericValueChanged_HelpFillSymmetricMatrix; } tableLayoutPanel.Controls.Add(MatrixA[r, c], c, r + 1); } tabIndex++; } //создаем и втсавляем заголовок для результатов Label labelResultB = new Label() { Text = "Результат B", AutoSize = true, Dock = DockStyle.Fill, TextAlign = ContentAlignment.TopCenter }; tableLayoutPanel.Controls.Add(labelResultB, CntVariables, 0); //добавляем контролы результатов tabIndex = -1; for (int i = 0; i < CntVariables; i++) { ResultB[i] = CreateNumericUpDown(tabIndex += CntVariables + 1, Color.LightBlue); rsc.Add(new ColumnStyle(SizeType.Percent, 100f / (CntVariables + 1))); tableLayoutPanel.Controls.Add(ResultB[i], CntVariables, i + 1); } //создаем кнопку для расчета СЛАУ и вставляем в контрол табличного размещения Button btnCalc = new Button() { Text = "Решить", Anchor = AnchorStyles.None, Dock = DockStyle.Fill, AutoSize = true }; btnCalc.Click += BtnCalc_Click; tableLayoutPanel.Controls.Add(btnCalc, 0, tableLayoutPanel.RowCount - 1); tableLayoutPanel.SetColumnSpan(btnCalc, tableLayoutPanel.ColumnCount); panelMatrixSLAE.Controls.Clear(); //очищаем панель panelMatrixSLAE.Controls.Add(tableLayoutPanel); // вставляем матрицу СЛАУ MatrixA[0, 0].Focus(); //делаем активным первый элемент в матрице }
public void Add(ColumnStyle columnStyle) { _columnStyles.Add(columnStyle); }
private void CalculateColumnRowSizes (TableLayoutPanel panel, int columns, int rows) { TableLayoutSettings settings = panel.LayoutSettings; panel.column_widths = new int[panel.actual_positions.GetLength (0)]; panel.row_heights = new int[panel.actual_positions.GetLength (1)]; int border_width = TableLayoutPanel.GetCellBorderWidth (panel.CellBorderStyle); Rectangle parentDisplayRectangle = panel.DisplayRectangle; TableLayoutColumnStyleCollection col_styles = new TableLayoutColumnStyleCollection (panel); foreach (ColumnStyle cs in settings.ColumnStyles) col_styles.Add( new ColumnStyle(cs.SizeType, cs.Width)); TableLayoutRowStyleCollection row_styles = new TableLayoutRowStyleCollection (panel); foreach (RowStyle rs in settings.RowStyles) row_styles.Add (new RowStyle (rs.SizeType, rs.Height)); // If we have more columns than columnstyles, temporarily add enough columnstyles if (columns > col_styles.Count) { for (int i = col_styles.Count; i < columns; i++) col_styles.Add(new ColumnStyle()); } // Same for rows.. if (rows > row_styles.Count) { for (int i = row_styles.Count; i < rows; i++) row_styles.Add (new RowStyle ()); } while (row_styles.Count > rows) row_styles.RemoveAt (row_styles.Count - 1); while (col_styles.Count > columns) col_styles.RemoveAt (col_styles.Count - 1); // Find the largest column-span/row-span values. int max_colspan = 0, max_rowspan = 0; foreach (Control c in panel.Controls) { max_colspan = Math.Max (max_colspan, settings.GetColumnSpan (c)); max_rowspan = Math.Max (max_rowspan, settings.GetRowSpan (c)); } // Figure up all the column widths int total_width = parentDisplayRectangle.Width - (border_width * (columns + 1)); int index = 0; // First assign all the Absolute sized columns.. foreach (ColumnStyle cs in col_styles) { if (cs.SizeType == SizeType.Absolute) { panel.column_widths[index] = (int)cs.Width; total_width -= (int)cs.Width; } index++; } // Next, assign all the AutoSize columns to the width of their widest // control. If the table-layout is auto-sized, then make sure that // no column with Percent styling clips its contents. // (per http://msdn.microsoft.com/en-us/library/ms171690.aspx) for (int colspan = 0; colspan < max_colspan; ++colspan) { for (index = colspan; index < col_styles.Count - colspan; ++index) { ColumnStyle cs = col_styles[index]; if (cs.SizeType == SizeType.AutoSize || (panel.AutoSize && cs.SizeType == SizeType.Percent)) { int max_width = panel.column_widths[index]; // Find the widest control in the column for (int i = 0; i < rows; i ++) { Control c = panel.actual_positions[index - colspan, i]; if (c != null && c != dummy_control && c.VisibleInternal) { // Skip any controls not being sized in this pass. if (settings.GetColumnSpan (c) != colspan + 1) continue; // Calculate the maximum control width. if (c.AutoSize) max_width = Math.Max (max_width, c.PreferredSize.Width + c.Margin.Horizontal); else max_width = Math.Max (max_width, c.ExplicitBounds.Width + c.Margin.Horizontal); max_width = Math.Max (max_width, c.Width + c.Margin.Left + c.Margin.Right); } } // Subtract the width of prior columns, if any. for (int i = Math.Max (index - colspan, 0); i < index; ++i) max_width -= panel.column_widths[i]; // If necessary, increase this column's width. if (max_width > panel.column_widths[index]) { max_width -= panel.column_widths[index]; panel.column_widths[index] += max_width; total_width -= max_width; } } } } index = 0; float total_percent = 0; // Finally, assign the remaining space to Percent columns, if any. if (total_width > 0) { int percent_width = total_width; // Find the total percent (not always 100%) foreach (ColumnStyle cs in col_styles) { if (cs.SizeType == SizeType.Percent) total_percent += cs.Width; } // Divvy up the space.. foreach (ColumnStyle cs in col_styles) { if (cs.SizeType == SizeType.Percent) { int width_change = (int)(((cs.Width / total_percent) * percent_width) - panel.column_widths[index]); if (width_change > 0) { panel.column_widths[index] += width_change; total_width -= width_change; } } index++; } } if (total_width > 0) { // Find the last column that isn't an Absolute SizeType, and give it // all this free space. (Absolute sized columns need to retain their // absolute width if at all possible!) int col = col_styles.Count - 1; for (; col >= 0; --col) { if (col_styles[col].SizeType != SizeType.Absolute) break; } if (col < 0) col = col_styles.Count - 1; panel.column_widths[col] += total_width; } // Figure up all the row heights int total_height = parentDisplayRectangle.Height - (border_width * (rows + 1)); index = 0; // First assign all the Absolute sized rows.. foreach (RowStyle rs in row_styles) { if (rs.SizeType == SizeType.Absolute) { panel.row_heights[index] = (int)rs.Height; total_height -= (int)rs.Height; } index++; } index = 0; // Next, assign all the AutoSize rows to the height of their tallest // control. If the table-layout is auto-sized, then make sure that // no row with Percent styling clips its contents. // (per http://msdn.microsoft.com/en-us/library/ms171690.aspx) for (int rowspan = 0; rowspan < max_rowspan; ++rowspan) { for (index = rowspan; index < row_styles.Count - rowspan; ++index) { RowStyle rs = row_styles[index]; if (rs.SizeType == SizeType.AutoSize || (panel.AutoSize && rs.SizeType == SizeType.Percent)) { int max_height = panel.row_heights[index]; // Find the tallest control in the row for (int i = 0; i < columns; i++) { Control c = panel.actual_positions[i, index - rowspan]; if (c != null && c != dummy_control && c.VisibleInternal) { // Skip any controls not being sized in this pass. if (settings.GetRowSpan (c) != rowspan + 1) continue; // Calculate the maximum control height. if (c.AutoSize) max_height = Math.Max (max_height, c.PreferredSize.Height + c.Margin.Vertical); else max_height = Math.Max (max_height, c.ExplicitBounds.Height + c.Margin.Vertical); max_height = Math.Max (max_height, c.Height + c.Margin.Top + c.Margin.Bottom); } } // Subtract the height of prior rows, if any. for (int i = Math.Max (index - rowspan, 0); i < index; ++i) max_height -= panel.row_heights[i]; // If necessary, increase this row's height. if (max_height > panel.row_heights[index]) { max_height -= panel.row_heights[index]; panel.row_heights[index] += max_height; total_height -= max_height; } } } } index = 0; total_percent = 0; // Finally, assign the remaining space to Percent rows, if any. if (total_height > 0) { int percent_height = total_height; // Find the total percent (not always 100%) foreach (RowStyle rs in row_styles) { if (rs.SizeType == SizeType.Percent) total_percent += rs.Height; } // Divvy up the space.. foreach (RowStyle rs in row_styles) { if (rs.SizeType == SizeType.Percent) { int height_change = (int)(((rs.Height / total_percent) * percent_height) - panel.row_heights[index]); if (height_change > 0) { panel.row_heights[index] += height_change; total_height -= height_change; } } index++; } } if (total_height > 0) { // Find the last row that isn't an Absolute SizeType, and give it // all this free space. (Absolute sized rows need to retain their // absolute height if at all possible!) int row = row_styles.Count - 1; for (; row >= 0; --row) { if (row_styles[row].SizeType != SizeType.Absolute) break; } if (row < 0) row = row_styles.Count - 1; panel.row_heights[row] += total_height; } }