User Tools

Site Tools


k20:k20.7:start

20.7 Classes Align and Arrange (gb.qt4)

You will use these two classes for the arrangement of components in a container or for the alignment of, for example, text in a text box or an image in a picture box.

20.7.1 Class Align

This static class provides constants and 6 methods that are used to specify the alignment of objects or text, provided they have the Alignment property. The Draw.Text method also uses the constants of the Align class.

20.7.1.1 Alignment of visible objects

To specify the alignment of visible objects, knowledge of the direction matrix is as necessary as the associated constants for the designated directions:

AlignConstantSymbolDescription
.BottomLeft33Alignment horizontal left and vertical bottom
.Bottom35Alignment horizontally centred and vertically below
.BottomRight34Alignment horizontal right and vertical bottom
.Left1Alignment horizontal left and vertical centre
.Center3Alignment horizontal and vertical centre
.Right2Alignment horizontal right and vertical centre
.TopLeft17Alignment horizontal left and vertical top
.Top19Alignment horizontally centred and vertically on top
.TopRight18Alignment horizontal right and vertical top

Table 20.7.1.1: Direction matrix - constants for the Alignment property.

20.7.1.2 Alignment of text

If you use the alignment of text, for example, with the editor TextEdit to specify the alignment of text, note the dependence on the writing direction of the language used:

AlignConstantDescription
.Normal0Alignment vertically centred and horizontally according to the writing direction
.TopNormal16Alignment vertically on top and horizontally according to the writing direction
.BottomNormal32Alignment vertically bottom and horizontally according to the writing direction
.Justify4Alignment horizontally and vertically centred

Table 20.7.1.2.1: Alignment of text according to writing direction

20.7.1.3 Set and read the values of the Alignment property

All property values from the upper tables can be set or read out using the 6 methods IsBottom, IsCenter, IsLeft, IsMiddle, IsRight and IsTop, as shown in Example 1:

Public Sub btnSetCenter_Click()
  If Not Align.IsCenter(PictureBox1.Alignment) Then
     PictureBox1.Alignment = Align.Center
  Endif
End

Example 2

This is how you set the text alignment of a text box - analogously also of a MaskBox - and a textarea:

...
  TextBox1.Text = "Ausrichtung"
  TextBox1.Alignment = Align.Right
  TextArea1.Alignment = Align.Normal
...

If, on the other hand, you want to align the current paragraph or the selected text in the TextEdit editor, set the property TextEdit.Format.Alignment with the 4 alignments Normal, Left, Center and Right :

Public Sub btnSetProperty_Click()
  TextEdit1.Format.Alignment = Align.Center
  TextEdit1.Format.Color = Color.Red
  TextEdit1.Format.Background = Color.LightGray
  TextEdit1.Format.Font = Font["FreeMono,14"]
End

20.7.2 Class Arrange

This class is static. It provides constants that can be used to set the Arrange property of many components if they have the Arrange property. Only components that act as containers for other components such as Shape, Panel, Expander, DrawingArea1 or TabStrip have the Arrangement property. The arrangement of components must always be specified relative to the container.

ArrangeConstantDescription
.None0Default value. The arrangement of the components in the client area of the container is not affected.
.Horizontal1The components in the container are arranged next to each other and are given the height of the container. Their width is not affected. The component in the container whose value Component.X is the smallest is displayed on the left. If these values are the same, the position in the hierarchy decides.
.Vertical2The components in the container are arranged below each other and receive the width of the container. Their height is not affected. The component in the container whose value Component.Y is the smallest is displayed at the top. If these values are the same, the position in the hierarchy decides.
.Column--
.TopBottom4The components are stacked in a column in the container if the height is sufficient. Otherwise another column is created.
.Row--
.LeftRight3The components are arranged in a row in the container, if the width is sufficient. Otherwise another row is created under the existing one.
.Fill5One component in the container fills the entire client area in the container. Multiple components do not make sense. If there are nevertheless several components in the container, then the component that is in the last position in the hierarchy - in relation to the container - is displayed.

Table 20.7.2.1: Constants for the Arrangement Property

20.7.2.1 Setting and Reading the Values of the Arrangement Property

You can use the values specified in Table 20.7.2.1 to arrange components in a container. When arranging components in a container, you can make further adjustments using the container properties Spacing and Padding:

  • Container.Padding - The integer value (pixels) determines the distance of a component from the edge of the container.
  • Container.Spacing - The integer value (pixels) determines the distance between individual components in the container.

Example 1

Three buttons are added to a Panel1. The following properties are set:

  • Panel1.W = 280
  • Panel1.Spacing = 14
  • Panel1.Padding = 7
  • Panel1. Arrangement = 1
  • Panel1.Border = 1 ' Plain


Figure 20.7.2.1.1: Design (Panel1 at design time).


Figure 20.7.2.1.2: Horizontal (Panel1. Arrangement = 1)


Figure 20.7.2.1.3: Row (Panel1. Arrangement = 3 (Row))

If you reduce another container with the three buttons instead of a panel - a window is very suitable - then you can easily see the effect of creating a new column. With the assignment Panel1. Arrangement = Arrange.Row you get the arrangement in Figure 20.7.1.3.

Example 2

An HSplit component is placed on the form FMain. With the instruction

FMain.Arrangement = Arrange.Fill

the HSplit component completely fills the space in the container form.

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k20/k20.7/start.txt · Last modified: 22.10.2023 by emma

Page Tools