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)
{
....
}
}
}
....
....
}