public void TestExtenderMethods () { TableLayoutPanel p = new TableLayoutPanel (); Control c = new Button (); Assert.AreEqual (new TableLayoutPanelCellPosition (-1, -1), p.GetCellPosition (c), "A1"); Assert.AreEqual (-1, p.GetColumn (c), "A2"); Assert.AreEqual (1, p.GetColumnSpan (c), "A3"); Assert.AreEqual (-1, p.GetRow (c), "A4"); Assert.AreEqual (1, p.GetRowSpan (c), "A5"); p.SetCellPosition (c, new TableLayoutPanelCellPosition (1, 1)); Assert.AreEqual (new TableLayoutPanelCellPosition (1, 1), p.GetCellPosition (c), "A6"); p.SetColumn (c, 2); Assert.AreEqual (2, p.GetColumn (c), "A7"); p.SetRow (c, 2); Assert.AreEqual (2, p.GetRow (c), "A9"); p.SetColumnSpan (c, 2); Assert.AreEqual (2, p.GetColumnSpan (c), "A8"); p.SetRowSpan (c, 2); Assert.AreEqual (2, p.GetRowSpan (c), "A10"); Assert.AreEqual (new TableLayoutPanelCellPosition (2, 2), p.GetCellPosition (c), "A11"); // ??????? //Assert.AreEqual (new TableLayoutPanelCellPosition (-1, -1), p.GetPositionFromControl (c), "A12"); //Assert.AreEqual (c, p.GetControlFromPosition(0, 0), "A13"); }
/// <summary> /// Copies the last row of a This and all controls in it. /// </summary> /// <param name="This">This TableLayoutPanel.</param> /// <param name="controlCallback">The control callback if any, passing targetRow,targetColumn,sourceControl,targetControl.</param> /// <param name="allowSuspendingLayout">if set to <c>true</c> allows suspending the This layout during this process to prevent flickering.</param> public static void CopyLastRow(this TableLayoutPanel This, Action <int, int, Control, Control> controlCallback = null, bool allowSuspendingLayout = true) { #if NET35 Debug.Assert(This != null); #else Contract.Requires(This != null); #endif if (allowSuspendingLayout) { This.SuspendLayout(); } var lastRow = This.RowCount - 1; // add line This.RowCount++; // add style var rowStyle = This.RowStyles[lastRow]; This.RowStyles.Add(new RowStyle(rowStyle.SizeType, rowStyle.Height)); // copy controls var alreadyVisitedControls = new Dictionary <Control, bool>(); for (var i = This.ColumnCount; i > 0;) { --i; var control = This.GetControlFromPositionFixed(i, lastRow); if (control == null || !alreadyVisitedControls.TryAdd(control, true)) { continue; } var newControl = control.Duplicate(); if (controlCallback != null) { controlCallback(lastRow + 1, i, control, newControl); } This.Controls.Add(newControl, i, lastRow + 1); This.SetRowSpan(newControl, This.GetRowSpan(control)); This.SetColumnSpan(newControl, This.GetColumnSpan(control)); } if (allowSuspendingLayout) { This.ResumeLayout(true); } }
/// <summary> /// Resizes editor controls to fit the last column /// </summary> /// <param name="tablePanel"></param> private static void ResizeEditorControls(TableLayoutPanel tablePanel) { tablePanel.SuspendLayout(); try { // Make editors small for (int row = 0; row < tablePanel.RowCount; ++row) { Control editorControl = tablePanel.GetControlFromPosition(tablePanel.ColumnCount - 1, row); Option o = (editorControl != null) ? editorControl.Tag as Option : null; if ((o == null) || (editorControl is Label)) { continue; } // Change editorControl.Anchor = AnchorStyles.None; editorControl.Width = 5; } // Layout once tablePanel.ResumeLayout(true); tablePanel.SuspendLayout(); // Size now int[] colWidths = tablePanel.GetColumnWidths(); for (int row = 0; row < tablePanel.RowCount; ++row) { Control editorControl = tablePanel.GetControlFromPosition(tablePanel.ColumnCount - 1, row); Option o = (editorControl != null) ? editorControl.Tag as Option : null; if ((o == null) || (editorControl is Label)) { continue; } // Change int colWidth = 0; for (int col = 0; col < tablePanel.GetColumnSpan(editorControl); col++) { colWidth += colWidths[colWidths.Length - 1 - col]; } editorControl.Width = colWidth - 10; editorControl.Anchor = AnchorStyles.Left | AnchorStyles.Top; } } finally { tablePanel.ResumeLayout(true); } }