Home

Controls Properties:
The Location of a Control

 

Introduction

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 to the left. The horizontal measurements move from the origin to the right. The vertical measurements move from the origin to the bottom.

In the previous lesson, we saw how to position a control on a form. The position of a control on a form (or a container) is referred to as its location. The location is defined by two values:

  • The horizontal value of a location is the distance from the left border of a form (or container) to the left border of a control
  • The vertical value of a location is the distance from the top border of a form (or container) to the top border of a control

The location of a Windows control can be illustrated as follows:

Location

The location of a control is programmatically specified using a structure called Point. To identify the concept of a point, the System.Drawing namespace provides the Point structure. The Point structure is defined in the System.Drawing.dll. Therefore, if you want to use a Point object in your application, include that assembly in your application:

Add Reference

After adding the reference, you can access the Point object. One of the properties of the Point structure is X, which represents the horizontal distance of the point from the top-left corner of the object that owns the point. Another property, Y, represents the vertical measurement of the point with regards to the top-left corner of the object that owns the point. Based on this, a Point object can be represented on the Windows coordinate system as follows:

Representation of a Point

To visually specify the location of a control, access its Properties window, and use the Location field:

  • You can enter two numbers separated by a comma
  • You can expand the Location field, click X and type a value; click Y and type a value

Programmatically Locating a Control

You can programmatically specify the location of a control. To support this, each control has a property named Location. To assist you with specifying the location of a control, the Point class provides a constructor as follows:

Public Sub New(x As Integer, y As Integer)

This constructor takes a left and a top arguments. Here is an example of programmatically setting the location of a control:

Imports System
Imports System.Drawing
Imports System.Windows.Forms

Module Exercise

    Public Class WinControls
        Inherits Form

        Private btnSubmit As Button

        Dim components As System.ComponentModel.Container

        Public Sub New()
            InitializeComponent()
        End Sub

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

            btnSubmit.Location = New Point(100, 40)

            Controls.Add(btnSubmit)
        End Sub

        Public Shared Function Main() As Integer

            Application.Run(New WinControls())
            Return 0

        End Function

    End Class

End Module

You can also use an existing Point object and assign it to the Location property of the control. Here is an example:

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

            Dim pt As Point = New Point()
            pt.X = 100
            pt.Y = 40
            btnSubmit.Location = pt

            Controls.Add(btnSubmit)
End Sub

You can also retrieve the location of a control and store it in a Point object. To do this, you can simply assign the Location property of a control to a Point object. 

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

            btnSubmit.Location = New Point(100, 40)

            Dim pt As Point = btnSubmit.Location

            Controls.Add(btnSubmit)
End Sub

When studying control alignment, we saw that you could use the location of one control as a reference to position another control. You can also do this programmatically by retrieving the location of one control and changing either its X or its Y values to position the new control. Here is an example:

Imports System
Imports System.Drawing
Imports System.Windows.Forms

Module Exercise

    Public Class WinControls
        Inherits Form

        Private btnSubmit As Button
        Dim txtEmployeeName As TextBox

        Dim components As System.ComponentModel.Container

        Public Sub New()
            InitializeComponent()
        End Sub

        Public Sub InitializeComponent()
            Dim pt As Point

            btnSubmit = New Button()
            btnSubmit.Text = "Submit"
            btnSubmit.Location = New Point(100, 40)

            txtEmployeeName = New TextBox()

            pt = btnSubmit.Location
            txtEmployeeName.Location = New Point(pt.X, pt.Y + 32)

            Controls.Add(btnSubmit)
            Controls.Add(txtEmployeeName)
        End Sub

        Public Shared Function Main() As Integer

            Application.Run(New WinControls())
            Return 0

        End Function

    End Class

End Module

This would produce:

The Point structure provides other constructors you can use for the location of a control.

 

Home Copyright © 2008 FunctionX, Inc. Home