locked
undo/redo with protected text in RichTextBox RRS feed

  • Question

  • hi, we are using RichTextBox to display text. we are having issues to undo/redo with protected text.

    here's the workflow, normally we have pure text and in this case undo/redo works perfectly. occasionally we need to mark certain text to be "Protected". we do the validation and convert the Rtf on the fly and do the following to assign it back to the RTB:

    m_rtbText.Rtf = sRtf

    the problem is after step over this line, the

    m_rtbText.CanUndo

    property will be set to False internally, as a result we lost all the undo steps. To give you an example, here is what it looks like before the validation:

    \cf1\i0\v\f2 <PSI_START_OBJECT><PSI_MEDIA><Media2><MediaID>16346</MediaID><Name>\f3\rtlch\'e4\'d4\'d1\'c9 \'cc\'e6\'ed\'c9</\f2\ltrch Name><FileName>N:\\media\f3\rtlch\\\'e4\'d4\'d1\'c9 \'cc\'e6\'ed\'c9_1.\f2\ltrch wav</FileName><Length>00:00:33.0</Length><OutCue></OutCue><Description></Description></Media2><PSI_MID_OBJECT\f3\rtlch >\v0\f0\'c7\'e1\'ca\'de\'d1\'ed\'d1 \'c7\'e1\'c5\'ce\'c8\'c7\'d1\'ed : \cf2\'e4\'d4\'d1\'c9 \'cc\'e6\'ed\'c9\par
    \cf1\'c7\'e1\'e3\'cf\'f8\'c9 : \cf2\f3 00:00:33.0\cf3\i\f4 .\v <\f1\ltrch PSI_END_OBJECT\f4\rtlch >\cf0\v0\f1\ltrch\par

    and here's what looks like after the validation:

    \cf1\i0\protect\v\f2 <PSI_START_OBJECT><PSI_MEDIA><Media2><MediaID>16346</MediaID><Name>\f3\rtlch\'e4\'d4\'d1\'c9 \'cc\'e6\'ed\'c9</\f2\ltrch Name><FileName>N:\\media\f3\rtlch\\\'e4\'d4\'d1\'c9 \'cc\'e6\'ed\'c9_1.\f2\ltrch wav</FileName><Length>00:00:33.0</Length><OutCue></OutCue><Description></Description></Media2><PSI_MID_OBJECT\f3\rtlch >\v0\f0\'c7\'e1\'ca\'de\'d1\'ed\'d1 \'c7\'e1\'c5\'ce\'c8\'c7\'d1\'ed : \cf2\'e4\'d4\'d1\'c9 \'cc\'e6\'ed\'c9\par
    \cf1\'c7\'e1\'e3\'cf\'f8\'c9 : \cf2\f3 00:00:33.0\cf3\i\f4 .\v <\f1\ltrch PSI_END_OBJECT\f4\rtlch >\cf0\protect0\v0\f1\ltrch\par

    as you can see, the only difference here are the 2 "protect" entries. Text wise they are exactly the same.

    could this be a Microsoft bug? FYI we are on .NET framework 4.6.2, VS2015 Update3

    thanks

    Jia

    Monday, May 1, 2017 5:28 PM

All replies

  • Hi Jia,

    I'm sorry that I can not reproduce your issue and I need some more details about it, so

    >>we do the validation and convert the Rtf on the fly and do the following to assign it back to the RTB: m_rtbText.Rtf = sRtf

    Could you please provide some code to explain the sRtf? And how to convert the Rtf?

    >>here is what it looks like before the validation:

    What validation do you perform?

    >>as a result we lost all the undo steps

    I have a test and I can use this method, maybe I have not reproduced your project, but please refer to the below test:

            private void btnLoad_Click(object sender, EventArgs e)
            {
                string path = @"D:\13456.rtf";
                richTextBox1.Rtf = File.ReadAllText(path);
            }
    
            private void btnUndo_Click(object sender, EventArgs e)
            {
                richTextBox1.Undo();
            }
    
            private void btnRedo_Click(object sender, EventArgs e)
            {
                richTextBox1.Redo();
            }

    Please provide more details and some code that I can reproduce your issue.

    Hope it helps!

    Best Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 2, 2017 8:39 AM
  • hi Stanly,

    thanks for the reply. you are not loading the correct text. the thing I pasted is the Rtf text not the text what we see on the front. we have a media object that's embedded into rtf and at the front we see the protected media tag so it cannot be modified. the validation is on the tag so that we put the "\protect" around it, see below highlighted they were added in the validation code:

    \cf1\i0\protect\v\f2 <PSI_START_OBJECT><PSI_MEDIA><Media2><MediaID>16346</MediaID><Name>\f3\rtlch\'e4\'d4\'d1\'c9 \'cc\'e6\'ed\'c9</\f2\ltrch Name><FileName>N:\\media\f3\rtlch\\\'e4\'d4\'d1\'c9 \'cc\'e6\'ed\'c9_1.\f2\ltrch wav</FileName><Length>00:00:33.0</Length><OutCue></OutCue><Description></Description></Media2><PSI_MID_OBJECT\f3\rtlch >\v0\f0\'c7\'e1\'ca\'de\'d1\'ed\'d1 \'c7\'e1\'c5\'ce\'c8\'c7\'d1\'ed : \cf2\'e4\'d4\'d1\'c9 \'cc\'e6\'ed\'c9\par
    \cf1\'c7\'e1\'e3\'cf\'f8\'c9 : \cf2\f3 00:00:33.0\cf3\i\f4 .\v <\f1\ltrch PSI_END_OBJECT\f4\rtlch >\cf0\protect0\v0\f1\ltrch\par

    that being said, in your test code you may type some text like "hello world, this is some sample text". hit undo and redo to make sure it's working as expected. now add a button "Test" and put a breakpoint there. once you hit the break point, examine the rtbText.Rtf value and you will see it's more or less similar to what I posted above. then set your

    sRtf = "something different than the original Rtf text", in our case we are injecting the \protect. once done, assign it back to rtbText.Rtf

    rtbText.Rtf = sRtf;

    at this point hit F5 to continue, you will see the undo stack is lost.

    let me know if you can reproduce it. thanks.

    Tuesday, May 2, 2017 2:10 PM
  • Have you tried to protect the fragment using SelectionProtected property instead of changing Rtf? However executing Undo will remove the protection, which is probably undesirable. If you can detect this situation, the execute one more Undo or Redo programmatically.

    Wednesday, May 3, 2017 5:18 AM