Recent

Author Topic: Training program  (Read 933 times)

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Training program
« on: June 28, 2022, 08:40:59 pm »
I'm training as a beginner and typed this Arabic small program, and I think it can be typed better than what I did, what do you think I have missed in this code?:

https://github.com/pascal111-fra/Lazarus-programs/blob/main/example1/unit1.pas

Complete project is in:
https://github.com/pascal111-fra/Lazarus-programs/tree/main/example1
La chose par la chose est rappelé.

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Training program
« Reply #1 on: June 29, 2022, 01:04:00 pm »
Ok! no answer, I think I need to specify a particular point. I saw before a way to gather some similar procedures in one procedure, it was using a sort of arrays or something like that, so how we can reduce the next procedures in just one procedure:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.CheckBox1Change(Sender: TObject);
  2. begin
  3.  
  4.   if (checkbox1.Checked) then
  5.   inc(attrib_count)
  6.   else
  7.   dec(attrib_count);
  8.  
  9. end;
  10.  
  11.  
  12. procedure TForm1.CheckBox2Change(Sender: TObject);
  13. begin
  14.   if (checkbox2.Checked) then
  15.   inc(attrib_count)
  16.   else
  17.   dec(attrib_count);
  18. end;
  19.  
  20. procedure TForm1.CheckBox3Change(Sender: TObject);
  21. begin
  22.   if (checkbox3.Checked) then
  23.   inc(attrib_count)
  24.   else
  25.   dec(attrib_count);
  26. end;
  27.  
  28. procedure TForm1.CheckBox4Change(Sender: TObject);
  29. begin
  30.   if (checkbox4.Checked) then
  31.   inc(attrib_count)
  32.   else
  33.   dec(attrib_count);
  34. end;
  35.  
  36. procedure TForm1.CheckBox5Change(Sender: TObject);
  37. begin
  38.   if (checkbox5.Checked) then
  39.   inc(attrib_count)
  40.   else
  41.   dec(attrib_count);
  42. end;
  43.  
La chose par la chose est rappelé.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11446
  • FPC developer.
Re: Training program
« Reply #2 on: June 29, 2022, 01:14:34 pm »
Let any such checkbox point to code like below:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.CheckBox1Change(Sender: TObject);
  2. begin
  3.   if sender is TCheckbox then
  4.    begin
  5.      if TCheckbox(sender).Checked then
  6.        inc(attrib_count)
  7.     else
  8.       dec(attrib_count);
  9.    end;
  10. end;

Let anyprocedure TForm1.CheckBox1Change(Sender: TObject);

Seenkao

  • Hero Member
  • *****
  • Posts: 549
    • New ZenGL.
Re: Training program
« Reply #3 on: June 29, 2022, 01:53:17 pm »
Let any such checkbox point to code like below:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.CheckBox1Change(Sender: TObject);
  2. begin
  3.   if sender is TCheckbox then
  4.    begin
  5.      if TCheckbox(sender).Checked then
  6.        inc(attrib_count)
  7.     else
  8.       dec(attrib_count);
  9.    end;
  10. end;

Let anyprocedure TForm1.CheckBox1Change(Sender: TObject);
И именно эту процедуру задайте каждому компоненту CheckBox. Не дублируйте!
Events -> OnChange -> выбрать нужную процедуру.

Eng:And it is this procedure that you set for each CheckBox component. Don't duplicate!
Events -> OnChange -> select the desired procedure.
Rus: Стремлюсь к созданию минимальных и достаточно быстрых приложений.

Eng: I strive to create applications that are minimal and reasonably fast.
Working on ZenGL

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Training program
« Reply #4 on: June 29, 2022, 01:57:30 pm »
Great! guys. It works fine, but what if I would like to change this procedure name "TForm1.CheckBox1Change" to more general name. You know "TForm1.CheckBox1Change" name is like if it's for "CheckBox1Change" only?
La chose par la chose est rappelé.

wildfire

  • Full Member
  • ***
  • Posts: 109
Re: Training program
« Reply #5 on: June 29, 2022, 02:10:51 pm »
You can call the procedure anything you want eg tForm1.ACheckBoxChanged(Sender: TObject), just ensure all the relevant events point to it.
« Last Edit: June 29, 2022, 02:32:01 pm by wildfire »
A halo is a mere circle, when does it end?

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Training program
« Reply #6 on: June 29, 2022, 02:26:22 pm »
You can call the procedure anything you want eg ACheckBoxChanged, just ensure all the relevant events point to it.
... and that it has the correct signature
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

wildfire

  • Full Member
  • ***
  • Posts: 109
Re: Training program
« Reply #7 on: June 29, 2022, 02:30:51 pm »
Yeah, I should have noticed this was the beginners section.

@OP I've edited my post.
A halo is a mere circle, when does it end?

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Training program
« Reply #8 on: June 29, 2022, 03:52:36 pm »
I changed the code to be more acceptable in a formal academic Lazarus forum:

https://github.com/pascal111-fra/Lazarus-programs/blob/main/example1/unit1.pas
La chose par la chose est rappelé.

 

TinyPortal © 2005-2018