C# custom classes for .Net Windows Form Application.
Custom Classes for Windows Form Application using C# in Object-Oriented Programming and Event-Driven approach Developed by Wilfred V. Pine @2020
Classes
"Classes
" folder and add existing item. Browse the classes that you downloaded from step 1. using Classes; // include the "Classes" folder
namespace MySampleProject
{
class Config
{
/*
* Database Information
*/
private static string host = "localhost";
private static string dbname = "dbinformation";
private static string dbuser = "root";
private static string dbpassword = "";
/*
* Classes
*/
public Database db;
public Validation validate;
public Form_UI ui;
public Visualizer visualizer;
public Str_Date_Time date_time;
public Upload upload;
/*
* Constructor
*/
public Config()
{
this.db = new Database(host, dbuser, dbpassword, dbname);
this.validate = new Validation();
this.ui = new Form_UI();
this.visualizer = new Visualizer();
this.date_time = new Str_Date_Time();
this.upload = new Upload();
}
}
}
public partial class FormLogin : Form
{
Config config = new Config(); // your Config class
public FormLogin()
{
InitializeComponent();
}
config.db.....
config.validate......
config.ui......
config.visualizer.....
config.date_time.....
config.upload.......
C:\Program Files (x86)\MySQL\MySQL Connector Net 8.0.27\Assemblies\v4.5.2\
and use the MySql.Data.dll
as a reference for your project.
Note: \v4.5.2\
is the .Net version of your project, you can use other version inside C:\Program Files (x86)\MySQL\MySQL Connector Net 8.0.27\Assemblies\
folder.C:\Program Files (x86)\MySQL\MySQL Connector Net 8.0.27\Assemblies\v4.5.2\MySql.Data.dll
)Sql Statements
select(string qry)
or select() method is use for select
statements and this method return MySqlDataReader
that reads a forward-only stream of rows from a MySQL database.var reader = config.db.select("select * from users");
while (reader.Read())
{
//reader["userid"].ToString();
}
save(string table, string[] column, string[] bind, string msg = "Successfully saved.")
or save() method is use for insert into
statements and does not return a value. This methods display a MessageBox for showing the result of your sql statement.The table
parameter is use to pass your table_name from your database, while column
parameter is use to pass your collection of column_name from your database table, and the bind
parameter is for the collection of values of the columns. Since the msg
parameter has a default value, it is optional.
string[] column = { "username", "password", "sex" };
string[] value = { txtUsername.Text, txtPassword.Text, cmbSex.Text };
config.db.save("users", column, value); // config.db.save("users", column, value, "User successfully saved!");
cud(string qry, string msg = "Transaction Completed!")
or cud() method is use for your insert into
, update
, and delete
statements. This method also does not return a value. This methods display a MessageBox for showing the result of your sql statement, or passing your custom notification message (optional) using msg
parameter.config.db.cud("Sql Query Here","Custom Notification Message");
//or
config.db.cud("Sql Query Here");
Example:
config.db.cud("INSERT INTO users (username,password,sex) VALUES ('" + txtUsername.Text + "','" + txtPassword.Text + "','" + cmbSex.Text + "')","User successfully saved!");
table(string qry, DataGridView dgv, string[] header = null)
or table() method is use for displaying data to a DataGridView Control using the DataSource properties.The qry
parameter use for your sql satement select
. The dgv
parameter is use to pass the name of your DataGridView Control, while the header
parameter (optional) is use to display the collection of custom header name for your DataGridView display.
//using custom header
string[] customheader = { "User ID", "Username", "Gender" };
//the table methods
config.db.table("select userid,username,sex from users", dgvUsers, customheader); // dgvUsers is the name of your datagridview
//the table methods without custom header
config.db.table("select userid,username,sex from users", dgvUsers);
list(string qry, ComboBox comboBox)
or list() method is use to display data to your comboBox control as an items.config.db.list("select course_name from courses", cmbCourse); // cmbCourse is the name of your comboBox control
exist(string qry)
or exist() method is use to validate if a data is existing in your database. It will return true
if exist.if(config.db.exist("select username from courses where username = '"+ txtusername.Text +"'"))
{
MessageBox.Show("Username already exist!");
}else{
// save
}
maxid(String column, String tbl)
or maxid() method is use to get the last inserted id
in your database. The column parameter is the name of the column you want to select, and the tbl parameter is your table name.int id = config.db.maxid('userid', 'users'); // it will return an int value
This class includes the traditional way of displaying form. Also includes the advanced UI manipulation using MDI or Multiple Document Interface.
Show(Form frmNew, Form frmOld)
or Show() method is simply use for displaying new Form and hiding active Form.frmain.Show();
this.Hide();
frmMain frmain = new frmMain();
config.ui.Show(frmain, this); // or config.ui.Show(new frmMain());
Dialog(Form frm)
or Dialog() methods use for displaying Form as Dialog.frmMain frmain = new frmMain();
config.ui.Dialog(frmain); // or config.ui.Show(new frmMain());
FormShow(Form frm, string dstyle = "Fill")
or FormShow() methods is use for displaying form child to your MdiParent Form.Instantiate an object dash
of a form frmDashboard
.
frmDashboard dash = new frmDashboard();
Passing object dash
to the first parameter, and "Top" (DockStyle
property value) to second parameter (Optional). Top property value is use for Dock properties of a form child. The default Dock property value is DockStyle.Fill
or Fill
.
config.ui.FormShow(dash, "Top");
// Fill
frmDashboard dash = new frmDashboard();
config.ui.FormShow(dash); // or config.ui.FormShow(new frmDashboard());
chart(Chart chart, string SeriesName, string[] x, int[] y, string chartType = "Column")
or chart() methodThe chart
parameter is the name of your Chart Control. You can add a Serries by passing to SeriesName
parameter and AddXY using x
and y
parameter. x
is the legend, name or label of every data you want to display while y
is the number value or count.
Example:
void loadChartSample()
{
/*
* SERIES 'MALE'
*/
// Array Legend
string[] X = { "BSIT", "BSED", "AB" };
// Array Value
int[] Y = { 12, 14, 9 };
// Pass to Chart Methods = Male Series
config.visualizer.chart(chartUser, "Male", X, Y, "Pie");
//_________________________________________________________________________________
/*
* NEW SERIES 'FEMALE'
*/
string[] x2 = { "BSIT", "BSED", "AB" };
int[] y2 = { 6, 0, 16 };
// Female Series
config.visualizer.chart(chartUser, "Female", x2, y2, "Pie");
}
How to add an item to array?
string[] X = { "BSIT", "BSED" };
// Add 1 item to array X
X = X.Concat(new string[] { "AB" }).ToArray();
// The new X is { "BSIT", "BSED", "AB" }
int[] Y = { 12, 14 };
// Add 1 item to array Y
Y = Y.Concat(new int[] { 2 }).ToArray();
// Y = 12, 14, 2
/*
* SERIES 'MALE'
*/
//initialized empty array
string[] X = {};
int[] Y = {};
var reader = db.select("select count(*) as c, course from users where sex = 'MALE'");
while (reader.Read())
{
X = X.Concat(new string[] { reader["course"].ToString() }).ToArray(); // string[] X = { "BSIT", "BSED", "AB" };
Y = Y.Concat(new int[] { Int32.Parse(reader["c"].ToString()) }).ToArray(); // int[] Y = { 12, 14, 9 };
}
// Chart Methods
// ChartSeries = "MALE"; ChartType = "Bar"; // Chart type is optional, the default is column
config.visualizer.chart(chartUser, "MALE", X, Y, "Bar");
/*
* SERIES 'FEMALE'
*/
//initialized empty array
string[] x2 = {};
int[] y2 = {};
var reader = db.select("select count(*) as c, course from users where sex = 'FEMALE'");
while (reader.Read())
{
x2 = x2.Concat(new string[] { reader["course"].ToString() }).ToArray(); // string[] x2 = { "BSIT", "BSED", "AB" };
y2 = y2.Concat(new int[] { Int32.Parse(reader["c"].ToString()) }).ToArray(); // int[] y2 = { 6, 0, 16 };
}
// Chart Methods
// ChartSeries = "FEMALE"; ChartType = "Bar"; // Chart type is optional, the default is column
config.visualizer.chart(chartUser, "FEMALE", x2, y2, "Bar");
txtRequired(TextBox[] txt, bool allow_message = false, string msg = "Please fillup required fields!")
or txtRequired() method use to validate required textbox controls. There is also validation for comboBox controls, the cmbRequired(ComboBox[] cmb, bool allow_message = false, string msg = "Please select required fields!")
method. These functions return false
if ther is an empty value in the fields.TextBox[] txt = { txtUsername, txtPassword, txtCPassword };
ComboBox[] cmb = { cmbSex };
if (!config.validate.txtRequired(txt) || !config.validate.cmbRequired(cmb))
return;
alpha(KeyPressEventArgs e)
or alpha() Method is use to disable a series of keyboard characters except letters. To use this method, set a KeyPress events to your texbox and call the alphanum(e)
methods to it's event handler.private void txtUsername_KeyPress(object sender, KeyPressEventArgs e)
{
config.validate.alphanum(e);
}