Recent

Author Topic: Creating component from scratch  (Read 13322 times)

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Creating component from scratch
« Reply #15 on: May 21, 2021, 10:56:05 pm »
Here another test. Add a third button to destroy the Panel by calling Panel1.Free

Press the first button to create a SimpleComponent
Press the second button to switch its parent to Panel1
Press the third button to destroy the Panel

What do you think will happen if you press the second button, now?


لقد إختفى كلّ شيء! الـ panel والمُستطيل الأصفر.

google translate:

"Everything has disappeared! The panel and the yellow rectangle."

(https://i.postimg.cc/sQCP2HQQ/Screenshot-at-2021-05-21-22-53-09.png)
La chose par la chose est rappelé.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Creating component from scratch
« Reply #16 on: May 21, 2021, 10:58:41 pm »
Did you press the second button  after they disappeared? You called it "Switch"

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Creating component from scratch
« Reply #17 on: May 21, 2021, 11:07:49 pm »
Did you press the second button  after they disappeared? You called it "Switch"

حسناً! لقد فعلتُ ما قُلتَ وظهر المُستطيل الأصفر مرّةً أُخرى.

google translate:

"Okay! I did what you said and the yellow rectangle appeared again."

(https://i.postimg.cc/R6x8zRvG/Screenshot-at-2021-05-21-23-06-07.png)
La chose par la chose est rappelé.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Creating component from scratch
« Reply #18 on: May 21, 2021, 11:39:08 pm »
As you saw, the component was still there but not visible. It needs a valid parent.

Now change the owner in the first button from self to Panel1

Press the first button to create the control.
Press the third button to destroy the owner "Panel1"

What happens to the control?

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Creating component from scratch
« Reply #19 on: May 22, 2021, 12:01:16 am »
As you saw, the component was still there but not visible. It needs a valid parent.

Now change the owner in the first button from self to Panel1

Press the first button to create the control.
Press the third button to destroy the owner "Panel1"

What happens to the control?

الذي حدث أنّ الـ control ظهر مباشرةً في الـ panel عند الضغط على الزر الأول ولكن عند الضغط على الزر الثالث إختفى الـ panel والـ control.

google translate:

"What happened is that the control appears directly in the panel when the first button is pressed, but when the third button is pressed, the panel and the control disappear."
La chose par la chose est rappelé.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Creating component from scratch
« Reply #20 on: May 22, 2021, 01:12:15 am »
If it appears on the Panel, that means you have Panel1 as its parent. Change it in the first button to Self.

So what we are after is:
Code: Pascal  [Select][+][-]
  1.   test:=TSimpleControl.Create(Panel1);
  2.   test.Parent := Self;

and when you destroy the owner Panel1, it will destroy the SimpleControl.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Creating component from scratch
« Reply #21 on: May 22, 2021, 01:36:40 am »
Can you change the control to use one of four colors randomly every time it paints itself. The four colors are:red, blue, green, and yellow.

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Creating component from scratch
« Reply #22 on: May 22, 2021, 01:17:48 pm »
If it appears on the Panel, that means you have Panel1 as its parent. Change it in the first button to Self.

So what we are after is:
Code: Pascal  [Select][+][-]
  1.   test:=TSimpleControl.Create(Panel1);
  2.   test.Parent := Self;

and when you destroy the owner Panel1, it will destroy the SimpleControl.

حسناً! جعلتُ الكائن المالكـ هو الـ panel والوالد هو self ،فعند النّقر على الزّر الأوّل ظهر الـ control على الـ form وليس على الـ panel وعند الضغط على الزّر الثالث إختفى كلٌّ من الـ panel والـ control.

google translate:

"Okay! I made the owner object the panel and the parent is the self. When the first button was clicked, the control appeared on the form and not on the panel, and when the third button was pressed, both the panel and the control disappeared."
La chose par la chose est rappelé.

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Creating component from scratch
« Reply #23 on: May 22, 2021, 01:20:37 pm »
Can you change the control to use one of four colors randomly every time it paints itself. The four colors are:red, blue, green, and yellow.


لا ،لا أعرف كود يختار بشكل عشوائي لوناً من أربعة ألوان مُحدّدة!


google translate:

"No, I don't know a code that randomly picks one of four specified colors!"
La chose par la chose est rappelé.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Creating component from scratch
« Reply #24 on: May 22, 2021, 02:13:44 pm »
Nothing to worry about. Every language comes with a way to get a random number.

Random, RandomRange, and RandomFrom are a few ways to get random numbers.

If you have an array of the four colors, you can choose one if you can produce random index. Here is a small program to give 20 random numbers between 0 and 4. The upper limit is *not* included:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Classes
  10.   { you can add units after this };
  11.  
  12. var
  13.   i, j: integer;
  14. begin
  15.   Randomize;
  16.   for j := 1 to 20 do
  17.   begin
  18.     i := Random(4);
  19.     WriteLn(i);
  20.   end;
  21.   ReadLn;
  22. end.

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Creating component from scratch
« Reply #25 on: May 22, 2021, 03:57:23 pm »
Nothing to worry about. Every language comes with a way to get a random number.

Random, RandomRange, and RandomFrom are a few ways to get random numbers.

If you have an array of the four colors, you can choose one if you can produce random index. Here is a small program to give 20 random numbers between 0 and 4. The upper limit is *not* included:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Classes
  10.   { you can add units after this };
  11.  
  12. var
  13.   i, j: integer;
  14. begin
  15.   Randomize;
  16.   for j := 1 to 20 do
  17.   begin
  18.     i := Random(4);
  19.     WriteLn(i);
  20.   end;
  21.   ReadLn;
  22. end.

حسناً! قُمت بتعديل TSimpleControl.Paint كالتالي وإضافة randomize في TForm1.FormCreate.

google translate:

"Okay! I modified TSimpleControl.Paint as follows and added randomize "in" the TForm1.FormCreate."

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   randomize;
  4. end;
  5.  

Code: Pascal  [Select][+][-]
  1. procedure TSimpleControl.Paint;
  2. var
  3.   R: TRect;
  4.   rnd_color: array [1..4] of tcolor;
  5. begin
  6.  
  7.   rnd_color[1]:=clyellow;
  8.   rnd_color[2]:=clblue;
  9.   rnd_color[3]:=clred;
  10.   rnd_color[4]:=clgreen;
  11.  
  12.   R := Rect(0,0,Width,Height);
  13.   with Canvas do
  14.   begin
  15.  
  16.     Brush.Color := rnd_color[random(4+1)+1];
  17.     Brush.Style:=bsSolid;
  18.     FillRect(R); //<--- this line does the work based on the color and style used above
  19.   end;
  20. end;
  21.  
  22.  
La chose par la chose est rappelé.

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Creating component from scratch
« Reply #26 on: May 22, 2021, 04:00:47 pm »
لقد قُمتُ بخطأ فنّي بسيط ،يُمكننا تعديل السطر التالي كتالي:

google translate:

"I made a simple technical error, we can modify the following line like this:"

Code: Pascal  [Select][+][-]
  1.     Brush.Color := rnd_color[random(4+1)+1];
  2.  

Code: Pascal  [Select][+][-]
  1.     Brush.Color := rnd_color[random(3+1)+1];
  2.  
La chose par la chose est rappelé.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Creating component from scratch
« Reply #27 on: May 22, 2021, 04:13:54 pm »
As you can see, from an idea to a real code. You are doing great.

If you change the parent of the SimpleControl, using your "Switch" button, you'll notice it changes its color. On Windows when I moved the form outside the screen and brought it back slowly, the SimpleControl had different colors at the same time. Check the attached image.

Canvas has a procedure Canvas.TextOut to draw text. Try to use it to print the word "Test" on the SimpleControl.
« Last Edit: May 22, 2021, 04:20:36 pm by engkin »

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Creating component from scratch
« Reply #28 on: May 22, 2021, 04:48:07 pm »
As you can see, from an idea to a real code. You are doing great.

If you change the parent of the SimpleControl, using your "Switch" button, you'll notice it changes its color. On Windows when I moved the form outside the screen and brought it back slowly, the SimpleControl had different colors at the same time. Check the attached image.

Canvas has a procedure Canvas.TextOut to draw text. Try to use it to print the word "Test" on the SimpleControl.

كلامكـ صحيح ،فعند الضغط على switch أدّى ذلكـ إلى نقل الـ control بلون مُختلف في كلّ مرّةٍ بين الـ panel والـ form ،ولكن لم أصل إلى تأثير في الألوان مثل الذي في الصورة التي أرفقتَها.

عموماً ،حاولتُ طباعة نصٍ كما أخبرتني ويبدو كلّ شيء صحيحاً إلّا أنّني لا أرى نصّاً قد طُبع ووضعت إحداثيّات جُزافيّةً في TextOut ظنّاً منّي أنّ ذلكـ يُدخل النّص في حيّز الـ control ولكنّني في الحقيقة لا أعرف كيفيّة تحديد موقع نصٍّ داخل الـ control بعد.

google translate:

"You are correct, when pressing switch led to the transfer of the control in a different color each time between the panel and the form, but I did not reach an effect on colors like the one in the picture that you attached.

In general, I tried to print text as you told me and everything seems correct except that I do not see a text printed and put random coordinates into TextOut because I thought that this puts the text in the control field "zone", but I really do not know how to locate a text inside the control yet."

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2.  
  3.  
  4. begin
  5.  
  6.  
  7.     test:=TSimpleControl.Create(panel1); //<--- "self" is the form itself,  will be the "owner" of the  component. It will free the component when the form is closed.
  8.  
  9.  
  10.     test.Parent:=self; //<--- where this component sits. Again, on the form. A panel could be used.
  11.  
  12.     test.Canvas.TextOut(10,10,'Test');
  13. end;
  14.  
  15.  
La chose par la chose est rappelé.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Creating component from scratch
« Reply #29 on: May 22, 2021, 05:00:18 pm »
Have you noticed your mistake?

You are not inside the Paint procedure.

 

TinyPortal © 2005-2018