Resize dialog based on text length in input

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
Grovkillen
Automation Wizard
Posts: 998
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Resize dialog based on text length in input

Post by Grovkillen » Mon Sep 20, 2021 5:19 am

Just a quick example on how you could have the window automatically resize based on the contents of an Edit element.

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  Left = 725
  Top = 225
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'CustomDialog'
  ClientHeight = 49
  ClientWidth = 120
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 96
  TextHeight = 13
  object Edit1: TEdit
    Left = 4
    Top = 5
    Width = 109
    Height = 37
    Alignment = taCenter
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -24
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    ParentFont = False
    TabOrder = 8
    Text = ''
  end
end
EndDialog>Dialog1

OnEvent>KEY_DOWN,VK27,0,EXIT_APP
AddDialogHandler>Dialog1,Edit1,OnKeyUp,CHECK_INPUT

Show>Dialog1

Label>mainLoopStart
Wait>0.05
Goto>mainLoopStart

SRT>EXIT_APP
  Exit>
END>EXIT_APP

SRT>CHECK_INPUT
  GetDialogProperty>Dialog1,Edit1,Text,EDIT1_text
  Length>EDIT1_text,EDIT1_length
  //8 is the number of characters per "step" of resize
  Let>WINDOW_SIZE={%EDIT1_length% div 8}
  GetDialogProperty>Dialog1,,Width,DIALOG1_width
  GetDialogProperty>Dialog1,Edit1,Width,EDIT1_width
  Let>SET_WIDTH_dialog1={Round(120 * (1 + %WINDOW_SIZE% * 1.0400))}
  Let>SET_WIDTH_edit1={Round(108 * (1 + %WINDOW_SIZE% * 1.1000))}
  SetDialogProperty>Dialog1,,Width,SET_WIDTH_dialog1
  SetDialogProperty>Dialog1,Edit1,Width,SET_WIDTH_edit1
END>CHECK_INPUT
Let>ME=%Script%

Running: 15.0.24
version history

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts