Поделиться через


How to: Resize Controls Within Worksheet Cells

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.

Applies to: The information in this topic applies to document-level projects for Excel 2007 and Excel 2010. For more information, see Features Available by Office Application and Project Type.

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

  1. From the Toolbox, drag a Windows Forms control to a worksheet.

  2. Right-click the control, and then click Format Control.

  3. In the Format Control dialog box, click the Properties tab.

  4. 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 Microsoft.Office.Interop.Excel.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

Other Resources

Controls on Office Documents