tbf/TestBenchFramework/BenchesDlg.cs
2014-07-28 09:56:40 +02:00

199 lines
7.1 KiB
C#

using System;
using System.Windows.Forms;
namespace TBF
{
/// <summary>
/// Dialog to edit local settings that specify database parameters (S1.2)
/// </summary>
public partial class BenchesDlg : Form
{
// Local copy of 'local settings' initialized to Program.localSettings.
// Dialog modifies this copy and updates Program.localSettings just
// when 'OK' is pressed.
LocalSettings localSettings;
/// <summary>
/// Constructor, initializes the dialog
/// </summary>
public BenchesDlg()
{
localSettings = new LocalSettings();
localSettings.Assign(Program.LocalSettings);
InitializeComponent();
RedrawTestBenchesListBox();
}
void RedrawTestBenchesListBox()
{
testBenchesListBox.Items.Clear();
if (localSettings.testBenches != null)
{
int nrBenches = localSettings.testBenches.GetLength(0);
for (int i = 0; i < nrBenches; i++)
{
testBenchesListBox.Items.Add(localSettings.testBenches[i].BenchName);
}
}
}
/// <summary>
/// Create a new test bench DB specification and append it to the list
/// </summary>
/// <param name="sender">Not used</param>
/// <param name="e">Not used</param>
private void addButton_Click(object sender, EventArgs e)
{
DatabaseSettings bench = new DatabaseSettings();
DatabaseSettingsDlg dlg = new DatabaseSettingsDlg(bench);
DialogResult dr = dlg.ShowDialog();
if (dr == DialogResult.OK)
{
int nrBenches =
(localSettings.testBenches == null) ? 0 : localSettings.testBenches.GetLength(0);
if (nrBenches == 0)
{
localSettings.testBenches = new DatabaseSettings[1];
localSettings.testBenches[0] = bench;
}
else
{
DatabaseSettings[] newTestBenches = new DatabaseSettings[nrBenches + 1];
for (int i = 0; i < nrBenches; i++)
{
newTestBenches[i] = localSettings.testBenches[i];
}
newTestBenches[nrBenches] = bench;
localSettings.testBenches = newTestBenches;
}
testBenchesListBox.Items.Add(bench.BenchName);
}
}
/// <summary>
/// Create a new test bench DB specification,which is a copy of the
/// selected bench, and append it to the list
/// </summary>
/// <param name="sender">Not used</param>
/// <param name="e">Not used</param>
private void copyButton_Click(object sender, EventArgs e)
{
if (testBenchesListBox.SelectedIndex >= 0)
{
DatabaseSettings ori = localSettings.testBenches[testBenchesListBox.SelectedIndex];
DatabaseSettings bench = (DatabaseSettings)ori.Clone();
DatabaseSettingsDlg dlg = new DatabaseSettingsDlg(bench);
DialogResult dr = dlg.ShowDialog();
if (dr == DialogResult.OK)
{
int nrBenches =
(localSettings.testBenches == null) ? 0 : localSettings.testBenches.GetLength(0);
DatabaseSettings[] newTestBenches = new DatabaseSettings[nrBenches + 1];
for (int i = 0; i < nrBenches; i++)
{
newTestBenches[i] = localSettings.testBenches[i];
}
newTestBenches[nrBenches] = bench;
localSettings.testBenches = newTestBenches;
testBenchesListBox.Items.Add(bench.BenchName);
}
}
}
/// <summary>
/// Edit selected test bench DB specification
/// </summary>
/// <param name="sender">Not used</param>
/// <param name="e">Not used</param>
private void editButton_Click(object sender, EventArgs e)
{
if (testBenchesListBox.SelectedIndex >= 0)
{
DatabaseSettingsDlg dlg = new DatabaseSettingsDlg(localSettings.testBenches[testBenchesListBox.SelectedIndex]);
DialogResult dr = dlg.ShowDialog();
if (dr == DialogResult.OK)
{
// Update the item name
testBenchesListBox.Items[testBenchesListBox.SelectedIndex] =
localSettings.testBenches[testBenchesListBox.SelectedIndex].BenchName;
}
}
}
/// <summary>
/// Remove selected test bench DB specification from the list
/// </summary>
/// <param name="sender">Not used</param>
/// <param name="e">Not used</param>
private void removeButton_Click(object sender, EventArgs e)
{
int selectedIx = testBenchesListBox.SelectedIndex;
if (selectedIx >= 0)
{
int nrBenches =
(localSettings.testBenches == null) ? 0 : localSettings.testBenches.GetLength(0);
if (nrBenches == 1)
{
localSettings.testBenches = null;
}
else
{
DatabaseSettings[] newTestBenches = new DatabaseSettings[nrBenches - 1];
for (int i = 0; i < selectedIx; i++)
{
newTestBenches[i] = localSettings.testBenches[i];
}
for (int i = selectedIx + 1; i < nrBenches; i++)
{
newTestBenches[i - 1] = localSettings.testBenches[i];
}
localSettings.testBenches = newTestBenches;
}
RedrawTestBenchesListBox();
}
}
/// <summary>
/// Update setting, return 'OK'
/// </summary>
/// <param name="sender">Not used</param>
/// <param name="e">Not used</param>
private void okButton_Click(object sender, EventArgs e)
{
Program.LocalSettings.Assign(localSettings); // Update Program.localSettings
DialogResult = DialogResult.OK;
Close();
return;
}
/// <summary>
/// Throw away changes, return 'Cancel'
/// </summary>
/// <param name="sender">Not used</param>
/// <param name="e">Not used</param>
private void cancelButton_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
return;
}
/// <summary>
/// Double click on an item = edit item
/// </summary>
/// <param name="sender">Not used</param>
/// <param name="e">Not used</param>
private void testBenchesListBox_DoubleClick(object sender, EventArgs e)
{
editButton_Click(sender, e);
}
}
}