Common Properties and Methods of Controls

By

Common Properties and Methods of Controls


 
Regular Properties

 
Control's Names

To create a control, the primary piece of information you must provide is its name. This allows you and the compiler to know what control you are referring to when the program is running. Specifying the name of a control may depend on the technique you decide to use to create the control.
After adding a control to a form, it automatically receives a name. In the Properties window, the control’s name displays in the (Name) field. The newly added control reflects the name of its button on the Properties window and adds an incremental number. For example, if you click the TextBox button on the Toolbox and click the form, the control would be named TextBox1. If you add a second TextBox control, it would be named TextBox2. This causes the default names to be incremental. Since a program usually consists of many controls, it is usually a good idea to rename your controls and give them meaningful and friendlier names. The name should help you identify what the button is used for. To change the name of a control, on the Properties window, click the (Name) field, type the desired name and press Enter.
 
The Text of a Control

Some controls, as we will see, are text-based, meaning they are meant to display or sometimes request text from the user. For such controls, this text is referred to as caption while it is simply called text for some other controls. This property is not available for all controls.
If a control displays text, it then has a Text property in the Properties window. After adding such a control to a form, its Text field would hold the same text as its name. At design time, to change the text of the control, click its Text field and type the desired value. For most controls, there are no strict rules to follow for this text. Therefore, it is your responsibility to type the right value.
The text provided in a Text field of a text-based control can only be set “as is” during design. If you want the text to change while the application is running, you can format it. For example, such a control can display the current time or the name of the user who is logged in. These format attributes cannot be set at deign time. To change the text of a text-based control at run time, either assign a simple string or provide a formatted string to the Text property.
 
Control's Visibility

A control is referred to as visible if it can be visually located on the screen. A user can use a control only if he or she can see it. As a programmer, you have the role of deciding whether a control must be seen or not and when. The visibility of an object is controlled by the its Visible property. This is a Boolean property.
At design time, when you add a control to a parent, it is visible by default. This is because its Visible property is set to True in the Properties window. In the same way, if you programmatically create a control, by default, it is made visible once you add it to its parent's Controls property.
If you don't want a control to primarily appear when the form comes up, you can either set its Visible property to False or set its parent’s visible property to False. Equivalently, at run time, to hide a control, assign a False value to either its Visible property or its parent’s Visible property. Remember that, as stated already, when a parent gets hidden, it also hides its children. On the other hand, a parent can be made visible but hide one or some of its children.
To check whether a control is visible at one time, apply a conditional statement (if or while) to its Visible property and check whether its value is True or False. In the following example, when the user clicks a form, a text box named TextBox1 reverses its visibility state:
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
        Me.TextBox1.Visible = Not Me.TextBox1.Visible
End Sub
Besides the Visible property, you can also display a control using the Show() method. Its syntax is:
Public Sub Show()
When you can this method, if the control is visible, nothing would happen. If it were hidden, then it would be revealed. In the following example, a previously hidden button is asked to display:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button2.Show()
End Sub
The Show() method internally sets the Visible property to true. We also saw that, to hide a control, you can set its Visible property to False. In the same way, to hide a control, you call can the Control.Hide() method. Its syntax is:
Public Sub Hide()
Keep in mind that hiding a control does not close or destroy it.
 
Control's Availability

For the user to use a control, it must allow it. For example, if a control is supposed to receive text, the user can enter characters in it only if this is made possible. To make a control available to the user, the object must be enabled. The availability of an object is controlled by the Boolean Enabled property.
By default, after adding a control to a form, it is enabled and its Enabled property in the Properties window is set to True. An enabled control displays its text or other characteristics in their normal settings. If you want to disable a control, set its Enabled property to False. In the following picture, a text box with the Text set to TextBox1 and a button with the Text set to Button2 are disabled:
To find out whether a control is enabled or not, check its Enabled property state.
 
Focus

The focus is a visual aspect that indicates that a control is ready to receive input from the user. In fact, to use a control, the user must first give it focus. Various controls have different ways of expressing that they have received focus.
Button controls indicate that they have focus by making a dotted rectangle around their caption. In the following picture, the button on the right has focus:
A text-based control indicates that it has focus by displaying a blinking cursor. A list-based control indicates that it has focus when one of its items has a surrounding dotted rectangle:
To give focus to a control, the user can press a key such as the Tab key. To programmatically give focus to a control, call the Focus() method. Here is an example:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
   Handles Button1.Click
        Me.ListBox1.Focus()
End Sub
 
Tab Ordering

A user can navigate through controls using the Tab key. When that key has been pressed, the focus moves from one control to the next. By their designs, not all controls can receive focus and not all controls can participate in tab navigation. Even controls that can receive focus must be primarily included in the tab sequence.

At design time, the participation to tab sequencing is controlled by the Boolean TabStop property in the Properties window. Every visual control that can receive focus is already configured to have this property set to True. If you want to remove a control from this sequence, set its TabStop value to False.
If a control has the TabStop property set to True, to arrange the navigation order of controls, at design time, you can click a control on the form. Then, on the Properties window, change the value of its TabIndex field. The value must be a positive short integer.
 
Control's Location

The controls added to a parent window are confined to the area of the body offered by that window. After adding it to a window, the control is positioned in the body of the parent using a Cartesian coordinate system whose origin is located on the top-left corner of the parent window. If the parent is a form, the origin is located just under the title bar left. The horizontal measurements move from the origin to the right. The vertical measurements move from the origin to the bottom:
The origin of the coordinate system and its axes
The distance from the control’s left border to the parent’s left border is referred to as the Left property. The distance from the control’s top border to the parent’s top border is referred to as the Top property. The Left and Top values are known as the control’s location. This can be illustrated as follows:
The location of a control inside a parent window
When you click a control on the Toolbox and click its parent window, the Left and Top values are set where the mouse landed. One of the operations you will perform during design consists of moving controls around to give them a better location and take advantage of the form's real estate. Various options are available to do this. To assist you with setting control's location, the form designer draws aligned dots on the form, forming columns and rows.
To move one control, click and hold the mouse on it, then drag in the desired direction, and release the mouse. This technique allows you to move a control one unit at a time in either direction. You can also click the control, then press one of the arrow keys to move one unit at a time, either left to move the control left, up to move the control up, right to move the control in the right direction, or down to move the control down.
To move a control with more precision, click the control to select it, press and hold Ctrl, then click one of the arrow keys, either left to move the control left, up to move the control up, right to move the control in the right direction, or down to move the control down.
To move a group of control, select them first. Then click and drag any area in the selection to the desired location. Alternatively, once you have  the controls, you can press one of the arrow keys to move the whole group.
When moving either a control or a group using either the mouse or the keyboard, the control or the group would follow the grids on the form and it can move only one grid mark at a time. This allows you to have a better alignment of controls. If you want to move the control or the group in smaller units than those of the grid, press and hold Ctrl. Then press one of the arrow keys. Once the control or the group is positioned to your liking, release the Ctrl key.
To programmatically move a control, which is equivalent to changing the values of the Left or the Top properties at run time, assign the desired respective values. If you set a negative value for the Left field, the left border of the control would be hidden. In the same way, a negative Top value would hide the top border of the control. Make sure you use valid integer values; otherwise you would receive an error when you compile the project.
 
Control's Dimensions

The distance from the left border to the right border of a control is referred to as its Width property. In the same way, the distance from the top to the bottom borders of a control is its Height value. This can be illustrated as follows:
The location and dimension of a control

If you click a control’s button on the Toolbox and click its parent window, the control assumes some default dimensions in the body of the parent. Most controls, including the form, can be resized using guiding mouse cursors. To resize a control, first select it. Except for the form, whenever a control is selected, there are eight handles around it. To resize the control, position your mouse on one of the handles. The mouse pointer will change, indicating in what direction you can move to resize the control.
Cursor Role
Moves the seized border in the North-West <-> South-East direction
Shrinks or heightens the control
Moves the seized border in the North-East <-> South-West direction
Narrows or enlarges the control

To resize the control by one grid unit, click one of the handles and drag in the desired location. Once satisfied, release the mouse.
To heighten or shrink a control by its lower border and one unit at a time, select the control, press and hold Shift. Then press the down arrow key as many times as necessary. Once satisfied, release the mouse and Shift.
To heighten or shrink a control by its lower border by small units, select the control, press and hold Ctrl and Shift. Then press the down arrow key as many times as necessary. Once satisfied, release the mouse and Ctrl and Shift.
 
The Bounding Rectangle of a Control

When a control has been added to a container, it occupies a Rectangle represented by the Bounds property. The syntax of this property is:
Public Property Bounds As Rectangle
At any time, to get the location and the dimensions of a control, call its Bounds property, which produces a Rectangle value.
 
Using External Libraries

 
Introduction

The Windows controls featured in the .NET Framework are highly varied and provide all the necessary regular functionality a normal application would need. They do this through various properties and their different methods. To enhance their functionality and speed up application development, Visual Basic offers a very extended library of functions to cover different issues including mathematics, finance, date, time, and commerce, etc. Because Visual Basic runs on Microsoft Windows, some functionality is available only as part of then operating system. This is done throughout a vast collection of functions and structures. Because these Win32 resources are not part of Visual Basic, if you want to use them, you must explicitly "load" them into your application
 
Using a Win32 Procedure

Before using a procedure that is part of the operating system's libraries, you must be familiar with it. This is simply done by checking the Win32 documentation or the Visual Studio help files. Most procedures are fairly documented, even though there are no examples for some of them.
To use a Win32 function, you can first declare its name and include its library. This is done using the Declare keyword followed by the name of the procedure and the name of the library in which the procedure is defined. You should also provide a friendly name of the procedure as you intend to call it in your code. Once the procedure is defined, you can then call it in your application. Here is an example that prevents the user from moving a form because, every time there is an attempt, the compiler calls the Win32 API's ReleaseCapture() function to take over:
Public Class Form1
    Inherits System.Windows.Forms.Form
    Declare Auto Function ReleaseTheMouse Lib "user32.dll" _
        Alias "ReleaseCapture" () As Boolean
#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    . . . No Change

#End Region

    Private Sub Form1_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Move
        ReleaseTheMouse()
    End Sub
End Class

0 comments:

Posting Komentar