Topic: Combine multiple controls that have the same value

Hello everyone and thanks for reading my request: what should be written in case two controls have the same instruction without having to rewrite the same instruction for both. Yes, I understand that the speech is a bit convoluted and it is not easy to understand.
Let's take an example: Edit1 and Edit2 must have the same text, I would write like this

form.Edit1.text and form.Edit2.text := Hello

instead of:

form.Edit1.text := Hello
form.Edit2.text := Hello

Going to a real case:

if frm.Dipendenti.cbImpiegato.check = 1 then frmDipendenti.lbDatalicenziamentodipendente.Visible and frmDipendenti.dtDatalicenziamentodipendente.visible = False

In this case MVD tells me it expects ';' before AND

Thanks and greetings

Re: Combine multiple controls that have the same value

No, it doesn't work that way.

form.Edit1.text and form.Edit2.text := Hello

instead of:

form.Edit1.text := Hello
form.Edit2.text := Hello

looks like a lot of typing
The difference in entering the code is 4 characters  )))
Although there is a life hack, copy and paste the first line again and change 1 to 2.


But seriously:
The text property for each component is readable and writeable, so what do we do after "text"?
Read (=) or write/set (:=) a value (string of text).
The same is true for other components and their properties.
Each has its own meaning.


But the program doesn't understand you and offers to at least end the line with the character ';'.


You can:

form.Edit1.text := Hello;
form.Edit2.text := Hello;

form.Edit1.text := Hello;
form.Edit2.text := form.Edit1.text;

Re: Combine multiple controls that have the same value

The And keyword is used to perform a logical or boolean 'and' of two logical values.
If both are true, then the result is true, otherwise, the result is false.

4 (edited by reteinformatica 2023-04-21 21:42:16)

Re: Combine multiple controls that have the same value

Thanks sparrow, your instructions were very clear and you also gave me an indication of something I've been thinking about since tonight: when to put := or just =
But would you be so kind as to explain it to me better?

Re: Combine multiple controls that have the same value

:= set value. For example
Form1.Edit1.Text := 'Hello';
Displays the text Hello in Edit1.


= is used for checks. For example
if Form1.Edit1.Text = 'OK' then showmessage('good');
if the Edit1 field contains the text 'OK', display a message that everything is fine.

Re: Combine multiple controls that have the same value

Very clear! Thanks sparrow.

Re: Combine multiple controls that have the same value

Hi Fabio, Hi Sparrow,

if frm.Dipendenti.cbImpiegato.check = 1 then frmDipendenti.lbDatalicenziamentodipendente.Visible and frmDipendenti.dtDatalicenziamentodipendente.visible = False

There may occasionally be an opportunity to reduce the amount of code in your script but it depends on the situation and so is not always an option.
Using your 'real life' example above, try placing your 'label object' and your 'date object' inside a panel;  a panel in this instance is being used as a container.  And because these objects are subordinate to the object in which they are contained, you only need to refer to the 'panel' in your script and not the individual objects within the panel.
It's far easier to explain with an example so look at the attachment - 4 labels, 3 edit boxes and 1 datetimepicker are all set to visible := true or visible := false with just one line of script.
The number of key-strokes is not really the issue - it's simply that you can maintain it within a single procedure and so there is less chance of making an error.
Derek.

Post's attachments

Attachment icon fabio panel.zip 335.48 kb, 76 downloads since 2023-04-22 

Re: Combine multiple controls that have the same value

Hi derek, very interesting this thing and solve my problem.
Before reading your answer I "made up" this thing:

if frmDipendenti.cbImpiegato.Checked = true
      then
        begin
        frmDipendenti.lbDatalicenziamentodipendente.Visible := false;
        frmDipendenti.dtDatalicenziamentodipendente.Visible := false;
        end
      else
        begin
        frmDipendenti.lbDatalicenziamentodipendente.Visible := true;
        frmDipendenti.dtDatalicenziamentodipendente.Visible := true;
        end


To work, it would seem to work but I'd like to know what you and sparrow think