Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to detect that memo is restored to the original text?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Suppose you have writen a text-editor and have used a TMemo.
when the text changes, Modified flag set to True. Now if you execute Undo command there is situations that you text is reset to its original content. The question is this:

"How we can determine that the text is now Not-Modified ?"

NOTE 1:I have tried some methods but all of them determine some cases correctly and ... for others (eg correct changes by deleting but don't correct changes by insertion.) So your suggestion must apply in these cases :Inserting (Keyboard typing and Paste), Deleting (BkSpace,Del and Cut), Replacing (Selecting some text by Keyboard, Mouse or Select-All command and then press any key) and any other situations.

NOTE2:MS-Notepad does very BAD in this case, so don't compare your suggestion result with notepad.
 
Hi, MF !

I believe that monitoring all the user's actions like Inserting, Deleting, Replacing and so on would lead to a really huge event handler.
An easier way to detect a change to the memo text is to make a copy of the initial memo text before you let the user edit the memo. If your memo takes less than 256 characters (Memo.MaxLength < 256) you can use the memo's text property (SavedText := Memo.Text). Otherwise you have to deal with the memo's lines :

Code:
 :
var 
  MemoTextCopy : TStringList; 
  i  : integer ; 
begin
  // Before the user edits the memo text : 
  // Make a copy of the memo text : 
  MemoTextCopy := TStringList.Create; 
  MemoTextCopy.Clear ; 
  for i := 0 to Memo.Lines.Count - 1 do 
    MemoTextCopy.Add(Memo.Lines[i]) ;
  :
  // User edits memo text here 
  : 
  // After editing : line by line comparison : 
  Memo.Modified := false ; 
  if Memo.Lines.Count <> MemoTextCopy.Count then // line count 
      Memo.Modified := true                      // has changed
  else
    for i := 0 to Memo.Lines.Count - 1 do 
      if MemoTextCopy.Strings[i] <> Memo.Lines[i] then
        Memo.Modified := true ; 
  MemoTextCopy.Free ;

As the user finishes his editting you compare the saved memo text to the current memo text. You can do this line by line as in the above example, but you will then detect a change in word wrapping as a change to the memo text. If you consider this to be no change you would have to compare character by character :

Code:
  : 
  CopyLine := 0 ;
  CopyCharIndex := 0 ; 
  MemoLine := 0 ; 
  MemoCharIndex := 0 ;  
  CopyChar := 'x' ; // dummy to let the while loop start
  MemoChar := 'x' ; // dummy to let the while loop start
  Memo.Modified := false ; 
  while (Memo.Modified = false) and 
    (CopyChar <> '' ) and 
    (MemoChar <> '' )  do
  begin
    // pick &quot;next&quot; character from the saved copy of the memo text:
    Inc(CopyCharIndex) ; 
    if CopyCharIndex > Length(MemoTextCopy.Strings[CopyLine])
    begin   // exceeding current string length
      Inc(CopyLine) ; // take next string
      if CopyLine < MemoTextCopy.Count then // not exceeding 
      begin                                 // number of strings
        CopyCharIndex := 1 ; 
        CopyChar := copy(MemoTextCopy.Strings[CopyLine], CopyCharIndex, 1) ; 
      end
      else
        CopyChar := '' ; // no further saved character left
    end 
    else 
      CopyChar := copy(MemoTextCopy, CopyCharIndex, 1) ;   

    // pick &quot;the next&quot; character from the current memo text : 
    Inc(MemoCharIndex) ; 
    :  // the same as above but with   Memo.Lines   instead 
    :  //  of   MemoTextCopy.Strings ... 
    MemoChar := copy(Memo.Lines[MemoLine], MemoCharIndex, 1) ; 

    // Compare the two characters : 
    if MemoChar <> CopyChar then 
      Memo.Modified := true ; 
  end ;

I think this will do the job. I hope I could help you
Bye
Thomas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top