0

Question: Is there any way to detect the occurrence of a file load in a RichTextBox (RTB) of a WPF app? I haven't find such an event in this list of events; or maybe there is some event in that list that can be used for a work around to achieve the following:

Background I'm allowing user to load a file in the RTB and close it after making changes (if needed). But before the user closes the file my app checks if the changes were made by placing bTextChanged flag in the TextChanged event. But I noticed that the TextChanged event is triggered even when a file is loaded. And even worst, the event is triggered for every character of the newly loaded file - that eventually can degrade the performance of the app if the loaded file is too long. so maybe there is a work around to make the TextChanged event triggered only when a text in the file is changed after the file was loaded.

public partial class MainWindow : Window
{
    string sgFileName = "";
    bool bTextChanged = false;
    ....
    ....
        private void BtnOpenFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Rich Text Format (*.rtf)|*.rtf|All files (*.*)|*.*";
            if (dlg.ShowDialog() == true)
            {
                sgFileName = dlg.FileName;
                FileStream fileStream = new FileStream(sgFileName, FileMode.Open);
                TextRange range = new TextRange(mainRTB.Document.ContentStart, mainRTB.Document.ContentEnd);
                range.Load(fileStream, DataFormats.Rtf);
            }
        }

        private void MainRTB_TextChanged(object sender, TextChangedEventArgs e)
        {
            bTextChanged = true;
        }

    private void BtnCloseDocument_Click(object sender, RoutedEventArgs e)
    {
        if (bTextChanged)
        {
            MessageBoxResult result = MessageBox.Show("Content has changed, do you want to save the changes?", "Content has Changed!", MessageBoxButton.YesNoCancel);
            switch (result)
            {
                ....
            }
        }
    }
....
....
}
nam
  • 21,967
  • 37
  • 158
  • 332
  • Can't you just unhook the text changed handler, load file, hook the handler back up? Alternately, convert the document to a string before and after and compare the two. https://stackoverflow.com/questions/2447856/saving-flowdocument-to-sql-server – Andy Apr 25 '19 at 17:01
  • @Andy Your suggestion of unhooking and hooking back the handler worked (thank you). But I read that more needs to be done when hooking and unhooking . I just used one liner for unhooking and one liner for hooking back. Is there anything else that I need to be done? It's just a simple app with a simple `TextChanged` handler shown in my post. – nam Apr 25 '19 at 19:48
  • What else did you read needs doing? I wouldn't think there was anything else. – Andy Apr 25 '19 at 19:59
  • @Andy Your suggestion seems to be correct but when I read some responses such as [this](https://stackoverflow.com/a/91853/1232087) along with some other responses there it seems they are doing lot more than what I did using your suggestion. – nam Apr 25 '19 at 20:33
  • @Andy You suggestion working for me so far (thank you). If you like you can convert your comment into a `response` and I'll mark it as an `answer` so other users can benefit from your response, as well. – nam Apr 26 '19 at 18:42

0 Answers0