如果显示消息框,则停止 c# wpf 应用程序中函数的其余执行

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

我有以下方法为用户浏览文件。

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;
        }

我想要的是当消息框被三个 if 语句之一触发时停止执行其余函数。这意味着数据 table 不会被填充,加载按钮也不会被启用。

应用程序的初始状态

消息框上的状态

当用户单击消息框上的“确定”时,我希望加载按钮仍处于禁用状态并且数据网格 table(黑框)未填充值。

在网上搜索我发现了这个 SO question,它主张创建一个 bool 函数。虽然,我不太确定如何在我的单个函数中嵌入 this 解决方案。

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.

如果您想在 c# 中停止函数的进一步执行,您可以使用 return; 退出当前函数,而不执行任何进一步的代码。

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;
}

请注意,如果您的函数 return 是布尔值之类的东西,您需要将相应的值添加到 return 语句中。在 bool 函数的情况下,要么为 false,要么为 true。

示例:

private bool ExampleBool() {
    // Halt Execution of Function and return out of it
    return false;

   // Code Below the return statement is not executed
}

Return Documentation