Recent

Author Topic: Can function be used for change event  (Read 2225 times)

Joanna

  • Hero Member
  • *****
  • Posts: 807
Can function be used for change event
« on: April 23, 2024, 11:30:10 am »
Has anyone ever tried using a function with boolean result for a change event?
Currently I use procedure change_event (sender:tobject);

Sometimes change events get activated when no change actually occurred such as when a tedit loses focus. It would help handle this event easier with something like

Function change_event (sender:tobject):boolean;

So I could do Something like
If not Sendermethod.change_event (sender)
    Then exit;

I believe This type of technique is used for the function encode date and other things.
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

cdbc

  • Hero Member
  • *****
  • Posts: 1162
    • http://www.cdbc.dk
Re: Can function be used for change event
« Reply #1 on: April 23, 2024, 11:42:02 am »
Hi
I use this technique all the time, e.g.: in Iterator-Callbacks, where the result determines if we continue or stop iteration. Also some, if not many Events, benefit from a returning result.
But, even though exactly 'TNotifyEvent' would be hard to change nowadays, there's nothing stopping you from defining your own event-functions  ;)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

VisualLab

  • Sr. Member
  • ****
  • Posts: 355
Re: Can function be used for change event
« Reply #2 on: April 23, 2024, 12:18:23 pm »
Has anyone ever tried using a function with boolean result for a change event?
Currently I use procedure change_event (sender:tobject);

Sometimes change events get activated when no change actually occurred such as when a tedit loses focus.

Specific events are used to handle such situations. In case of loss of focus, handling the OnExit event helps. There is no need to come up with an unusual solution.

Joanna

  • Hero Member
  • *****
  • Posts: 807
Re: Can function be used for change event
« Reply #3 on: April 23, 2024, 01:48:36 pm »
In this case the event is oneditingdone I suppose different controls have different types of change events but I try to handle them all through one event procedure. Which is why it’s important to know if the control actually changed..,

I have no idea what would be involved in creating a new type of event that uses a function instead of procedure but acts like tnotifyevent. How are events created? Can I create one in my tedit descendant similar to oneditingdone ?
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

VisualLab

  • Sr. Member
  • ****
  • Posts: 355
Re: Can function be used for change event
« Reply #4 on: April 23, 2024, 02:15:30 pm »
In this case the event is oneditingdone I suppose different controls have different types of change events but I try to handle them all through one event procedure. Which is why it’s important to know if the control actually changed..,

And that's probably why you have trouble detecting which of these events did and did not occur. I understand that this is less code to create. This is the temptation to make the program simpler. But that's not always a good idea. The more we want to increase the capabilities (functionality) of the program, the more complex it becomes. It is impossible to go below a certain level of complexity while maintaining its established functionality.

I don't know how you organize your code, but for larger programs (main window, several dialog windows) I avoid placing more program code in form units. Instead, I create classes: a data container, a view class, and a controller class. Sometimes I have several (two or three) containers. Likewise with controllers. It all depends on the complexity of the program (the problem/issue it is supposed to solve). I usually have at least a couple of views. Sometimes these are classes derived from TCustomControl. And sometimes they come from TComponent and are associated with typical controls, e.g. TEdit, TButton, TMemo, etc. Then such a view is coupled with the mentioned controls. Events generated by views (or controls coupled to them) are usually handled by the controller, sometimes also by the container (usually there are few of them).

This allows me to organize and structure the program design. This is simply a practical use of the MVC design pattern. Of course, you need to exercise moderation. Patterns are helpful for more extensive programs. There is no need to use them in small programs.

I have no idea what would be involved in creating a new type of event that uses a function instead of procedure but acts like tnotifyevent. How are events created? Can I create one in my tedit descendant similar to oneditingdone ?

Events are pointers to methods in objects. Your best bet is to look at some existing classes in the library (in this case LCL). I did that.

jamie

  • Hero Member
  • *****
  • Posts: 6226
Re: Can function be used for change event
« Reply #5 on: April 23, 2024, 10:53:10 pm »
In this case the event is oneditingdone I suppose different controls have different types of change events but I try to handle them all through one event procedure. Which is why it’s important to know if the control actually changed..,

I have no idea what would be involved in creating a new type of event that uses a function instead of procedure but acts like tnotifyevent. How are events created? Can I create one in my tedit descendant similar to oneditingdone ?

For the OnEditingDone event do this..

While in the event, test for the focus..

If Self.Focused then We are still in the control and someone hit the return key.
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 14619
  • Sensorship about opinions does not belong here.
Re: Can function be used for change event
« Reply #6 on: April 24, 2024, 10:24:31 am »
You can look at how the modified property is implemented for edit/memo
You can also have a look at the observer/observed pattern that is available for any control
https://www.freepascal.org/docs-html/rtl/classes/ifpobserved.fponotifyobservers.html
Also note that you can indeed define a function, since TMethod accepts not only a procedure, but also a function.



« Last Edit: April 24, 2024, 10:48:18 am by Thaddy »
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

Joanna

  • Hero Member
  • *****
  • Posts: 807
Re: Can function be used for change event
« Reply #7 on: April 24, 2024, 12:48:19 pm »
Thanks for all the insights  :)

Routing events is indeed tricky because the sender is the simple control and I need to find out what owns it to route the event to the correct place. This usually involves inspecting the parent hierarchy of the sender to find what class it came from. I can then call the change event of the class responsible for the sender and pass the sender there.

 I don’t have a lot of code inside the form itself, most of the code is located inside of classes and uses virtual methods. I also use “is” to identify what controls are and then typecast them to call appropriate virtual method.

@jamie it isn’t so much hitting return key as clicking on another control pr trying to navigate with tab keys firing off the editing done event when nothing has changed.

@thaddy yes indeed it’s possible to declare a function but getting editingdone to connect to it is another story.
The modified seems like a useful property I didn’t know about it. Unfortunately other controls don’t seem to have it. I will test it out.

The tmethod thing does that let me define new methods ?

✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

jamie

  • Hero Member
  • *****
  • Posts: 6226
Re: Can function be used for change event
« Reply #8 on: April 25, 2024, 12:26:42 am »
The modified property would be a good one to use however, I think it needs to be cleared when the control gets reentered because  it may retain its last value.

 The idea behind that is you can always have a bunch of controls on a form, each having a MODIFIED property and at the end of the editing session you can then scan all of these controls for changes and then update some external data area. etc.

 but on initial entry of the form, it maybe a good idea to clear all of these.
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 14619
  • Sensorship about opinions does not belong here.
Re: Can function be used for change event
« Reply #9 on: April 25, 2024, 10:44:05 am »
but on initial entry of the form, it maybe a good idea to clear all of these.
In principle that already happens in a well written control, e.g. Tmemo, but it is a good idea to do it manually anyway.
The modified property is all about persist: it stays until the application is persisted in some form, e.g. open changes are stored to disk or ... an edit is cancelled. The latter is often forgotten by the programmer, even in professional software. It needs some kind of stack.
That is easy to do with e.g. TRecall.
« Last Edit: April 25, 2024, 11:01:21 am by Thaddy »
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

Joanna

  • Hero Member
  • *****
  • Posts: 807
Re: Can function be used for change event
« Reply #10 on: April 25, 2024, 05:14:23 pm »
It seems like the modified property should start with a value of false and be reset everytime there is an editingdone event. 
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

Weiss

  • Full Member
  • ***
  • Posts: 144
Re: Can function be used for change event
« Reply #11 on: May 11, 2024, 07:18:28 am »
The modified property would be a good one to use however, I think it needs to be cleared when the control gets reentered because  it may retain its last value.

 The idea behind that is you can always have a bunch of controls on a form, each having a MODIFIED property and at the end of the editing session you can then scan all of these controls for changes and then update some external data area. etc.

 but on initial entry of the form, it maybe a good idea to clear all of these.

does it matter if you clear on exit? I always have this dilemma

Thaddy

  • Hero Member
  • *****
  • Posts: 14619
  • Sensorship about opinions does not belong here.
Re: Can function be used for change event
« Reply #12 on: May 11, 2024, 08:24:13 am »
Yes, you can clear on exit as long as you save your work! Otherwise there is no way left to determine if some content has to be saved or persisted in another way. That can only be determined by reading modified.
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

Joanna

  • Hero Member
  • *****
  • Posts: 807
Re: Can function be used for change event
« Reply #13 on: May 12, 2024, 12:04:49 am »
I have implemented the code To check modified property when entering the change event procedure and exit if not modified. It seems to work ok and the event for the button click immediately follows the editingdone event for tedit control which lost focus.
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

 

TinyPortal © 2005-2018