[英]Save file dialog confirmation
i got struck at making a save file dialog box. 我对制作一个保存文件对话框感到震惊。 I got everything done, but I want to show a dialog box when I try to save file which is already existing, with options to override,cancel or no.
我已完成所有工作,但是当我尝试保存已存在的文件时,想要显示一个对话框,并带有覆盖,取消或否的选项。 When the user click no, I want the saveFodlerDialog to show up again and iterate the process.
当用户单击否时,我希望saveFodlerDialog再次出现并重复该过程。 But I dont know how to implement it.
但是我不知道如何实现它。
I am pasting relevant code below: 我在下面粘贴相关代码:
private void New_Project(object sender, RoutedEventArgs e)
{
var saveFolderDlg = new System.Windows.Forms.FolderBrowserDialog();
System.Windows.Forms.DialogResult dlgResult = saveFolderDlg.ShowDialog();
if (dlgResult == System.Windows.Forms.DialogResult.OK)
{
saveFolderDlg.RootFolder = Environment.SpecialFolder.Desktop;
saveFolderDlg.ShowNewFolderButton = true;
string projectPath = saveFolderDlg.SelectedPath;
string prjFileName = System.IO.Path.GetFileName(projectPath);
string newPath = System.IO.Path.Combine(projectPath, prjFileName);
if (!System.IO.File.Exists(newPath+".rnd"))
{
CreateNewProejct(projectPath);//works fine
}
else
{
string msgBoxTxt = "Project already exists, Override?";
MessageBoxButton button = MessageBoxButton.YesNoCancel;
string caption = "New porject";
MessageBoxImage icon = MessageBoxImage.Warning;
MessageBoxResult result = MessageBox.Show(msgBoxTxt,caption, button, icon);
switch (result)
{
case MessageBoxResult.No:
//what to do here to restart the process of saving project
break;
case MessageBoxResult.Cancel:
break;
case MessageBoxResult.Yes:
CreateNewProejct(projectPath);
break;
}
}
}
}
You could recursively call the NewProject method again. 您可以递归地再次调用NewProject方法。
private void NewProject()
{
var saveFolderDlg = new FolderBrowserDialog();
DialogResult dlgResult = saveFolderDlg.ShowDialog();
if (dlgResult == System.Windows.Forms.DialogResult.OK)
{
saveFolderDlg.RootFolder = Environment.SpecialFolder.Desktop;
saveFolderDlg.ShowNewFolderButton = true;
string projectPath = saveFolderDlg.SelectedPath;
string prjFileName = System.IO.Path.GetFileName(projectPath);
string newPath = System.IO.Path.Combine(projectPath, prjFileName);
if (!System.IO.File.Exists(newPath + ".rnd"))
{
CreateNewProejct(projectPath);//works fine
}
else
{
string msgBoxTxt = "Project already exists, Override?";
MessageBoxButton button = MessageBoxButton.YesNoCancel;
string caption = "New porject";
MessageBoxImage icon = MessageBoxImage.Warning;
MessageBoxResult result = MessageBox.Show(msgBoxTxt, caption, button, icon);
switch (result)
{
case MessageBoxResult.No:
NewProject();
break;
case MessageBoxResult.Cancel:
break;
case MessageBoxResult.Yes:
CreateNewProejct(projectPath);
break;
}
}
}
}
Btw: there are some typos in your code --> Proejct :) 顺便说一句:您的代码中有一些错别字-> Proejct :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.