Home

Controls Properties: The Text

 

Control Identification

 

Introduction

Most of the controls you will use to create your applications are defined in the .NET Framework and each is based on a particular class. To provide them with basic and common characteristics, all visual Windows controls of the .NET Framework are based on a class called Control which is available in the System.Windows.Forms namespace of the System.Windows.Forms.dll assembly. Based on this, the characteristics common to .NET Framework graphical controls are accessed and managed from one point, then inherited by those controls.

Auto-Sizing a Control

In the previous lesson, we saw how you could visually change the size of a control. In reality, many pre-requisites must be met and many options are available. After adding a control to a form, we saw that it would be surrounded by 8 small squares In reality, this is not true for all controls. 

Auto Sizing Auto Sizing

Notice that some controls, such as the label, display one square handle. Some other controls, such as the text box, display 2 square handles. And then some controls display 8 square handles:

  • The presence of the single square button indicates that the control can only be moved: it cannot be resized (at that time)
  • Besides the possibility of moving it, the presence of only two square handles indicates that the control can only be enlarged or narrowed: it cannot be heightened or shrunk
  • The presence of the 8 square handles means that the control's size can be changed in all 8 directions

Many controls (not all), such as the Label , are configured to resize themselves as you provide a string to their Text field in the Properties window. If you type a long string, the control would be made large enough to show the whole text. Some other controls, such as the Button , will not resize themselves if their text is too long. On the following form, both the button and the label received the same text but, while the label shows the whole string, the button can only show the length that its width can afford:

The ability of a control to enlarge itself to accommodate its long is controlled by a Boolean property named AutoSize. For some controls, the default value of this property is False. For some other controls, this property is defaulted to True. Therefore, use this property to control whether a control on the form can be resized or not.

To programmatically specify the auto-sizing option of a control, access its AutoSize property and assign it the desired Boolean value. Here is an example:

Public Sub InitializeComponent()
            btnSubmit = New Button()
            btnSubmit.Text = "Submit"

            btnSubmit.AutoSize = True

            Controls.Add(btnSubmit)
End Sub
 

Home Copyright © 2008 FunctionX, Inc. Home