Recent

Author Topic: Solved- Reading Form Properties  (Read 1555 times)

Wilko500

  • Full Member
  • ***
  • Posts: 180
Solved- Reading Form Properties
« on: December 28, 2020, 11:58:20 pm »
I have a question about reading form properties at runtime for enabling dynamic form positioning.  Coming from a VB6 environment I [incorrectly] assumed that form properties such as Borderstyle's bsSizeable were actually constants so I could do
Code: Pascal  [Select][+][-]
  1. iStyle:=Form1.BorderStyle;
  2. //Do something with iStyle
  3.  

I now realise that they are not constants but members of TFormBorderStyle and cannot be accessed like that.
I managed to read BorderStyle as follows but is there an easier way?
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button8Click(Sender: TObject);
  2. var
  3.     iStyle:        TFormBorderStyle;
  4. Begin
  5. iStyle:=Form1.BorderStyle;
  6. Case iStyle Of
  7.   bsNone: ShowMessage('1');
  8.   bsSingle: ShowMessage('2');
  9.   bsSizeable: ShowMessage('3')
  10. End;
  11. end;
  12.  

Thanks Richard

Lazarus 2.0.10 FPC 3.2.0 Win7Pro 32bit
« Last Edit: December 29, 2020, 11:21:48 pm by Wilko500 »
MacBook Pro mid 2015 with OS Monterey 12.7.6
FPC 3.2.3 Lazarus 3.7
FPC 3.2.2 Lazarus 3.4

speter

  • Sr. Member
  • ****
  • Posts: 494
Re: Reading Form Properties
« Reply #1 on: December 29, 2020, 12:13:21 am »
There is no need to get a copy of the borderstyle...
Code: Pascal  [Select][+][-]
  1.   Case BorderStyle Of
  2.     bsNone: s := 'bsNone';
  3.     bsDIalog: s := 'bsDIalog';
  4.     bsSingle: s := 'bsSingle';
  5.     bsSizeable: s := 'bsSizeable';
  6.     bsSizeToolwin: s := 'bsSizeToolwin';
  7.     bsToolwindow: s := 'bsToolwindow';
  8.     else s := 'something else';
  9.   End;
  10.   memo1.append(s);
  11.  
The above, works fine (if you have a memo, of course). :)

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

wp

  • Hero Member
  • *****
  • Posts: 13433
Re: Reading Form Properties
« Reply #2 on: December 29, 2020, 12:17:31 am »
And don't use the name of the form variable (Form1) inside the code of the class TForm1, otherwise your code will work only when the instance of TForm1 is named "Form1", just drop the "Form1".

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. Begin
  3.   case BorderStyle of    // no "Form1." here!
  4.     bsNone: ShowMessage('1');
  5.     bsSingle: ShowMessage('2');
  6.     bsSizeable: ShowMessage('3')
  7.   End;
  8. end;

speter

  • Sr. Member
  • ****
  • Posts: 494
Re: Reading Form Properties
« Reply #3 on: December 29, 2020, 12:24:12 am »
You can also create a constant...
Code: Pascal  [Select][+][-]
  1. const
  2.   style_str : array [bsNone..bsSizeToolwin] of string =
  3.               ('bsNone','bsSingle','bsSizeable','bsDIalog','bsToolwindow','bsSizeToolwin');
  4. begin
  5.   memo1.append(style_str[borderstyle]);
  6. end;

cheers
S. :)
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Reading Form Properties
« Reply #4 on: December 29, 2020, 12:35:20 am »
Hi!

The simple way:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var  s : String;
  3. begin
  4.  writeStr(s,self.BorderStyle);
  5.  showMessage (s);
  6. end;
  7.  

Winni
« Last Edit: December 29, 2020, 12:36:57 am by winni »

jamie

  • Hero Member
  • *****
  • Posts: 7610
Re: Reading Form Properties
« Reply #5 on: December 29, 2020, 02:07:48 am »
if you want a numerical value I believe you can cast the results with a ORD

Index := ORD(MyForm.BorderStyle);

or something like that.

I am sure that is a ENUM type so it should return the position within the ENUM type..
if there were a SET then maybe different story  :D
The only true wisdom is knowing you know nothing

egsuh

  • Hero Member
  • *****
  • Posts: 1761
Re: Reading Form Properties
« Reply #6 on: December 29, 2020, 03:51:10 am »
I believe winni won one point.  :D

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: Reading Form Properties
« Reply #7 on: December 29, 2020, 03:57:22 am »
if there were a SET then maybe different story  :D

A set can be "moved" to an array of byte of size SizeOf(TheSet) or, depending on said SizeOf(), any integer long enough to contain it, though AFAIK there is no single function to do this in the RTL, like Ord() does for ordinal types.

I believe winni won one point.  :D

Only one? :o
He won the race, IMHO :D
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Wilko500

  • Full Member
  • ***
  • Posts: 180
Re: Solved- Reading Form Properties
« Reply #8 on: December 29, 2020, 11:38:31 pm »
Once again, thanks, great feedback. I wanted a value so I could hook into existing routines that make dynamic form position adjustments.  In Windows fitting forms into available screen area and in alignment to other forms depends on much including border style. So far I have managed to cope with task bar at left, top, right, large/small icons, different screen resolutions all without use of adjusting constants.  There are bound to be combinations that I have not tested but code works for me for now.  One day I'll be wanting to run this code on my Mac!!!

Since I am new to Lazarus/FP and porting an existing VB6 program I'm looking for simple ways in the first instance. I'v still got plenty of complicated stuff to work through!!

I think winni gets the prize this time round.


MacBook Pro mid 2015 with OS Monterey 12.7.6
FPC 3.2.3 Lazarus 3.7
FPC 3.2.2 Lazarus 3.4

 

TinyPortal © 2005-2018