c# - 如果显示消息框,则停止 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 函数。虽然,我不太确定如何将此解决方案嵌入到我的单个函数中。
解决方案
我想要的是当三个 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
}
推荐阅读
- python - Django REST API 接受列表而不是发布请求中的字典
- javascript - MongooseServerSelectionError:连接 ECONNREFUSED ::1:27017 不会得到修复
- awk - NF-i 场的累积和
- typescript - 如何在打字稿中根据类类型调用方法?
- android - 如何构建应用程序来注册指纹以进行身份验证
- python - 如何在python中腌制外部仪器状态
- css - 自定义元素的命名问题
- joomla - Joomla 的 GTranslate 显示 400 错误并停止工作
- python - 当我尝试使用命令行参数清除数据库时,db.drop_all() 没有给出这样的表错误
- go - 如何从 filepath.walkDir() 中排除根目录?