Forum > Beginners

[SOLVED]Accessing OnChange event of SynEdit created by code

(1/1)

TomTom:
[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: ---PageControl.ActiveTab.Caption: = PageControl.ActiveTab.Caption + '(*)';
--- End code ---
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 :)

Martin_fr:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---SynEdit1.OnChange := @DoOnSynEditChange; procedure TForm1.DoOnSynEditChange(Sender: TObject);begin end; 
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TSynStatusChanges = set of (scCaretX, scCaretY,    scLeftChar, scTopLine, scLinesInWindow, scCharsInWindow,    scInsertMode, scModified, scSelection, scReadOnly,    scFocus,     // received or lost focus    scOptions    // some Options were changed (only triggered by some optinos)   ); 
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---SynEdit.OnStatusChange := @DoSynStatusEventAnd 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:
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: ---File_name.txt ***********************************
--- End code ---
:) But it is easy to do :)

Bart:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TEPlusForm.OnEditorStatusChange(Sender: TObject;  Changes: TSynStatusChanges);var  Line, Col: LongInt;begin...  with (Sender as TEditor) do //TEditor is a TSynEdit   begin    if (scCaretX in Changes) or (scCaretY in Changes) then    begin      Col := CaretX;      Line := CaretY;      StatusBar.Panels[pXY].Text := Format('%s %-3d  %s %-3d',[SLine,Line,SCol,Col]);    end;    if (scModified in Changes) then    begin      if Modified then StatusBar.Panels[pMod].Text := SModified      else StatusBar.Panels[pMod].Text := '';    end;
You see that I check wether Modified is true, because after SaveToFile and LoadFromFile I set Editor.Modified to False.

Bart

TomTom:
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 :) 

 

Navigation

[0] Message Index

Go to full version