Recent

Author Topic: SOLVED, BUT..: need to use control name in instruction  (Read 808 times)

xaver13

  • Full Member
  • ***
  • Posts: 102
SOLVED, BUT..: need to use control name in instruction
« on: October 23, 2019, 02:32:49 pm »
in Tabsheet2 I have a Tedit named kolik (and many others)
I would like to set color of such controls using cycle - something as follows.
I need an advice how to write it.
My unsuccessfull attempts follow.
I have name of the control (kolik) in variable a but I do not know how to set color of such control.

Thank you very much for help.

Code: Pascal  [Select]
  1.      for i := 0 to TabSheet2.ControlCount -1 do
  2.      begin
  3.        if TabSheet2.Controls[i] is TEdit then begin
  4.        //TabSheet2.Controls[i].Color := StringToColor('clBlack');
  5.        //TabSheet2.Controls[i].Font.Color := StringToColor('clSilver');
  6.        a:=(TabSheet2.Controls[i].Name);
  7.        //@a.Color:= StringToColor('clBlack');
  8.  
  9.  
  10.        kolik.Color := StringToColor('clBlack');
  11.        kolik.Font.Color := StringToColor('clSilver');
  12.  
  13.        end;
  14.      end;  
« Last Edit: October 26, 2019, 04:37:21 pm by xaver13 »
--
Jiri Cvrk

af0815

  • Sr. Member
  • ****
  • Posts: 360
Re: need to use control name in instruction
« Reply #1 on: October 23, 2019, 02:39:40 pm »
Code: Pascal  [Select]
  1. for i := 0 to TabSheet2.ControlCount -1 do
  2.      begin
  3.        if TabSheet2.Controls[i] is TEdit then begin
  4.         TEdit( TabSheet2.Controls[i]).Color := StringToColor('clBlack');
  5.         TEdit( TabSheet2.Controls[i]).Font.Color := StringToColor('clSilver');
  6.        end;
  7.      end;
  8.  
Only say the compiler the right type of the Element
« Last Edit: October 23, 2019, 02:42:55 pm by af0815 »
regards
Andreas

xaver13

  • Full Member
  • ***
  • Posts: 102
Re: need to use control name in instruction
« Reply #2 on: October 23, 2019, 02:45:53 pm »
thank you very much it's workig :)
--
Jiri Cvrk

lucamar

  • Hero Member
  • *****
  • Posts: 2084
Re: need to use control name in instruction
« Reply #3 on: October 23, 2019, 02:49:53 pm »
Or, if you want to do it by name:

Code: Pascal  [Select]
  1. for i := 0 to TabSheet2.ControlCount -1 do
  2. begin
  3.   if TabSheet2.Controls[i].Name = 'kolik' then begin
  4.     { Why bother with StringToColor(constant_name)? }
  5.     TEdit(TabSheet2.Controls[i]).Color := clBlack;
  6.     TEdit(TabSheet2.Controls[i]).Font.Color := clSilver;
  7.   end;
  8. end;

Extrapolate for other names, of course.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

xaver13

  • Full Member
  • ***
  • Posts: 102
Re: SOLVED:need to use control name in instruction
« Reply #4 on: October 23, 2019, 03:36:24 pm »
Thank you.
I changed everything but buttons :(

https://forum.lazarus.freepascal.org/index.php?topic=36774.0

I tried SpeedButtons and ColorButtons too.
« Last Edit: October 23, 2019, 05:04:08 pm by xaver13 »
--
Jiri Cvrk

lucamar

  • Hero Member
  • *****
  • Posts: 2084
Re: SOLVED:need to use control name in instruction
« Reply #5 on: October 23, 2019, 07:21:54 pm »
You can do it for any descendant of TControl since most of them inherit their Name, Color and Font from the base class:

Code: Pascal  [Select]
  1. for i := 0 to TabSheet2.ControlCount -1 do
  2. begin
  3.   if TabSheet2.Controls[i].Name = 'kolik' then begin
  4.     TabSheet2.Controls[i].Color := clBlack;
  5.     TabSheet2.Controls[i].Font.Color := clSilver;
  6.   end;
  7. end;

In that case you don't need to (and, indeed, shouldn't) hard-cast to the wanted class but use directly the TControl object returned by Controls "array" element getter.

That should cope with almost any control whether a descendant of TWinControl, like TEdit, or of TGraphicControl, like TSpeedButton and TColorButton.

HTH
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

xaver13

  • Full Member
  • ***
  • Posts: 102
Re: SOLVED:need to use control name in instruction
« Reply #6 on: October 23, 2019, 08:03:18 pm »
good, thank you! Now I am using
Code: Pascal  [Select]
  1.      for i := 0 to TabSheet2.ControlCount -1 do
  2.       if TabSheet2.Controls[i] is Tedit or TabSheet2.Controls[i] is TDBEdit or TabSheet2.Controls[i] is TLabel then begin
  3.         TabSheet2.Controls[i].Color := clBlack;
  4.         TabSheet2.Controls[i].Font.Color := clSilver;
  5.       end;
  6.  

But maybe should be nicer something as:  ;)
Code: Pascal  [Select]
  1. const
  2.     blackControls = [TEdit,TDBEdit,TLabel];
  3. begin
  4.      for i := 0 to TabSheet2.ControlCount -1 do
  5.       begin
  6.       if TabSheet2.Controls[i] in blackControls do begin
  7.         TabSheet2.Controls[i].Color := clBlack;
  8.         TabSheet2.Controls[i].Font.Color := clSilver;
  9.       end;
  10. end;
  11.  

btw. this is not working yet  (const..Ordinal Expression expected, in..Operator is not overloaded), please, is it possible to use in there and how? I tried some overloading of 'in' but nothing was correct.
« Last Edit: October 24, 2019, 01:32:21 pm by xaver13 »
--
Jiri Cvrk

howardpc

  • Hero Member
  • *****
  • Posts: 3182
Re: SOLVED:need to use control name in instruction
« Reply #7 on: October 23, 2019, 09:12:56 pm »
You can try some adaptation of this:
Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Forms, Controls, Graphics, ComCtrls, StdCtrls, Classes;
  9.  
  10. type
  11.  
  12.   TForm1 = class(TForm)
  13.     Button1: TButton;
  14.     Edit1: TEdit;
  15.     Label1: TLabel;
  16.     PageControl1: TPageControl;
  17.     TabSheet1: TTabSheet;
  18.     TabSheet2: TTabSheet;
  19.     TestButton: TButton;
  20.     procedure TestButtonClick(Sender: TObject);
  21.   end;
  22.  
  23.   procedure SetControlColorsInTabsheet(aTabSheet: TTabSheet;
  24.             aColor, aFontColor: TColor; aControlClasses: array of const);
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. procedure SetControlColorsInTabsheet(aTabSheet: TTabSheet; aColor,
  32.              aFontColor: TColor; aControlClasses: array of const);
  33.  
  34.   function DesiredControlClass(aControlClass: TControlClass): Boolean;
  35.   var
  36.     vr: TVarRec;
  37.   begin
  38.     for vr in aControlClasses do
  39.       if (vr.VType = vtClass) and (vr.VClass = aControlClass) then
  40.         Exit(True);
  41.     Result := False;
  42.   end;
  43.  
  44. var
  45.   i: Integer;
  46. begin
  47.   with aTabSheet do
  48.     for i := 0 to ControlCount-1 do
  49.       if DesiredControlClass(TControlClass(Controls[i].ClassType)) then
  50.         begin
  51.           Controls[i].Color := aColor;
  52.           Controls[i].Font.Color := aFontColor;
  53.         end;
  54. end;
  55.  
  56. {$R *.lfm}
  57.  
  58. { TForm1 }
  59.  
  60. procedure TForm1.TestButtonClick(Sender: TObject);
  61. begin
  62.   SetControlColorsInTabsheet(TabSheet2, clBlue, clSilver, [TEdit, TButton, TLabel]);
  63. end;
  64.  
  65. end.

ASerge

  • Hero Member
  • *****
  • Posts: 1412
Re: SOLVED:need to use control name in instruction
« Reply #8 on: October 24, 2019, 10:56:15 pm »
Code: Pascal  [Select]
  1. var
  2.   C: TControl;
  3. begin
  4.   for C in TabSheet2.GetEnumeratorControls do
  5.     if C.Name = VariableWithKolikName then
  6.     begin
  7.       C.Color := clBlack;
  8.       C.Font.Color := clSilver;
  9.     end;
  10. end;
or
Code: Pascal  [Select]
  1. var
  2.   C: TControl;
  3. begin
  4.   for C in TabSheet2.GetEnumeratorControls do
  5.     case C.Name of
  6.       'kolik':
  7.         begin
  8.           C.Color := clBlack;
  9.           C.Font.Color := clSilver;
  10.         end;
  11.       'OtherName':
  12.         begin
  13.           //...
  14.         end;
  15.     end;
  16. end;

xaver13

  • Full Member
  • ***
  • Posts: 102
Re: SOLVED, BUT..: need to use control name in instruction
« Reply #9 on: October 26, 2019, 09:36:16 am »
Thank you very much ASerge
my old version:
Code: Pascal  [Select]
  1.   for i := 0 to GroupBox1.ControlCount - 1 do
  2.   begin
  3.     if GroupBox1.Controls[i] is Tedit or GroupBox1.Controls[i] is
  4.       TDBEdit or GroupBox1.Controls[i] is TLabel
  5.       or GroupBox1.Controls[i] is TComboBox
  6.       then
  7.       begin
  8.       GroupBox1.Controls[i].Color := myDark;
  9.       GroupBox1.Controls[i].Font.Color := clSilver;
  10.     end;
  11.   end;  
new version by you
Code: Pascal  [Select]
  1.  
  2. var
  3.   C: TControl;
  4. ..
  5. for C in GroupBox1.GetEnumeratorControls do
  6.       begin
  7.         C.Color := myDark;
  8.         C.Font.Color := clSilver;
  9.       end;
« Last Edit: October 26, 2019, 09:48:35 am by xaver13 »
--
Jiri Cvrk