0

I have a WPF application that reads a huge text file and shows it to textbox, but I need to know that all the file has been read before changing the normalize button to visible.

My question is how can I know that all the file was read and displayed in the textbox (is means that the textbox is still changing) and just after assign the button to visible?

        try
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == true)
            {
                using (StreamReader sr = new StreamReader(openFileDialog.FileName))
                {
                    FileBuff = await sr.ReadToEndAsync();
                    txtEditor.Text = FileBuff;
                    Normalize_button.IsEnabled = true;
                }             
            }
        }
        catch (Exception ex)
        {
            UiInvoke(() => txtEditor.Text = "Could not read the file");
        }
yosi
  • 23
  • 6
  • Do you mean you want to wait for the text to actually load in the textbox? Have you tried the `TextChanged` event? – stuartd May 27 '16 at 10:11
  • yes that what i mean... i read about "TextChanged" but I couldn't use it. – yosi May 27 '16 at 10:14
  • I suggest you to use BackgroundWorker. You can use the OnCompleted Event – Ugur May 27 '16 at 10:22
  • Sam, yes i tried it now but its not change the button to be visible :( – yosi May 27 '16 at 10:30
  • Button to be enabled or visibile? – Justin CI May 27 '16 at 11:31
  • enabled i mean , sorry – yosi May 27 '16 at 12:29
  • Is this code `txtEditor.TextChanged += (o, args) => MessageBox.Show("txtEditor.TextChanged");` shows message box? Are you subcsribe to `TextChanged` event after `InitializeComponent()` and before running logic from your question? Can you provide `Normalize_button` declaration from xaml? – Sam May 27 '16 at 12:34
  • in the first file i open its not work, in the second its works and showes 2 massageboxes... why its work like that? – yosi May 27 '16 at 12:50
  • Are you loading files simultaneously, starting to load second file before first file finish loading? And event will not be triggered if empty file was loaded. – Sam May 27 '16 at 13:09
  • No, i loading just one file a time. i mean that when i open the first file (and finish loading it) nothing happen, in the second time its showes 2 messageBoxs, at the third its showes 3 messageBoxs and etc... – yosi May 27 '16 at 13:33
  • That means you subscribe to `TextChanged` event every time you loading file. You should subscribe to this event just once. Try to find `InitializeComponent()` entry in your code-behing (xaml.cs) and place subscription right after it, but not in any other place in your code. And if it will work, try to add `Normalize_button.IsEnabled = true` right after message box showing code. – Sam May 27 '16 at 13:45
  • I dont understand what do you mean by find the InitializeComponent(), my code is in the browse_button_click() – yosi May 27 '16 at 14:29
  • I mean invoking of `InitializeComponent();` method inside constructor of your `UserControl` (or `Window`). Subscribing to event after this invoking (when `TextBox` will already exist) will guarantee than you will subscribe just once. – Sam May 27 '16 at 15:36

1 Answers1

1

bind BigText in XAML
I would also bind button IsEnabled

string bigText 
string BigText  
{
    get { return bigText; }
    set 
    {
        bigText = value;
        Normalize_button.IsEnabled = true;
        NotifyPropertyChanged("BigText");
    }
}

FileBuff = sr.ReadToEndAsync();
BigText = await FileBuff;

But you should be using a background worker so you have a callback event (OnCompleted)
And you also have the option to cancel the background worker
Look into priority binding

paparazzo
  • 44,497
  • 23
  • 105
  • 176