How to: Resize Controls Within Worksheet Cells
Applies to |
---|
The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office. Project type
Microsoft Office version
For more information, see Features Available by Application and Project Type. |
When you resize columns or rows on a worksheet, any host controls contained in the cells automatically resize to the height or width of the cell that was resized. Windows Forms controls do not resize automatically by default.
If you add the controls at design time, you must set positioning options for each control.
If you add a Windows Forms control programmatically and supply a range argument, the control automatically resizes when a cell within the range is resized. For more information, see Adding Controls to Office Documents at Run Time.
Resizing Controls at Design Time
To make controls resize with cells at design time
From the Toolbox, drag a Windows Forms control to a worksheet.
Right-click the control, and then click Format Control.
In the Format Control dialog box, click the Properties tab.
Under Object Positioning, select the Move and size with cells option, and then click OK.
When you resize the cell that contains the control, the control resizes to fit the cell.
Resizing Controls at Run Time
If you add a Windows Forms control at run time and pass in a Range as the location for the control, the control will automatically resize when the worksheet cell that contains the range is resized.
To make controls resize with cells at run time
Add a control to range A1.
Dim control1 As Microsoft.Office.Tools.Excel.Controls.Button = _ Me.Controls.AddButton(Me.Range("A1"), "control1")
Microsoft.Office.Tools.Excel.Controls.Button control1 = this.Controls.AddButton(this.Range["A1", missing], "control1");
When you resize the cell that contains the control, the control resizes to fit the cell.
Resetting Control Placement
You can reset the placement and resizing of the control by setting the Placement property to one of the following XlPlacement values:
To change the behavior of a control so that it does not resize or move with the cell
Call the placement property of the control and set the value to xlFreeFloating.
control1.Placement = Microsoft.Office.Interop.Excel.XlPlacement.xlFreeFloating
control1.Placement = Microsoft.Office.Interop.Excel.XlPlacement.xlFreeFloating;
See Also
Tasks
How to: Add Windows Forms Controls to Office Documents
How to: Hide Controls on Worksheets when Printing
Concepts
Adding Controls to Office Documents at Run Time
Limitations of Windows Forms Controls on Office Documents