Skip to content

Instantly share code, notes, and snippets.

Created April 11, 2013 19:37
Show Gist options
  • Save anonymous/5366535 to your computer and use it in GitHub Desktop.
Save anonymous/5366535 to your computer and use it in GitHub Desktop.
using System;
using System.Windows.Forms;
using System.Threading;
using System.Collections;
using System.Data.OleDb;
using System.Configuration;
using System.IO;
using System.Windows.Forms;
public partial class frmMain : Form
{
[STAThread]
public static void Main()
{
frmMain main = new frmMain();
Application.Run(main);
}
public frmMain()
{
InitializeComponent();
}
private string mUserFile;
private OleDbConnection mDB;
//ARRAYLIST HEREprivate ArrayList mEmployee = new ArrayList();
private bool validateInput(TextBox txtInput, int min, int max, out int userInput)
{
string fieldName;
fieldName = txtInput.Name.Substring(3);
userInput = 0;
if (txtInput.Text == "")
{
ShowMessage("Please enter a value for " + fieldName);
txtInput.Focus();
return false;
}
if (int.TryParse(txtInput.Text, out userInput) == false)
{
ShowMessage("Only numbers are allowed for " + fieldName + ". Please re-enter:");
txtInput.Focus();
return false;
}
if (userInput < min || userInput > max)
{
ShowMessage(fieldName + " must be between " + min.ToString() + " and " + max.ToString());
txtInput.Focus();
return false;
}
return true;
}
// The overloaded validateInput helper methods handle the existence check, type check, and range check for a given
// input form object and assigns the equivalent value to its corresponding variable. (This one handles double data.)
private bool validateInput(TextBox txtInput, double min, double max, out double userInput)
{
string fieldName;
fieldName = txtInput.Name.Substring(3);
userInput = 0D;
if (txtInput.Text == "")
{
ShowMessage("Please enter a value for " + fieldName);
txtInput.Focus();
return false;
}
if (double.TryParse(txtInput.Text, out userInput) == false)
{
ShowMessage("Only numbers are allowed for " + fieldName + ". Please re-enter:");
txtInput.Focus();
return false;
}
if (userInput < min || userInput > max)
{
ShowMessage(fieldName + " must be between " + min.ToString() + " and " + max.ToString());
txtInput.Focus();
return false;
}
return true;
}
// The overloaded validateInput helper methods handle the existence check, type check, and range check for a given
// input form object and assigns the equivalent value to its corresponding variable. (This one handles decimal data.)
private bool validateInput(TextBox txtInput, decimal min, decimal max, out decimal userInput)
{
string fieldName;
fieldName = txtInput.Name.Substring(3);
userInput = 0M;
if (txtInput.Text == "")
{
ShowMessage("Please enter a value for " + fieldName);
txtInput.Focus();
return false;
}
if (decimal.TryParse(txtInput.Text, out userInput) == false)
{
ShowMessage("Only numbers are allowed for " + fieldName + ". Please re-enter:");
txtInput.Focus();
return false;
}
if (userInput < min || userInput > max)
{
ShowMessage(fieldName + " must be between " + min.ToString() + " and " + max.ToString());
txtInput.Focus();
return false;
}
return true;
}
// The overloaded validateInput helper methods handle the existence check, type check, and range check for a given
// input form object and assigns the equivalent value to its corresponding variable. (This one handles DateTime data.)
private bool validateInput(TextBox txtInput, DateTime min, DateTime max, out DateTime userInput)
{
string fieldName;
fieldName = txtInput.Name.Substring(3);
userInput = DateTime.Parse("01/01/1900");
if (txtInput.Text == "")
{
ShowMessage("Please enter a date in the format mm/dd/yyyy for " + fieldName);
txtInput.Focus();
return false;
}
if (DateTime.TryParse(txtInput.Text, out userInput) == false)
{
ShowMessage("Only dates are allowed for " + fieldName + ". Please re-enter:");
txtInput.Focus();
return false;
}
if (userInput < min || userInput > max)
{
ShowMessage(fieldName + " must be between " + min.ToShortDateString() + " and " + max.ToShortDateString());
txtInput.Focus();
return false;
}
return true;
}
// The overloaded validateInput helper methods handle the existence check for a given Boolean input form object
// and assigns the equivalent value to its corresponding variable.
private bool validateInput(TextBox txtInput, out bool userInput)
{
string fieldName;
fieldName = txtInput.Name.Substring(3);
userInput = false;
if (txtInput.Text == "")
{
ShowMessage("Please enter a value for " + fieldName);
txtInput.Focus();
return false;
}
userInput = bool.Parse(txtInput.Text);
return true;
}
// The overloaded validateInput helper method handles the existence check for a given string input form object
// and assigns the equivalent value to its corresponding variable.
private bool validateInput(TextBox txtInput, out string userInput)
{
string fieldName;
fieldName = txtInput.Name.Substring(3);
userInput = "";
if (txtInput.Text == "")
{
ShowMessage("Please enter a value for " + fieldName);
txtInput.Focus();
return false;
}
userInput = txtInput.Text;
return true;
}
private void ShowMessage(string msg)
{
MessageBox.Show(msg, "Message from Application", MessageBoxButtons.OK);
}
private void openDatabaseConnection()
{
string connectionString = ConfigurationManager.AppSettings["DBConnectionString"] + mUserFile;
mDB = new OleDbConnection(connectionString);
}
private void closeDatabaseConnection()
{
if (mDB != null)
mDB.Close();
}
// The btnExit_Click method terminates the program when the EXIT button is clicked.
private void btnExit_Click(object sender, EventArgs e)
{
Close();
}
// The btnOK_Click method XXXX.
private void btnOK_Click(object sender, EventArgs e)
{
MessageBox.Show("Happy New Year!");
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
try
{
ofd.Title = "Select a Database file to open";
ofd.Filter = "Database Files (*.accdb)|*.accdb|All Files (*.*)|*.*";
ofd.InitialDirectory = Path.Combine(Application.StartupPath, @"\Databases");
if (ofd.ShowDialog() == DialogResult.OK)
{
mUserFile = ofd.FileName;
}
}
catch (Exception ex)
{
MessageBox.Show("There was an unexpected error: " + ex.Message);
}
//loadDBTable(//SQL Query here);
//ShowArrayInList();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment