Reputation: 643
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. 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;
}
}
}
}
Upvotes: 0
Views: 2459
Reputation: 1857
You could recursively call the NewProject method again.
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 :)
Upvotes: 1