Recent

Author Topic: TRect event within a class  (Read 6425 times)

nikel

  • Full Member
  • ***
  • Posts: 186
TRect event within a class
« 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?
« Last Edit: October 16, 2021, 02:22:57 am by nikel »

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: TRect event within a class
« Reply #1 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 ?
The only true wisdom is knowing you know nothing

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TRect event within a class
« Reply #2 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.

nikel

  • Full Member
  • ***
  • Posts: 186
Re: TRect event within a class
« Reply #3 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.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TRect event within a class
« Reply #4 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.

nikel

  • Full Member
  • ***
  • Posts: 186
Re: TRect event within a class
« Reply #5 on: October 16, 2021, 12:06:47 am »
I'm trying to create a TrackBar with two sliders.
« Last Edit: October 16, 2021, 12:43:31 am by nikel »

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: TRect event within a class
« Reply #6 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.

bytebites

  • Hero Member
  • *****
  • Posts: 625
Re: TRect event within a class
« Reply #7 on: October 16, 2021, 08:56:26 am »

nikel

  • Full Member
  • ***
  • Posts: 186
Re: TRect event within a class
« Reply #8 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;

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TRect event within a class
« Reply #9 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.

nikel

  • Full Member
  • ***
  • Posts: 186
Re: TRect event within a class
« Reply #10 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.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TRect event within a class
« Reply #11 on: October 17, 2021, 08:35:14 pm »
You're welcome. Use it and adapt it as needed.

 

TinyPortal © 2005-2018