Visual Studio Command Bar for Arranging Controls on Documents and Worksheets (McLean Schofield)

  • Comments 2

One of the lesser-known features of Visual Studio Tools for Office is the Microsoft Office Word and Microsoft Office Excel command bar. When you are developing a document-level project for Word or Excel (for example, a Word Document or Excel Workbook project) and you have the document or one of your worksheets open in the designer, this command bar appears in Visual Studio, just above the designer. In Excel, this command bar looks like the following by default.

image 

The first several buttons in this command bar are for changing the keyboard scheme and mapping XML to the document or worksheet. This post is about the purpose of the rest of the buttons.

If you are designing a custom UI for your document or worksheet that includes Windows Forms controls that you add by using the designer, you can use these buttons to arrange the controls with a single click. When you select multiple controls in the designer, these buttons become enabled.

image 

From left to right, here are the buttons you can use:

  • Align Lefts. This button moves all of the controls to the left, so that they are aligned with the left side of the left-most control.

  • Align Centers. This button moves all of the controls left or right so that they are aligned around the vertical axis of the widest control.

  • Align Rights. This button moves all of the controls to the right, so that they are aligned with the right side of the right-most control.

  • Align Tops. This button moves all of the controls up, so that they are aligned with the top side of the upper-most control.

  • Align Middles. This button moves all of the controls up or down, so that they are aligned around the horizontal axis of the tallest control.

  • Align Bottoms. This button moves all of the controls down, so that they are aligned with the bottom side of the bottom-most control.

  • Make Horizontal Spacing Equal. The left-most and right-most control remain in place; the controls in between move horizontally so that there is equal horizontal space between each control.

  • Make Vertical Spacing Equal. The top-most and bottom-most control remain in place; the controls in between move vertically so that there is equal vertical space between each control.

A special note about Word. In Word projects, the alignment buttons are enabled only if the selected controls are not in line with text. By default, controls that you add to a Word document at design time are in line with text. To change the layout style of the control, right-click the control and then click Format Control. Then, on the Layout tab, select a wrapping style other than In line with text:

image

Examples. Enough words! Here are some examples that show these alignment options in action. These examples are pretty contrived, but they should give you an idea of the effects of each option.

Given the following buttons on a worksheet:

image

Align Lefts rearranges the controls as follows:

image

Align Centers rearranges the controls as follows:

image

And Align Rights rearranges the controls as follows:

image

Given the following buttons on a worksheet:

image 

Align Tops rearranges the controls as follows:

image 

Align Middles rearranges the controls as follows:

image

And Align Bottoms rearranges the controls as follows:

image

Given the following buttons on a worksheet:

image

Make Horizontal Spacing Equal rearranges the controls as follows:

image

Finally, given the following buttons on a worksheet:

image 

Make Vertical Spacing Equal rearranges the controls as follows:

image 

I hope these options come in handy then next time you are adding controls to a document or worksheet in VSTO.

-------------------------------------------------

McLean Schofield, Programming Writer

Leave a Comment
  • Please add 6 and 6 and type the answer here:
  • Post
Page 1 of 1 (2 items)

Visual Studio Command Bar for Arranging Controls on Documents and Worksheets (McLean Schofield)