Recent

Author Topic: Which Control should I use ?  (Read 2020 times)

J-G

  • Hero Member
  • *****
  • Posts: 1114
Which Control should I use ?
« on: June 12, 2026, 05:51:10 pm »
I've been going around in circles today trying to fathom which contol I should use to provide a selection of one of four different options.

These  are concerned with Font Style - ie. Normal, Bold, Italic or Bold-Italic  -  You'l no doubt observe that they are exclusive and to that end I first attempted to use four separate TCheckBoxes  This got me into a right mess when I added Event code.

My next trial was a TCheckGroup set up as 4 columns since the ideal would be a single row rather than 4, but this gave me grief over the fact that I couldn't determine which Index had been selected  -  LCL Help seemed to indicate that there was an 'Index' but the compiler disagrees.

Here is a snippet of code which fails to compile - and I'm sure is simply not the way to approach this problem but may give someone an insight into what I'd like to achieve.

Code: Pascal  [Select][+][-]
  1. procedure TTriangleSolution.Font_StyleClick(Sender: TObject);
  2. var
  3.   I : byte;
  4. begin
  5.   If Font_Style.Checked[0] := true then I := 0
  6.   else
  7.     If Font_Style.Checked[1] := true then I := 1
  8.     else
  9.       If Font_Style.Checked[2] := true then I := 2
  10.       else
  11.         If Font_Style.Checked[3] := true then I := 3;
  12.  
  13.   case I of
  14.     0 : begin
  15.         end;
  16.     1 : begin
  17.           FontNames.Font.Style:=fsBold;
  18.         end;
  19.     2 : begin
  20.           FontNames.Font.Style:=fsItalic;
  21.         end;
  22.     3 : begin
  23.           FontNames.Font.Style.fsBold:=True;
  24.           FontNames.Font.Style.fsItalic:=True;
  25.         end;
  26.   end;
  27. end;

FontNames is a TComboBox which has been populated with a list of potential Font Names. Once a selection has been made, I wish to display the name in the appropriate style.

The compiler throws an error on Line 17 but that's the least of my problems at the minute ::)

Given that there surely ought to be a simple means by which I can select one of four options I'd appreciate the advice from the wealth of wisdom there is on this forum  :D
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

bytebites

  • Hero Member
  • *****
  • Posts: 794
Re: Which Control should I use ?
« Reply #1 on: June 12, 2026, 06:02:50 pm »
Code: Pascal  [Select][+][-]
  1.  If Font_Style.Checked[0] then

I would prefer two checkboxes for bold and italic, so both, either or neither checked.

J-G

  • Hero Member
  • *****
  • Posts: 1114
Re: Which Control should I use ?
« Reply #2 on: June 12, 2026, 06:15:32 pm »
Code: Pascal  [Select][+][-]
  1.  If Font_Style.Checked[0] then

I would prefer two checkboxes for bold and italic, so both, either or neither checked.

That has crossed my mind  :)  particularly since there isn't an fsNormal style as a Font attribute, but what 'Control' would be the ideal?


FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Xenno

  • Full Member
  • ***
  • Posts: 132
    • BS Programs
Re: Which Control should I use ?
« Reply #3 on: June 12, 2026, 06:48:32 pm »
You may want to use TCheckListBox and make use of its OnClickCheck event.
Lazarus 4.6, Windows 10, https://www.youtube.com/@bsprograms
If I want to share, I give. If I want money, I sell. I don't set traps.

dsiders

  • Hero Member
  • *****
  • Posts: 1657
Re: Which Control should I use ?
« Reply #4 on: June 12, 2026, 06:59:22 pm »
The fundamental issue is that TFont.Style is a set type and not a single value.

wp

  • Hero Member
  • *****
  • Posts: 13630
Re: Which Control should I use ?
« Reply #5 on: June 12, 2026, 06:59:37 pm »
Find a few examples in the attached demo (single checkboxes, TCheckGroup, TRadioGroup, TCombobox, TToolbar with TToolButtons), and there are even more which work similarly: TMenu or TPopupMenu with TMenuItems, TCheckListBox, TCheckComboBox, two SpeedButtons on a panel. There is no "best" one - select what suites your needs.

[EDIT]
Attachment removed because it cannot be loaded with released Lazarus versions. Please use the attachment a few posts later.
« Last Edit: June 12, 2026, 08:12:37 pm by wp »

J-G

  • Hero Member
  • *****
  • Posts: 1114
Re: Which Control should I use ?
« Reply #6 on: June 12, 2026, 07:43:26 pm »
Find a few examples in the attached demo (single checkboxes, TCheckGroup, TRadioGroup, TCombobox, TToolbar with TToolButtons), and there are even more which work similarly: TMenu or TPopupMenu with TMenuItems, TCheckListBox, TCheckComboBox, two SpeedButtons on a panel. There is no "best" one - select what suites your needs.

Thanks for that input @WP  -  I have downloaded the [Demo] and can see that there is some data in the .PAS file but when I try to [Open] the .LPI I just get an empty Source Editor  and the [View] Menu item has Toggle Form/Unit View greyed out  ?????

Reading the .PAS file does however give me a clue as to why the line
' FontNames.Font.Style:=fsBold; ' fails !!


« Last Edit: June 12, 2026, 07:47:49 pm by J-G »
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12560
  • Debugger - SynEdit - and more
    • wiki
Re: Which Control should I use ?
« Reply #7 on: June 12, 2026, 07:54:23 pm »
Code: Pascal  [Select][+][-]
  1. procedure TTriangleSolution.Font_StyleClick(Sender: TObject);
  2. var
  3.   I : byte;
  4. begin
  5.   If Font_Style.Checked[0] := true then I := 0
  6.   else
  7.     If Font_Style.Checked[1] := true then I := 1
  8.     else
  9.       If Font_Style.Checked[2] := true then I := 2
  10.       else
  11.         If Font_Style.Checked[3] := true then I := 3;
  12.  

Whatever control you use, the code above can be done better. You don't need a byte or numeric value. Just use TFontstyles (the set type)
Code: Pascal  [Select][+][-]
  1. var NewFontStyle: TFontStyles;
  2. begin
  3.   NewFontStyle := []; // empty / no style
  4.   if some_condition_for_bold then NewFontStyle := NewFontStyle + [fsBold];  // or Include(NewFontStyle, fsBold);
  5.   if some_condition_for_italic then NewFontStyle := NewFontStyle + [fsItalic];  // or Include(NewFontStyle, fsItalic);
  6. ....
  7.   Font.Style := NewFontStyle;
  8. end;

wp

  • Hero Member
  • *****
  • Posts: 13630
Re: Which Control should I use ?
« Reply #8 on: June 12, 2026, 08:11:31 pm »
I have downloaded the [Demo] and can see that there is some data in the .PAS file but when I try to [Open] the .LPI I just get an empty Source Editor  and the [View] Menu item has Toggle Form/Unit View greyed out  ?????
Sorry, I am working with Laz/trunk, there was a incompatible file change recently, and I forgot to set the compatibility flag - try again with the attachment here.

Reading the .PAS file does however give me a clue as to why the line
' FontNames.Font.Style:=fsBold; ' fails !!
Font.Style is a set, and therefore the assigned elements must be in square brackets:
Code: Pascal  [Select][+][-]
  1.  FontNames.Font.Style:=[fsBold];

J-G

  • Hero Member
  • *****
  • Posts: 1114
Re: Which Control should I use ?
« Reply #9 on: June 12, 2026, 08:36:07 pm »
Sorry, I am working with Laz/trunk, there was a incompatible file change recently, and I forgot to set the compatibility flag - try again with the attachment here.

Reading the .PAS file does however give me a clue as to why the line
' FontNames.Font.Style:=fsBold; ' fails !!
Font.Style is a set, and therefore the assigned elements must be in square brackets:
Code: Pascal  [Select][+][-]
  1.  FontNames.Font.Style:=[fsBold];

That sorted the problem - and I can see all the various ways that my goal could be accomplished  :D

As I said, Reading the .PAS file and seeing the Square Brackets was the key. I knew that it ought to be a simple matter and I had done something very similar previously - and of course used '[ ]' -  I even have a proc already written within this code which I'd copied from an old project - so therefore worked  :-[  -  I just hadn't put 2 & 2 together  %)

Having said that, I have been coding since about 8am so the brain could well be addled. It may well be time for a break !!
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

J-G

  • Hero Member
  • *****
  • Posts: 1114
Re: Which Control should I use ?
« Reply #10 on: June 13, 2026, 03:00:32 pm »
In the light of the advice from @WP & @Martin_fr (many thanks both)  I have now re-evaluated a number of issues and changed the way I deal with the Font Style. It makes much more sence to use the TFontStyles Set - (now that I understand how to set and re-set such !!) - however I do now have a small 'cosmetic' issue inasmuch as because I've just set the Font attributes prior to showing the Panel, the font name in the TComboBox is always highlighted as seen on the attached ScreenGrab.

I did try to circumvent this by using the SetFocus to the Panel after setting the font and after making the Panel visible  but that had no effect whatsoever.

Is there some other method by which I can present a 'clean' panel display ?   ie. NO highlighting.

EDIT - corrected the 'not' to 'now'  !!!
« Last Edit: June 13, 2026, 06:57:29 pm by J-G »
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

bytebites

  • Hero Member
  • *****
  • Posts: 794
Re: Which Control should I use ?
« Reply #11 on: June 13, 2026, 03:09:46 pm »
AutoSelect := false ?

cdbc

  • Hero Member
  • *****
  • Posts: 2870
    • http://www.cdbc.dk
Re: Which Control should I use ?
« Reply #12 on: June 13, 2026, 03:10:54 pm »
Hi
Choose your TEdit in the Object-Inspector and look for a property "AutoSelection" or "AutomaticHighlight"... Anyway it will be set to TRUE by default -- Turn that off  ;D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

J-G

  • Hero Member
  • *****
  • Posts: 1114
Re: Which Control should I use ?
« Reply #13 on: June 13, 2026, 03:33:56 pm »
Both suggestions (@cdbc & @bytebites) look promising but regrettably neither are effective (essentially they are the same though - just explained differently).

You are correct in stating that AutoSelect is true by default but removing the 'tick' didn't have any effect on the behaviour.

I have also tried FontNames.focused := false but the compiler reports that 'Argument cannot be assigned'  which seems rather odd when the feature can be selected  %)

The component is a TComboBox rather than a TEdit - but both do have AutoSelect
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

cdbc

  • Hero Member
  • *****
  • Posts: 2870
    • http://www.cdbc.dk
Re: Which Control should I use ?
« Reply #14 on: June 13, 2026, 07:51:57 pm »
Hi
Ok, after setting 'AutoSelect' to false, you could try to focus another widget, say a button, maybe... %)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

 

TinyPortal © 2005-2018