首页 > 解决方案 > 如果显示消息框,则停止 c# wpf 应用程序中函数的其余执行

问题描述

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

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 语句之一触发消息框以停止函数其余部分的执行时。这意味着数据表不会被填充,加载按钮也不会被启用。

应用程序的初始状态

在此处输入图像描述

消息框上的状态

在此处输入图像描述

当用户在消息框上单击确定时,我希望加载按钮仍然被禁用并且数据网格表(黑框)没有填充值。

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

标签: c#wpfmessagebox

解决方案


我想要的是当三个 if 语句之一触发消息框以停止函数其余部分的执行时。

如果要在 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;
}

请注意,如果您的函数返回一些东西,例如 bool,您需要将相应的值添加到 return 语句中。在布尔函数的情况下,eiter false 或 true。

例子:

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

   // Code Below the return statement is not executed
}

返回文件


推荐阅读