Save/Open file dialog init

Initialize outside of object initializer, solves warnings if any property sets throw exceptions
This commit is contained in:
Kurt
2023-10-14 19:26:56 -07:00
parent 0da8c33c52
commit 738c51d596
15 changed files with 88 additions and 102 deletions

View File

@@ -32,11 +32,9 @@ protected override void OnDrawItem(DrawItemEventArgs e)
e.DrawBackground();
}
using var flags = new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center,
};
using var flags = new StringFormat();
flags.Alignment = StringAlignment.Center;
flags.LineAlignment = StringAlignment.Center;
using var text = new SolidBrush(ForeColor);
var tab = TabPages[index];
graphics.DrawString(tab.Text, Font, text, bounds, flags);
@@ -90,11 +88,9 @@ protected override void OnDrawItem(DrawItemEventArgs e)
e.DrawBackground();
}
using var flags = new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center,
};
using var flags = new StringFormat();
flags.Alignment = StringAlignment.Center;
flags.LineAlignment = StringAlignment.Center;
using var text = new SolidBrush(ForeColor);
var tab = TabPages[index];
graphics.DrawString(tab.Text, Font, text, bounds, flags);

View File

@@ -225,7 +225,9 @@ public bool SaveBoxBinary()
if (dr == DialogResult.Yes)
{
using var sfd = new SaveFileDialog { Filter = "Box Data|*.bin", FileName = "pcdata.bin" };
using var sfd = new SaveFileDialog();
sfd.Filter = "Box Data|*.bin";
sfd.FileName = "pcdata.bin";
if (sfd.ShowDialog() != DialogResult.OK)
return false;
File.WriteAllBytes(sfd.FileName, SAV.GetPCBinary());
@@ -233,7 +235,9 @@ public bool SaveBoxBinary()
}
if (dr == DialogResult.No)
{
using var sfd = new SaveFileDialog { Filter = "Box Data|*.bin", FileName = $"boxdata {CurrentBoxName}.bin" };
using var sfd = new SaveFileDialog();
sfd.Filter = "Box Data|*.bin";
sfd.FileName = $"boxdata {CurrentBoxName}.bin";
if (sfd.ShowDialog() != DialogResult.OK)
return false;
File.WriteAllBytes(sfd.FileName, SAV.GetBoxBinary(CurrentBox));

View File

@@ -724,7 +724,9 @@ private void B_JPEG_Click(object sender, EventArgs e)
return;
}
string filename = $"{s6.JPEGTitle}'s picture";
using var sfd = new SaveFileDialog { FileName = filename, Filter = "JPEG|*.jpeg" };
using var sfd = new SaveFileDialog();
sfd.FileName = filename;
sfd.Filter = "JPEG|*.jpeg";
if (sfd.ShowDialog() != DialogResult.OK)
return;
File.WriteAllBytes(sfd.FileName, jpeg);
@@ -818,7 +820,8 @@ public bool ExportBackup()
}
var suggestion = Util.CleanFileName(SAV.Metadata.BAKName);
using var sfd = new SaveFileDialog { FileName = suggestion };
using var sfd = new SaveFileDialog();
sfd.FileName = suggestion;
if (sfd.ShowDialog() != DialogResult.OK)
return false;

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Text;
using System.Windows.Forms;
@@ -9,12 +9,10 @@ public sealed partial class ErrorWindow : Form
public static DialogResult ShowErrorDialog(string friendlyMessage, Exception ex, bool allowContinue)
{
var lang = System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName;
using var dialog = new ErrorWindow(lang)
{
ShowContinue = allowContinue,
Message = friendlyMessage,
Error = ex,
};
using var dialog = new ErrorWindow(lang);
dialog.ShowContinue = allowContinue;
dialog.Message = friendlyMessage;
dialog.Error = ex;
var dialogResult = dialog.ShowDialog();
if (dialogResult == DialogResult.Abort)
Environment.Exit(1);

View File

@@ -204,9 +204,7 @@ private void AddRibbonNumericUpDown(RibbonInfo rib, int row, Control label)
nud.ValueChanged += (sender, e) =>
{
var controlName = PrefixPB + rib.Name;
var pb = FLP_Ribbons.Controls[controlName];
if (pb is null)
throw new ArgumentException($"{controlName} not found in {FLP_Ribbons.Name}.");
var pb = FLP_Ribbons.Controls[controlName] ?? throw new ArgumentException($"{controlName} not found in {FLP_Ribbons.Name}.");
pb.Visible = (rib.RibbonCount = (byte)nud.Value) != 0;
pb.BackgroundImage = RibbonSpriteUtil.GetRibbonSprite(rib.Name, (int)nud.Maximum, (int)nud.Value);

View File

@@ -101,11 +101,9 @@ private void PromptSaveCSV(object sender, FormClosingEventArgs e)
{
if (WinFormsUtil.Prompt(MessageBoxButtons.YesNo, MsgReportExportCSV) != DialogResult.Yes)
return;
using var savecsv = new SaveFileDialog
{
Filter = "Spreadsheet|*.csv",
FileName = "Box Data Dump.csv",
};
using var savecsv = new SaveFileDialog();
savecsv.Filter = "Spreadsheet|*.csv";
savecsv.FileName = "Box Data Dump.csv";
if (savecsv.ShowDialog() == DialogResult.OK)
{
Hide();

View File

@@ -28,12 +28,9 @@ public SAV_CGearSkin(SaveFile sav)
private void B_ImportPNG_Click(object sender, EventArgs e)
{
using var ofd = new OpenFileDialog
{
Filter = "PNG File|*.png",
FileName = "Background.png",
};
using var ofd = new OpenFileDialog();
ofd.Filter = "PNG File|*.png";
ofd.FileName = "Background.png";
if (ofd.ShowDialog() != DialogResult.OK)
return;
@@ -51,25 +48,19 @@ private void B_ImportPNG_Click(object sender, EventArgs e)
private void B_ExportPNG_Click(object sender, EventArgs e)
{
Image png = PB_Background.Image;
using var sfd = new SaveFileDialog
{
Filter = "PNG File|*.png",
FileName = "Background.png",
};
using var sfd = new SaveFileDialog();
sfd.Filter = "PNG File|*.png";
sfd.FileName = "Background.png";
if (sfd.ShowDialog() != DialogResult.OK)
return;
png.Save(sfd.FileName, ImageFormat.Png);
PB_Background.Image.Save(sfd.FileName, ImageFormat.Png);
}
private void B_ImportCGB_Click(object sender, EventArgs e)
{
using var ofd = new OpenFileDialog
{
Filter = CGearBackground.Filter + "|PokeStock C-Gear Skin|*.psk",
};
using var ofd = new OpenFileDialog();
ofd.Filter = CGearBackground.Filter + "|PokeStock C-Gear Skin|*.psk";
if (ofd.ShowDialog() != DialogResult.OK)
return;
@@ -89,10 +80,8 @@ private void B_ImportCGB_Click(object sender, EventArgs e)
private void B_ExportCGB_Click(object sender, EventArgs e)
{
using var sfd = new SaveFileDialog
{
Filter = CGearBackground.Filter,
};
using var sfd = new SaveFileDialog();
sfd.Filter = CGearBackground.Filter;
if (sfd.ShowDialog() != DialogResult.OK)
return;

View File

@@ -769,7 +769,9 @@ private void B_UnlockAllMusicalProps_Click(object sender, EventArgs e)
private void B_DumpFC_Click(object sender, EventArgs e)
{
using var sfd = new SaveFileDialog { Filter = ForestCityBinFilter, FileName = string.Format(ForestCityBinPath, SAV.Version) };
using var sfd = new SaveFileDialog();
sfd.Filter = ForestCityBinFilter;
sfd.FileName = string.Format(ForestCityBinPath, SAV.Version);
if (sfd.ShowDialog() != DialogResult.OK)
return;
@@ -779,7 +781,9 @@ private void B_DumpFC_Click(object sender, EventArgs e)
private void B_ImportFC_Click(object sender, EventArgs e)
{
using var ofd = new OpenFileDialog { Filter = ForestCityBinFilter, FileName = string.Format(ForestCityBinPath, SAV.Version) };
using var ofd = new OpenFileDialog();
ofd.Filter = ForestCityBinFilter;
ofd.FileName = string.Format(ForestCityBinPath, SAV.Version);
if (ofd.ShowDialog() != DialogResult.OK)
return;

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
@@ -42,7 +42,8 @@ private void B_Cancel_Click(object sender, EventArgs e)
private void B_Import_Click(object sender, EventArgs e)
{
using var ofd = new OpenFileDialog { Filter = PL6.Filter };
using var ofd = new OpenFileDialog();
ofd.Filter = PL6.Filter;
if (ofd.ShowDialog() != DialogResult.OK)
return;
@@ -58,7 +59,8 @@ private void B_Import_Click(object sender, EventArgs e)
private void B_Export_Click(object sender, EventArgs e)
{
using var sfd = new SaveFileDialog { Filter = PL6.Filter };
using var sfd = new SaveFileDialog();
sfd.Filter = PL6.Filter;
if (sfd.ShowDialog() != DialogResult.OK)
return;

View File

@@ -418,7 +418,9 @@ private void B_Export_Click(object sender, EventArgs e)
var tr = sb.TrainerName;
if (string.IsNullOrWhiteSpace(tr))
tr = "Trainer";
using var sfd = new SaveFileDialog { Filter = "Secret Base Data|*.sb6", FileName = $"{sb.BaseLocation:D2} - {Util.CleanFileName(tr)}.sb6" };
using var sfd = new SaveFileDialog();
sfd.Filter = "Secret Base Data|*.sb6";
sfd.FileName = $"{sb.BaseLocation:D2} - {Util.CleanFileName(tr)}.sb6";
if (sfd.ShowDialog() != DialogResult.OK)
return;

View File

@@ -165,12 +165,10 @@ private void B_ExportGoFiles_Click(object sender, EventArgs e)
private void B_Import_Click(object sender, EventArgs e)
{
using var sfd = new OpenFileDialog
{
Filter = GoFilter,
FilterIndex = 0,
RestoreDirectory = true,
};
using var sfd = new OpenFileDialog();
sfd.Filter = GoFilter;
sfd.FilterIndex = 0;
sfd.RestoreDirectory = true;
// Export
if (sfd.ShowDialog() != DialogResult.OK)
@@ -211,13 +209,11 @@ private void B_Export_Click(object sender, EventArgs e)
index = Math.Clamp(index, 0, max);
var data = Park[index];
using var sfd = new SaveFileDialog
{
FileName = data.FileName,
Filter = GoFilter,
FilterIndex = 0,
RestoreDirectory = true,
};
using var sfd = new SaveFileDialog();
sfd.FileName = data.FileName;
sfd.Filter = GoFilter;
sfd.FilterIndex = 0;
sfd.RestoreDirectory = true;
if (sfd.ShowDialog() != DialogResult.OK)
return;

View File

@@ -164,7 +164,8 @@ private void B_ImportFolder_Click(object sender, EventArgs e)
private void B_ExportAllSingle_Click(object sender, EventArgs e)
{
using var sfd = new SaveFileDialog { FileName = "raw.bin" };
using var sfd = new SaveFileDialog();
sfd.FileName = "raw.bin";
if (sfd.ShowDialog() != DialogResult.OK)
return;
@@ -186,7 +187,8 @@ private void B_ExportAllSingle_Click(object sender, EventArgs e)
private void B_LoadOld_Click(object sender, EventArgs e)
{
using var ofd = new OpenFileDialog { FileName = "main" };
using var ofd = new OpenFileDialog();
ofd.FileName = "main";
if (ofd.ShowDialog() != DialogResult.OK)
return;
TB_OldSAV.Text = ofd.FileName;
@@ -196,7 +198,8 @@ private void B_LoadOld_Click(object sender, EventArgs e)
private void B_LoadNew_Click(object sender, EventArgs e)
{
using var ofd = new OpenFileDialog { FileName = "main" };
using var ofd = new OpenFileDialog();
ofd.FileName = "main";
if (ofd.ShowDialog() != DialogResult.OK)
return;
TB_NewSAV.Text = ofd.FileName;
@@ -232,7 +235,8 @@ private void CompareSaves()
private static void ExportSelectBlock(SCBlock block)
{
var name = GetBlockFileNameWithoutExtension(block);
using var sfd = new SaveFileDialog { FileName = $"{name}.bin" };
using var sfd = new SaveFileDialog();
sfd.FileName = $"{name}.bin";
if (sfd.ShowDialog() != DialogResult.OK)
return;
File.WriteAllBytes(sfd.FileName, block.Data);
@@ -242,7 +246,8 @@ private static void ImportSelectBlock(SCBlock blockTarget)
{
var key = blockTarget.Key;
var data = blockTarget.Data;
using var ofd = new OpenFileDialog { FileName = $"{key:X8}.bin" };
using var ofd = new OpenFileDialog();
ofd.FileName = $"{key:X8}.bin";
if (ofd.ShowDialog() != DialogResult.OK)
return;

View File

@@ -142,7 +142,8 @@ private void SetCardID(int cardID)
private void B_Import_Click(object sender, EventArgs e)
{
var fileFilter = WinFormsUtil.GetMysterGiftFilter(SAV.Context);
using var import = new OpenFileDialog { Filter = fileFilter };
using var import = new OpenFileDialog();
import.Filter = fileFilter;
if (import.ShowDialog() != DialogResult.OK)
return;

View File

@@ -166,9 +166,7 @@ private static string GetResourcePath(params string[] subdir)
var path = Application.StartupPath;
while (true)
{
var parent = Directory.GetParent(path);
if (parent is null)
throw new DirectoryNotFoundException();
var parent = Directory.GetParent(path) ?? throw new DirectoryNotFoundException(path);
path = parent.FullName;
if (path.EndsWith(repo))
return Path.Combine(path, Path.Combine(subdir));

View File

@@ -256,15 +256,13 @@ public static void AddSaveFileExtensions(IEnumerable<string> exts)
public static bool OpenSAVPKMDialog(IEnumerable<string> extensions, out string? path)
{
string supported = string.Join(";", extensions.Select(s => $"*.{s}").Concat(new[] { "*.pk" }));
using var ofd = new OpenFileDialog
{
Filter = "All Files|*.*" +
using var ofd = new OpenFileDialog();
ofd.Filter = "All Files|*.*" +
$"|Supported Files (*.*)|main;*.bin;{supported};*.bak" + ExtraSaveExtensions +
"|Save Files (*.sav)|main" + ExtraSaveExtensions +
"|Decrypted PKM File (*.pk)|" + supported +
"|Binary File|*.bin" +
"|Backup File|*.bak",
};
"|Backup File|*.bak";
// Detect main
SaveFile? sav = null;
@@ -306,12 +304,10 @@ public static bool SavePKMDialog(PKM pk)
(allowEncrypted ? $"|Encrypted PKM File|*.e{pkx[1..]}" : string.Empty) +
"|Binary File|*.bin" +
"|All Files|*.*";
using var sfd = new SaveFileDialog
{
Filter = genericFilter,
DefaultExt = pkx,
FileName = Util.CleanFileName(pk.FileName),
};
using var sfd = new SaveFileDialog();
sfd.Filter = genericFilter;
sfd.DefaultExt = pkx;
sfd.FileName = Util.CleanFileName(pk.FileName);
if (sfd.ShowDialog() != DialogResult.OK)
return false;
@@ -346,13 +342,11 @@ private static void SaveBackup(string path)
/// <returns>Result of whether or not the file was saved.</returns>
public static bool ExportSAVDialog(SaveFile sav, int currentBox = 0)
{
using var sfd = new SaveFileDialog
{
Filter = sav.Metadata.Filter,
FileName = sav.Metadata.FileName,
FilterIndex = 1000, // default to last, All Files
RestoreDirectory = true,
};
using var sfd = new SaveFileDialog();
sfd.Filter = sav.Metadata.Filter;
sfd.FileName = sav.Metadata.FileName;
sfd.FilterIndex = 1000; // default to last, All Files
sfd.RestoreDirectory = true;
if (Directory.Exists(sav.Metadata.FileFolder))
sfd.InitialDirectory = sav.Metadata.FileFolder;
@@ -403,11 +397,9 @@ private static void ExportSAV(SaveFile sav, string path)
/// <returns>Result of whether or not the file was saved.</returns>
public static bool ExportMGDialog(DataMysteryGift gift)
{
using var sfd = new SaveFileDialog
{
Filter = GetMysterGiftFilter(gift.Context),
FileName = Util.CleanFileName(gift.FileName),
};
using var sfd = new SaveFileDialog();
sfd.Filter = GetMysterGiftFilter(gift.Context);
sfd.FileName = Util.CleanFileName(gift.FileName);
if (sfd.ShowDialog() != DialogResult.OK)
return false;