Lazarus

Programming => LCL => Topic started by: nikel on October 15, 2021, 09:09:43 pm

Title: TRect event within a class
Post by: nikel on October 15, 2021, 09:09:43 pm
Hello, I'm trying to create a slider class. The code below works fine on main form but while running it within the class I'm getting error: Identifier not found "OnPaint";

Code: Pascal  [Select][+][-]
  1. Procedure TSlideBox.DoRun;
  2. Begin
  3.   TRect.Create(FBackRect, True);
  4.  
  5.   With FBackRect Do
  6.   Begin
  7.     Left:=8;
  8.     Top:=152;
  9.     Width:=618;
  10.     OnPaint:=@BackRectOnPaint;
  11.   End;
  12. End;

How can I set onpaint event within a class?
Title: Re: TRect event within a class
Post by: jamie on October 15, 2021, 09:54:21 pm
It has to be parented.

You need to provide a way to set the parent of your class which you know should be a TFORM for example.

And in that routine..

TForm(Parent).OnPaint := .....

Is that what you were looking for ?
Title: Re: TRect event within a class
Post by: howardpc on October 15, 2021, 10:00:58 pm
If TSlideBox is designed to be an independent control which can paint itself it must descend from TGraphicControl or TCustomControl, which both provide a Paint method and an OnPaint property.
All descendants of TCustomControl (such as TScrollBox and TForm) and all descendants of TGraphicControl (such as TShape) then inherit an OnPaint property which you can call.
Currently it looks as though TSlideBox has a different ancestry.
Title: Re: TRect event within a class
Post by: nikel on October 15, 2021, 10:49:22 pm
I'm new to oop, the class is defined as:

Code: Pascal  [Select][+][-]
  1. Type
  2.   PSlideBox = ^TSlideBox;
  3.  
  4.   { TSlideBox }
  5.  
  6.   TSlideBox = Class
  7.   Private
  8.     FName         : String;
  9. ...

When I run TSlideBox = Class(TForm1) I'm getting errors. Thank you for your replies.
Title: Re: TRect event within a class
Post by: howardpc on October 15, 2021, 11:27:32 pm
You can try
Code: Pascal  [Select][+][-]
  1. uses ... Controls, ... ;
  2.  
  3. type
  4.  
  5. TSlideBox = class(TCustomControl)
  6. private
  7.    FName: String;
  8. ...
  9. end;
However, if this is your first go at designing a new component, you may need a bit more help than this.
Not sure why you would ever need the PSlideBox type.
Title: Re: TRect event within a class
Post by: nikel on October 16, 2021, 12:06:47 am
I'm trying to create a TrackBar with two sliders.
Title: Re: TRect event within a class
Post by: ASerge on October 16, 2021, 08:43:07 am
Code: Pascal  [Select][+][-]
  1. Procedure TSlideBox.DoRun;
  2. Begin
  3.   TRect.Create(FBackRect, True);
  4.  
  5.   With FBackRect Do
  6.   Begin
  7.     Left:=8;
  8.     Top:=152;
  9.     Width:=618;
  10.     OnPaint:=@BackRectOnPaint;
  11.   End;
  12. End;
If your code is really like this, then you can simply delete line 3 - nothing will change.
Title: Re: TRect event within a class
Post by: bytebites on October 16, 2021, 08:56:26 am
Like so https://forum.lazarus.freepascal.org/index.php?topic=45063.0 ?
Title: Re: TRect event within a class
Post by: nikel on October 16, 2021, 10:55:39 am
Hello, I tried changing class definition to "TSlideBox = Class(TCustomControl)" but this time I got error for the TRect Canvas.

I found this at an external site:

Quote
... forget about OnClick, simply use Click

Code: Pascal  [Select][+][-]
  1. Public
  2.   Procedure Click; Override;

Code: Pascal  [Select][+][-]
  1. Procedure TLoginButton.Click;
  2. Begin
  3.   Inherited; // Call the inherited Click method.
  4.   // Do something new.
  5. End;
Title: Re: TRect event within a class
Post by: howardpc on October 17, 2021, 05:59:38 pm
I'm trying to create a TrackBar with two sliders.
I thought about how I would implement such a thing, and the attached test program and unit might give you some ideas.
I implemented mouse and keyboard support (but not design support if you want to turn it into a standalone component).


With two sliders there are various behaviours that you can support. In the attached example clicking on the track affects the lower slider if you click to the left of it, and clicking between the sliders causes them to move towards each other. Clicking to the right of the higher slider moves it further right.


Keyboard movement with arrow keys affects the lower slider. To move the upper slider, press Ctrl+arrow key.


I have not implemented working code for the vertical orientation, but left placeholders you can complete if this is what you need. It is almost untested, and only so far on Linux, so I am sure it has bugs. It is offered as a proof of concept.
Title: Re: TRect event within a class
Post by: nikel on October 17, 2021, 08:30:44 pm
Thank you for your reply. I tried it and seemed fine. I'd like to use it in my project if you excuse me.
Title: Re: TRect event within a class
Post by: howardpc on October 17, 2021, 08:35:14 pm
You're welcome. Use it and adapt it as needed.
TinyPortal © 2005-2018