#region GNU notice
// GridProcessor - Search and replace abilities for the .NET DataGridView
// Copyright (C) 2007, by Evan Stein
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion GNU notice
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace GridProcessor
{
/// <summary>
/// Saves an upper/lower casing expression to the database
/// </summary>
public partial class FormSaveCaseExpression : Form
{
private Replacement mReplacement;
private int mReplacementTypeID;
private Settings mSettings;
/// <summary>
/// Saves an upper/lower casing expression to the database
/// </summary>
/// <param name="replacement">A replacement object with casing information</param>
/// <param name="settings">Database connection informatin</param>
public FormSaveCaseExpression(Replacement replacement, Settings settings)
{
InitializeComponent();
mSettings = settings;
// Make sure we're not operating on the original
mReplacement = replacement.Clone();
}
private void FormSaveReplacementExpression_Load(object sender, EventArgs e)
{
if (!SetAdapter())
this.Close();
try
{
oleDbDataAdapter1.Fill(dataSetReplacement1, "GridProcess");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Load error", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.DialogResult = DialogResult.Cancel;
this.Hide();
return;
}
mReplacementTypeID = Util.GetGridProcessTypeID("Casing", mSettings);
bindingSource1.Filter = "GridProcessTypeID=" + mReplacementTypeID.ToString();
comboBox_name.Text = mReplacement.Name;
textBox_description.Text = mReplacement.Description;
textBox_searchExpr.Text = mReplacement.SearchExpression;
if (mReplacement.ToUpper)
radioButton_upper.Checked = true;
else
radioButton_lower.Checked = true;
checkBox_caseSensitive.Checked = mReplacement.CaseSensitive;
}
private bool SetAdapter()
{
string connectString;
connectString = mSettings.ConnectionString;
this.oleDbConnection1.ConnectionString = connectString;
return true;
}
private void button_save_Click(object sender, EventArgs e)
{
DataTable dt;
string name = string.Empty, searchName;
int rowIndex;
DialogResult result;
DataRow dr;
if (comboBox_name.Text == string.Empty)
{
MessageBox.Show("Case expression must have a name", "Save",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
if (textBox_description.Text == string.Empty)
{
MessageBox.Show("Case Expression must have some sort of description", "Save",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
if (textBox_searchExpr.Text == string.Empty)
{
MessageBox.Show("Case expression must have a search expression", "Save",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
mReplacement.Name = comboBox_name.Text;
mReplacement.Description = textBox_description.Text;
mReplacement.SearchExpression = textBox_searchExpr.Text;
if (radioButton_upper.Checked)
mReplacement.ToUpper = true;
else
mReplacement.ToUpper = false;
dt = dataSetReplacement1.Tables["GridProcess"];
searchName = mReplacement.Name.ToUpper().Trim();
rowIndex = -1;
for (int i = 0; i < dt.Rows.Count; i++)
{
name = dt.Rows[i]["Name"].ToString().ToUpper().Trim();
if (name == searchName)
{
rowIndex = i;
name = dt.Rows[i]["Name"].ToString();
break;
}
}
if (rowIndex >= 0)
{
result = MessageBox.Show("Replace existing item [" + name + "] ?", "Replace",
MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (result != DialogResult.Yes)
return;
else
dr = dt.Rows[rowIndex];
}
else
{
dr = dt.NewRow();
dt.Rows.Add(dr);
mReplacement.ID = -1;
}
dr["GridProcessID"] = mReplacement.ID;
dr["GridProcessTypeID"] = mReplacementTypeID;
dr["Name"] = mReplacement.Name;
dr["Description"] = mReplacement.Description;
dr["SearchExpression"] = mReplacement.SearchExpression;
dr["CaseSensitive"] = mReplacement.CaseSensitive;
dr["ToUpper"] = mReplacement.ToUpper;
try
{
oleDbDataAdapter1.Update(dt);
}
catch (Exception ex)
{
MessageBox.Show("Error during save: " + ex.Message, "Save", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
this.DialogResult = DialogResult.OK;
this.Hide();
}
/// <summary>
/// The case expression being saved
/// </summary>
public Replacement Replacement
{
get { return mReplacement; }
}
private void button_cancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Hide();
}
}
}