Recent

Author Topic: [SOLVED]Accessing OnChange event of SynEdit created by code  (Read 968 times)

TomTom

  • Full Member
  • ***
  • Posts: 108
[SOLVED]Accessing OnChange event of SynEdit created by code
« on: January 07, 2019, 01:08:48 pm »
[EDIT] - or maybe I should use Form1.OnKeyDown or sth like that to check which tab is active and then look for SynEdit instance and run some procedure that is run on OnChange event of that SynEdit.. Ehh this is so confusing :P


Ok It will be difficult to explain but I will try.
I wrote a text editor based on tabs that I can add and remove using the buttons.
I would like the title of the active tab to change when the user makes some change in the text, e.g.
The name of the tab is New File.txt
The user enters some change in SynEdit (he adds his content) and then the name of the active bookmark changes to "New_file.txt (*)" - all that he knows that he will have to save the changes in the file because he changed something.
The change of the tab title is simple
Code: [Select]
PageControl.ActiveTab.Caption: = PageControl.ActiveTab.Caption + '(*)'; but I do not know how to refer to the OnChange event of the component that I created dynamically using the code.
Or maybe I know how to refer to it but I do not know when.

I hope this is clear :)
« Last Edit: January 12, 2019, 05:03:25 pm by TomTom »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5163
    • wiki
Re: Accessing OnChange event of SynEdit created by code
« Reply #1 on: January 07, 2019, 01:31:23 pm »
Code: Pascal  [Select]
  1. SynEdit1.OnChange := @DoOnSynEditChange;
  2.  
  3. procedure TForm1.DoOnSynEditChange(Sender: TObject);
  4. begin
  5.  
  6. end;
  7.  

OnChange is called by SynEdit whenever an undo-able (or redo-able) action occurs.

That is usually any input or text change by the user.
That also includes change via SynEdit.TextBetweenPoints and similar.

That does not  include SynEdit.Lines[n] := 'Foo'; 
Access to Lines directly does not create undo events.


If you need more feedback from the SynEdit, you can look into SynEdit.OnStatusChange, which includes a reason, and sends for the following reasons
Code: Pascal  [Select]
  1. TSynStatusChanges = set of (scCaretX, scCaretY,
  2.     scLeftChar, scTopLine, scLinesInWindow, scCharsInWindow,
  3.     scInsertMode, scModified, scSelection, scReadOnly,
  4.     scFocus,     // received or lost focus
  5.     scOptions    // some Options were changed (only triggered by some optinos)
  6.    );
  7.  


If you want to assign an event by code, but do not know how to declare the method for it, just let the IDE do it.

Write
Code: Pascal  [Select]
  1. SynEdit.OnStatusChange := @DoSynStatusEvent
And while the caret is at "DoSynStatusEvent" press Shift-Ctrl-C

For more tricks like this read http://wiki.lazarus.freepascal.org/Lazarus_IDE_Tools
And for an showcase of some of the IDE features go to http://wiki.lazarus.freepascal.org/New_IDE_features_since

TomTom

  • Full Member
  • ***
  • Posts: 108
Re: Accessing OnChange event of SynEdit created by code
« Reply #2 on: January 07, 2019, 01:54:40 pm »
The problem was that it did not reach me that I could refer to PageControl1 through my caption changing procedure. I thought the program would spill even though I did not even try it that way. Now the program works :) Thank you for your help. It turned out that I thought well but was afraid to try to implement this idea: P
Now I have to write a conditional function, which will check if * is already in the caption tab because I finish with the tabs in the style
Code: [Select]
File_name.txt *********************************** :) But it is easy to do :)

Bart

  • Hero Member
  • *****
  • Posts: 3359
    • Bart en Mariska's Webstek
Re: Accessing OnChange event of SynEdit created by code
« Reply #3 on: January 07, 2019, 02:21:35 pm »
Here's a code snippet from my own editor (TSynEdits on a PageControl, just like you), but unlike you I have a "Modified" text in the statusbar, but that's just an implementation detail.

Code: Pascal  [Select]
  1. procedure TEPlusForm.OnEditorStatusChange(Sender: TObject;
  2.   Changes: TSynStatusChanges);
  3. var
  4.   Line, Col: LongInt;
  5. begin
  6. ...
  7.   with (Sender as TEditor) do //TEditor is a TSynEdit
  8.   begin
  9.     if (scCaretX in Changes) or (scCaretY in Changes) then
  10.     begin
  11.       Col := CaretX;
  12.       Line := CaretY;
  13.       StatusBar.Panels[pXY].Text := Format('%s %-3d  %s %-3d',[SLine,Line,SCol,Col]);
  14.     end;
  15.     if (scModified in Changes) then
  16.     begin
  17.       if Modified then StatusBar.Panels[pMod].Text := SModified
  18.       else StatusBar.Panels[pMod].Text := '';
  19.     end;

You see that I check wether Modified is true, because after SaveToFile and LoadFromFile I set Editor.Modified to False.

Bart

TomTom

  • Full Member
  • ***
  • Posts: 108
Re: Accessing OnChange event of SynEdit created by code
« Reply #4 on: January 12, 2019, 04:59:47 pm »
Thank You Martin and thank You Bart :) and to the others This are amazing things I have learnt from all of You :) 
I know it's basic stuff and probably I should start with that a year ago (thats when I started using Lazarus/Freepascal). Maybe then it would took me less time and would be much easier to create my first big app Mammoth ThumbNAILER :P (program doing some 'crazy' image conversions for insitution in wich I work). Instead, I've jumped just right into 'TOO SERIOUS' stuff :) . Thank You once again :) Cheers :)