1. Technology

C# Programming Tutorial - Programming Advanced Winforms in C#

By

9 of 10

Common Control Properties you should know
Using Anchors

You can select multiple controls at the same time by holding down the shift key when you select the second and subsequent controls, even controls of different types. The Properties window shows just those properties common to both, so you can set them all to the same size, color and text fields etc. Even the same event handlers can be assigned to multiple controls.

Anchors Aweigh

Depending on the use, some forms will often end up being resized by the user. Nothing looks worse than resizing a form and seeing controls stay in the same position. All controls have anchors that let you "attach" them to the 4 edges so that the control moves or stretches when an attached edge is moved. This leads to the following behavior when a form is stretched from the right edge:

  1. Control Attached to Left but not right. - It Doesn't move or stretch (bad!)
  2. Control attached to both left and right edges. It stretches when the form is stretched.
  3. Control attached to right edge. It moves when the form is stretched.

For buttons like Close which are traditionally in the bottom right, behavior 3 is what is needed. ListViews and DataGridViews are best with 2 if the number of columns is enough to overflow the form and needs scrolling). The Top and Left anchors are the default. The Property Window includes a nifty little editor that looks like the England Flag. Just click any of the bars (two horizontal and two vertical) to set or clear the appropriate anchor, as shown in the picture above.

Tagging Along

One property that doesn't get much mention is the Tag property and yet it can be incredibly useful. In the Properties Window you can only assign text but in your code you can have any value that descends from Object.

I've used Tag to hold an entire object while only showing a few of its properties in a ListView. For instance you might only want to show a Customer Name and number in a Customer Summary list. But right click on the selected customer and then open a form with all the customer's details. This is easy if when you build up the customer list by reading all customer's details in memory and assigning a reference to the Customer Class Object in the Tag. All controls have a Tag.


On the next page: How to work with TabControls
  1. About.com
  2. Technology
  3. C / C++ / C#
  4. C# / C Sharp
  5. Learn C Sharp
  6. Common Control Properties you should know

©2014 About.com. All rights reserved.