Steve
Steve

Reputation: 732

c# wpf change button content execute some code and then change it back

I have a simple wpf c# app that takes the text from the inputField tries to find some info there and returns the result into outputField. Here is the code:

private void FindButton_Click(object sender, RoutedEventArgs e)
{
    try
    {
        string parsed = string.Empty;
        if (string.IsNullOrWhiteSpace(new TextRange(InputField.Document.ContentStart, InputField.Document.ContentEnd).Text));
        {
            OutputField.Document.Blocks.Clear();
            MessageBox.Show("Empty input");
        }
        else
        {
            Parser nOb = new Parser(new TextRange(InputField.Document.ContentStart, InputField.Document.ContentEnd).Text);
            string[] result = nOb.findAddresses();

            if (result.Length == 0)
            {
                OutputField.Document.Blocks.Clear();
                MessageBox.Show("Nothing found");
            }
            else
            {
                for (int i = 0; i < result.Length; i++)
                {
                    parsed += result[i] + Environment.NewLine;
                }

                OutputField.Document.Blocks.Clear();
                OutputField.Document.Blocks.Add(new Paragraph(new Run(parsed)));

                MessageBox.Show("Success");
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message);
    }
}

What I want to do is to change FindButton text from "Find" to "Searching..." and back when the search is completed. I tried to add the following code just before try{}:

FindButton.Content = "Searching...";
FindButton.IsEnabled = false;

And changed it back after try{} to "Find" but it didn't work. As I read somewhere I need to use async method here or threading. I found a few solutions here and tried to add "async" to my function and also changed the code:

await Task.Run(() => {
//My code which is above
});

But it started returning the following error:

MS.Internal.PtsHost.UnsafeNativeMethods.PTS.SecondaryException NullReferenceException

I'm completely new to these topics and don't know how to make it work. Please someone help.

Upvotes: 0

Views: 196

Answers (1)

mm8
mm8

Reputation: 169200

Assuming that your findAddresses() method access UI elements and must be executed on the UI thread you could try to use Task.Delay to give the UI thread a chance to update the FindButton just before you kick of your operation. Try this:

private async void FindButton_Click(object sender, RoutedEventArgs e)
{
    FindButton.Content = "Searching...";
    FindButton.IsEnabled = false;
    await Task.Delay(1);

    try
    {
        string parsed = string.Empty;
        if (string.IsNullOrWhiteSpace(new TextRange(InputField.Document.ContentStart, InputField.Document.ContentEnd).Text)) ;
        {
            OutputField.Document.Blocks.Clear();
            MessageBox.Show("Empty input");
        }
                else
                {
            Parser nOb = new Parser(new TextRange(InputField.Document.ContentStart, InputField.Document.ContentEnd).Text);
            string[] result = nOb.findAddresses();

            if (result.Length == 0)
            {
                OutputField.Document.Blocks.Clear();
                MessageBox.Show("Nothing found");
            }
            else
            {
                for (int i = 0; i < result.Length; i++)
                {
                    parsed += result[i] + Environment.NewLine;
                }

                OutputField.Document.Blocks.Clear();
                OutputField.Document.Blocks.Add(new Paragraph(new Run(parsed)));

                MessageBox.Show("Success");
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message);
    }

    FindButton.Content = "Default";
    FindButton.IsEnabled = true;
}

Upvotes: 1

Related Questions