Search code examples
c#wpfmessagebox

If messagebox is shown halt the rest execution of the function in c# wpf application


I have the following method that browse a file for the user.

public void BrowseFile(TextBox filanametextbox, TextBlock textblocname, DataGrid datagrid, Button browsebutton, Button loadbutton)
        {
            // Create OpenFileDialog
            OpenFileDialog openFileDlg = new OpenFileDialog();

            // Launch OpenFileDialog by calling ShowDialog method
            Nullable<bool> result = openFileDlg.ShowDialog();

            // Get the selected file name and display in a TextBox.
            // Load content of file in a TextBlock
            if (result == true)
            {
                filanametextbox.Text = openFileDlg.FileName;
                textblocname.Text = "Created on: " + File.GetCreationTime(openFileDlg.FileName).ToString() + "\n";

                //Debug.WriteLine(File.GetCreationTime(openFileDlg.FileName).ToString());

                var datatablematrix = ConvertToDataTable(filePath: openFileDlg.FileName);

                if (browsebutton.Name.ToString()=="BrowseButton")
                {
                    if (!filanametextbox.Text.Contains("Files.csv"))
                    {
                        MessageBox.Show("The file imported is an invalid format file! \n Please check that you have imported the correct one.", "Warning", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    }
                }

                else if (browsebutton.Name.ToString()=="BrowseButtonLayout")
                {
                    if (!filanametextbox.Text.Contains("Layout.csv"))
                    {
                        MessageBox.Show("The file imported is an invalid layout file! \n Please check that you have imported the correct one.", "Warning", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    }
                }

                else if (browsebutton.Name.ToString() == "BrowseButtonBC")
                {
                    if (!filanametextbox.Text.Contains("BusinessChecks.csv"))
                    {
                        MessageBox.Show("The file imported is an invalid business checks file! \n Please check that you have imported the correct one.", "Warning", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    }
                }

                datagrid.DataContext = datatablematrix.DefaultView;
            }

            // Set filter for file extension and default file extension  
            openFileDlg.DefaultExt = ".txt";
            openFileDlg.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";

            // Set initial directory
            openFileDlg.InitialDirectory = @"C:\Documents\";

            // Multiple selection with all file types    
            openFileDlg.Multiselect = true;

            browsebutton.IsEnabled = true;
            loadbutton.IsEnabled = true;
        }

What I want somehow is when the message box is triggered by one of the three if-statements to halt the execution for the rest of the function. Which means that the Data table won't get filled and the loadbutton won't be enabled whatsoever.

Initial state of the app

enter image description here

State on message box

enter image description here

When the user will click OK on the message box I want the Load Button to still be disabled and the Data Grid table (black box) not filled with values.

Searching online I found this SO question, which argues to create a bool function. Although, I am not quite sure how to embed this solution in my single function.


Solution

  • What I want somehow is when the message box is triggered by one of the three if-statements to halt the execution for the rest of the function.

    If you want to halt further execution of a function in c# you can use return; which exits out of the current function, without executing any further code.

    private bool ExampleVoid() {
        MessageBox.Show("The file imported is an invalid layout file! \n Please check that 
            + you have imported the correct one.", "Warning", MessageBoxButton.OK, 
            MessageBoxImage.Exclamation);
        
        // Return out of function because an error happened
        return;
    }
    

    Beware that if your function returns something for example a bool you need to add the corresponding values tot the return statment. In the case of a bool function eiter false or true.

    Example:

    private bool ExampleBool() {
        // Halt Execution of Function and return out of it
        return false;
    
       // Code Below the return statement is not executed
    }
    

    Return Documentation