Mike Torrettinni
Mike Torrettinni

Reputation: 1824

How to align controls left and top without fixed width?

I have 6 RichEdit controls in ScrollBox. They need to be aligned left in sequence from top to bottom, but I need width to be not fixed, so I can use scroll bar to scroll to see full text, when text length goes beyond ScrollBox width.

If I set:

RichEdit1.Align := alTop;
RichEdit2.Align := alTop;
RichEdit3.Align := alTop;
RichEdit4.Align := alTop;
RichEdit5.Align := alTop;
RichEdit6.Align := alTop;

they align perfectly and stay fixed in design so can't move them accidentally, but the width gets fixed. Not good.

If I align them manually, then they are not fixed when clicking on them and can be moved and I need to re-arrange all the time. Annoying.

The picture show on top all RichEdits with Align = alTop and width is fixed to ScrollBox width. Bottom example is with manual alignment of all RichEdits that width can go beyond ScrollBox's width, but they can be moved around in design:

enter image description here

So, I would like to get them fixed to left, top as does alTop, but not to fix the width. How can I achieve this?

Upvotes: 3

Views: 1658

Answers (2)

Jerry Dodge
Jerry Dodge

Reputation: 27276

If preventing accidental moving is your goal, I see two solutions.

Use Delphi's "Lock Controls" option

Edit > Lock Controls, but this is a temporary solution which is not saved, and upon closing / re-opening, it will be disabled again.

Align them all inside of a panel

And then make that panel to the width you need. However this still wouldn't prevent you from accidentally moving the panel - just the edit controls. And even then, you're still able to re-arrange aligned controls - if you were to accidentally drag one beyond the edge of its neighbor.

Here's a sample DFM structure:

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 340
  ClientWidth = 392
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object ScrollBox1: TScrollBox
    Left = 24
    Top = 8
    Width = 329
    Height = 281
    TabOrder = 0
    object Panel1: TPanel
      Left = 3
      Top = 3
      Width = 500
      Height = 217
      TabOrder = 0
      object Edit1: TEdit
        Left = 1
        Top = 1
        Width = 498
        Height = 21
        Align = alTop
        TabOrder = 0
        Text = 'Edit1'
        ExplicitLeft = 40
        ExplicitTop = 48
        ExplicitWidth = 121
      end
      object Edit2: TEdit
        Left = 1
        Top = 22
        Width = 498
        Height = 21
        Align = alTop
        TabOrder = 1
        Text = 'Edit2'
        ExplicitLeft = 16
        ExplicitTop = 56
        ExplicitWidth = 121
      end
      object Edit3: TEdit
        Left = 1
        Top = 43
        Width = 498
        Height = 21
        Align = alTop
        TabOrder = 2
        Text = 'Edit3'
        ExplicitLeft = 40
        ExplicitTop = 96
        ExplicitWidth = 121
      end
      object Edit4: TEdit
        Left = 1
        Top = 64
        Width = 498
        Height = 21
        Align = alTop
        TabOrder = 3
        Text = 'Edit4'
        ExplicitLeft = 32
        ExplicitTop = 128
        ExplicitWidth = 121
      end
      object Edit5: TEdit
        Left = 1
        Top = 85
        Width = 498
        Height = 21
        Align = alTop
        TabOrder = 4
        Text = 'Edit5'
        ExplicitLeft = 56
        ExplicitTop = 160
        ExplicitWidth = 121
      end
      object Edit6: TEdit
        Left = 1
        Top = 106
        Width = 498
        Height = 21
        Align = alTop
        TabOrder = 5
        Text = 'Edit6'
        ExplicitLeft = 80
        ExplicitTop = 192
        ExplicitWidth = 121
      end
    end
  end
end

On another note, this has inspired me to investigate how I can implement a LockChildren Boolean property on the TControl level which, when enabled, prevents you from moving or sizing its child controls.


Actually, I just found an apparent bug in Delphi with the "Lock Controls" option. If you enable it on a form, then close and re-open the form, the controls are no longer locked. But at the same time, if you go to the "Edit" menu, the "Lock Controls" option appears as if it's still enabled (even though it isn't). I think I recall this bug waaaaaay back in Delphi 7, but I see it still on Delphi 10 Seattle.

EDIT I found the QC report still open:

http://qc.embarcadero.com/wc/qcmain.aspx?d=82764

Upvotes: 3

Tom Brunberg
Tom Brunberg

Reputation: 21033

At design time use Align = alTop. Then, at runtime (e,g, OnCreate of form) set Align := alNone, and change the width as you like.

Upvotes: 6

Related Questions