PDF-Merge/ConfForm.cs
efrick 3b57fb0edf The date format is now selectable.
Added the form controls and AppSettings keys to allow the user to select
the format of the date appended to the output file.
Further update to issue #1.
2024-08-06 10:37:42 -04:00

154 lines
5.1 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PDF_Merge
{
public partial class ConfForm : Form
{
public ConfForm()
{
InitializeComponent();
string sourcePath = ConfigurationManager.AppSettings["PDF-Path"];
string outputPath = ConfigurationManager.AppSettings["PDF-Output"];
string outputName = ConfigurationManager.AppSettings["PDF-Name"];
string outputExt = ConfigurationManager.AppSettings["PDF-Extension"];
string dateIndex = ConfigurationManager.AppSettings["dateIndex"];
int dateIndexValue;
bool indexSet = int.TryParse(dateIndex, out dateIndexValue);
if (indexSet)
{
dateFormatOpts.SelectedIndex = dateIndexValue;
} else
{
dateFormatOpts.SelectedIndex = 0;
}
sourceBox.Text = sourcePath;
outputBox.Text = outputPath;
FileNameBox.Text = outputName;
fileExtlabel.Text = outputExt;
if (ConfigurationManager.AppSettings["overwrite"] == true.ToString())
{
overrideCBox.Checked = true;
}
else
{
overrideCBox.Checked = false;
}
if (ConfigurationManager.AppSettings["appendDate"] == true.ToString())
{
appendDatecheckBox.Checked = true;
}
else
{
appendDatecheckBox.Checked = false;
}
CheckAppend();
}
private void CancelBtn_Click(object sender, EventArgs e)
{
this.Close();
}
private void SaveBtn_Click(object sender, EventArgs e)
{
Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection appSettings = appConfig.AppSettings;
if (sourceBox.Text.Length > 0)
{
appSettings.Settings["PDF-Path"].Value = sourceBox.Text;
}
else
{
MessageBox.Show("Source path cannot be empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (outputBox.Text.Length > 0)
{
appSettings.Settings["PDF-Output"].Value = outputBox.Text;
}
else
{
MessageBox.Show("Output path cannot be empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (FileNameBox.Text.Length > 0)
{
appSettings.Settings["PDF-Name"].Value = FileNameBox.Text;
}
else
{
MessageBox.Show("File must be named.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
appSettings.Settings["overwrite"].Value = overrideCBox.Checked.ToString();
appSettings.Settings["appendDate"].Value = appendDatecheckBox.Checked.ToString();
appSettings.Settings["dateFormat"].Value = dateFormatOpts.SelectedItem.ToString();
appSettings.Settings["dateIndex"].Value = dateFormatOpts.SelectedIndex.ToString();
appConfig.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
this.Close();
}
private void SourceDirBtn_Click(object sender, EventArgs e)
{
using (var SourceDirPicker = new FolderBrowserDialog())
{
if (sourceBox.Text == "")
{
SourceDirPicker.RootFolder = Environment.SpecialFolder.MyDocuments;
}
else
{
string startingDir = sourceBox.Text;
SourceDirPicker.SelectedPath = startingDir;
}
if (SourceDirPicker.ShowDialog() == DialogResult.OK)
{
var sourceDir = SourceDirPicker.SelectedPath;
sourceBox.Text = sourceDir;
}
}
}
private void OutPathBtn_Click(object sender, EventArgs e)
{
using (var OutPathPicker = new FolderBrowserDialog())
{
OutPathPicker.RootFolder = Environment.SpecialFolder.Desktop;
if (OutPathPicker.ShowDialog() == DialogResult.OK)
{
var OutPath = OutPathPicker.SelectedPath;
outputBox.Text = OutPath;
}
}
}
private void AppendDatecheckBox_CheckedChanged(object sender, EventArgs e)
{
CheckAppend();
}
private void CheckAppend()
{
if (appendDatecheckBox.Checked)
{
dateFormatOpts.Visible = true;
}
else
{
dateFormatOpts.Visible = false;
}
}
}
}