Skip to content

Commit 6c5755d

Browse files
committed
updated docs and class
1 parent c7596ea commit 6c5755d

File tree

2 files changed

+149
-87
lines changed

2 files changed

+149
-87
lines changed

README.md

Lines changed: 149 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,75 @@
11
# CSharf Custom Classes
22

3-
Custom classes for Windows Form Application in C# Developed by Wilfred V. Pine
3+
Custom [Classes](https://github.com/redmalmon/csharp-custom-classes/tree/main/Classes) for Windows Form Application using C# Developed by Wilfred V. Pine
44
@2020
55

66
[MIT License](https://github.com/redmalmon/CSharf-Custom-Classes/blob/main/LICENSE)
77

88
## [Classes](https://github.com/redmalmon/csharp-custom-classes/tree/main/Classes)
99

10-
### 1. [Database](https://github.com/redmalmon/csharp-custom-classes/blob/main/Classes/Database.cs) class - this is use for MySqlClient configurations, sql statements, & etc. This will also included the loading of data to a form control including dataGridView and comboBox.
11-
12-
* Instantiation - creating an object `db` from a class type `Database` and instantiate using the `new` keyword.
13-
14-
see: [c-sharfcorner](https://www.c-sharpcorner.com/article/C-Sharp-object-instantiation-part-i-constructors/)
15-
10+
### How to use?
11+
1. Copy the [Classes](https://github.com/redmalmon/csharp-custom-classes/tree/main/Classes) folder into your project folder.
12+
2. From your project folder root directory, create a class named " [Config](https://github.com/redmalmon/csharp-custom-classes/blob/main/Config.cs) " and create a public instance of all the classes from [Classes](https://github.com/redmalmon/csharp-custom-classes/tree/main/Classes) folder. See the code below:
13+
```c#
14+
using Classes; // include the "Classes" folder
15+
namespace MySampleProject
16+
{
17+
class Config
18+
{
19+
/*
20+
* Database Information
21+
*/
22+
private static string host = "localhost";
23+
private static string dbname = "dbinformation";
24+
private static string dbuser = "root";
25+
private static string dbpassword = "";
26+
/*
27+
* Classes
28+
*/
29+
public Database db;
30+
public Validation validate;
31+
public Form_UI ui;
32+
public Visualizer visualizer;
33+
public Str_Date_Time date_time;
34+
public Upload upload;
35+
/*
36+
* Constructor
37+
*/
38+
public Config()
39+
{
40+
this.db = new Database(host, dbuser, dbpassword, dbname);
41+
this.validate = new Validation();
42+
this.ui = new Form_UI();
43+
this.visualizer = new Visualizer();
44+
this.date_time = new Str_Date_Time();
45+
this.upload = new Upload();
46+
}
47+
}
48+
}
49+
```
50+
3. Create an instance of your [Config](https://github.com/redmalmon/csharp-custom-classes/blob/main/Config.cs) class.
1651
```c#
17-
// instantiate an object `db` from `Database` class
18-
Database db = new Database();
52+
public partial class FormLogin : Form
53+
{
54+
Config config = new Config(); // your Config class
55+
56+
public FormLogin()
57+
{
58+
InitializeComponent();
59+
}
1960
```
61+
4. You can now use [Database](https://github.com/redmalmon/csharp-custom-classes/blob/main/Classes/Database.cs) , [Validation](https://github.com/redmalmon/csharp-custom-classes/blob/main/Classes/Validation.cs) , [Visualizer](https://github.com/redmalmon/csharp-custom-classes/blob/main/Classes/Visualizer.cs) , [Upload](https://github.com/redmalmon/csharp-custom-classes/blob/main/Classes/Upload.cs) , [Date_Time](https://github.com/redmalmon/csharp-custom-classes/blob/main/Classes/Str_Date_Time.cs) , & [Form_UI](https://github.com/redmalmon/csharp-custom-classes/blob/main/Classes/Form_UI.cs) Class.
62+
```c#
63+
config.db.....
64+
config.validate......
65+
config.ui......
66+
config.visualizer.....
67+
config.date_time.....
68+
config.upload.......
69+
```
70+
71+
72+
### Using [Database](https://github.com/redmalmon/csharp-custom-classes/blob/main/Classes/Database.cs) class - this is use for MySqlClient configurations, sql statements, & etc. This will also included the loading of data to a form control including dataGridView and comboBox.
2073
2174
#### Using Database' Methods
2275

@@ -25,67 +78,97 @@ Sql Statements
2578
* `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.
2679

2780
```c#
28-
var reader = db.select("select * from users");
81+
var reader = config.db.select("select * from users");
2982
while (reader.Read())
3083
{
3184
//reader["userid"].ToString();
3285
}
3386
```
3487

35-
* `save(string table, string[] column, string[] bind)` 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.
88+
* `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.
3689

37-
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.
90+
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.
3891

3992
```c#
40-
string[] value = { txtUsername.Text, txtPassword.Text, cmbSex.Text };
4193
string[] column = { "username", "password", "sex" };
42-
db.save("users", column, value);
94+
string[] value = { txtUsername.Text, txtPassword.Text, cmbSex.Text };
95+
config.db.save("users", column, value); // config.db.save("users", column, value, "User successfully saved!");
4396
```
4497

45-
* `cud(string qry, string msg = "")` 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.
98+
* `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.
4699

47100
```c#
48-
db.cud("Sql Query Here","Custom Notification Message");
101+
config.db.cud("Sql Query Here","Custom Notification Message");
49102
//or
50-
db.cud("Sql Query Here");
103+
config.db.cud("Sql Query Here");
51104
```
52105

106+
Example:
53107
```c#
54-
db.cud("INSERT INTO users (username,password,sex) VALUES ('" + txtUsername.Text + "','" + txtPassword.Text + "','" + cmbSex.Text + "')","Successfully Saved");
108+
config.db.cud("INSERT INTO users (username,password,sex) VALUES ('" + txtUsername.Text + "','" + txtPassword.Text + "','" + cmbSex.Text + "')","User successfully saved!");
55109
```
56110

57-
* `table(string qry, DataGridView dgv, string[] header = null)` or table() method is use for displaying data to DataGridView Control using the DataSource properties.
58111

59-
The `qry` parameter use for your sql satements `select`. The `dgv` parameter is use to pass the name of your DataGridView Control, while the `header` parameter (optional) is use to display the custom header name for your DataGridView display.
112+
* `table(string qry, DataGridView dgv, string[] header = null)` or table() method is use for displaying data to a DataGridView Control using the DataSource properties.
113+
114+
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.
60115

61116
```c#
62117
//using custom header
63118
string[] customheader = { "User ID", "Username", "Gender" };
64119
//the table methods
65-
db.table("select userid,username,sex from users", dgvUsers, customheader);
120+
config.db.table("select userid,username,sex from users", dgvUsers, customheader); // dgvUsers is the name of your datagridview
66121
```
67122

68123
```c#
69124
//the table methods without custom header
70-
db.table("select userid,username,sex from users", dgvUsers);
125+
config.db.table("select userid,username,sex from users", dgvUsers);
71126
```
72127

128+
* `list(string qry, ComboBox comboBox)` or list() method is use to display data to your comboBox control as an items.
73129

74-
### 2. [Date_time](https://github.com/redmalmon/CSharf-Custom-Classes/blob/main/C-Sharf%20Classes/Classes/Date_time.cs) class - for date & time conversion
130+
```c#
131+
config.db.list("select course_name from courses", cmbCourse); // cmbCourse is the name of your comboBox control
132+
```
75133

76-
*
134+
* `exist(string qry)` or exist() method is use to validate if a data is existing in your database. It will return `true` if exist.
135+
```c#
136+
if(config.db.exist("select username from courses where username = '"+ txtusername.Text +"'"))
137+
{
138+
MessageBox.Show("Username already exist!");
139+
}else{
140+
// save
141+
}
142+
```
77143

78-
### 3. [Public_variables](https://github.com/redmalmon/CSharf-Custom-Classes/blob/main/C-Sharf%20Classes/Classes/Public_variables.cs) class - stored global variables
144+
* `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.
79145

80-
*
146+
```c#
147+
int id = config.db.maxid('userid', 'users'); // it will return an int value
148+
```
149+
150+
151+
152+
### Using [Form_UI](https://github.com/redmalmon/csharp-custom-classes/blob/main/Classes/Form_UI.cs) class
81153

82-
### 4. [UI_events](https://github.com/redmalmon/CSharf-Custom-Classes/blob/main/C-Sharf%20Classes/Classes/UI_events.cs) class - form controls & events
154+
This class includes the traditional way of displaying form. Also includes the advanced UI manipulation using MDI or Multiple Document Interface.
83155

84-
* Instantiate
156+
#### The Basic :
157+
158+
* `Show(Form frmNew, Form frmOld)` or Show() method is simply use for displaying new Form and hiding active Form.
159+
- Behind the methods: `frmain.Show();` `this.Hide();`
160+
```c#
161+
frmMain frmain = new frmMain();
162+
config.ui.Show(frmain, this); // or config.ui.Show(new frmMain());
163+
```
164+
165+
* `Dialog(Form frm)` or Dialog() methods use for displaying Form as Dialog.
85166

86167
```c#
87-
UI_events ui = new UI_events();
168+
frmMain frmain = new frmMain();
169+
config.ui.Dialog(frmain); // or config.ui.Show(new frmMain());
88170
```
171+
#### The MDI :
89172

90173
* `FormShow(Form frm, string dstyle = "Fill")` or FormShow() methods is use for displaying form child to your MdiParent Form.
91174

@@ -95,33 +178,17 @@ frmDashboard dash = new frmDashboard();
95178
```
96179
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`.
97180
```c#
98-
ui.FormShow(dash, "Top");
181+
config.ui.FormShow(dash, "Top");
99182
```
100183

101184
```c#
102185
// Fill
103186
frmDashboard dash = new frmDashboard();
104-
ui.FormShow(dash);
187+
config.ui.FormShow(dash); // or config.ui.FormShow(new frmDashboard());
105188
```
106189

107-
* `Show(Form frmNew, Form frmOld)` or Show() method is simply use for displaying new Form and hiding active Form.
108-
109-
```c#
110-
frmMain frmain = new frmMain();
111-
ui.Show(frmain, this);
112-
//behind the methods
113-
//frmain.Show();
114-
//this.Hide();
115-
```
116-
117-
* `Dialog(Form frm)` or Dialog() methods use for displaying Form as Dialog.
118-
119-
```c#
120-
frmMain frmain = new frmMain();
121-
ui.Dialog(frmain);
122-
```
123190

124-
#### Chart
191+
### Using [Visualizer](https://github.com/redmalmon/csharp-custom-classes/blob/main/Classes/Visualizer.cs) - use for visualization of data.
125192

126193
* `chart(Chart chart, string SeriesName, string[] x, int[] y, string chartType = "Column")` or chart() method
127194

@@ -140,15 +207,15 @@ void loadChartSample()
140207
// Array Value
141208
int[] Y = { 12, 14, 9 };
142209
// Pass to Chart Methods = Male Series
143-
ui.chart(chartUser, "Male", X, Y, "Pie");
210+
config.visualizer.chart(chartUser, "Male", X, Y, "Pie");
144211
//_________________________________________________________________________________
145212
/*
146213
* NEW SERIES 'FEMALE'
147214
*/
148215
string[] x2 = { "BSIT", "BSED", "AB" };
149216
int[] y2 = { 6, 0, 16 };
150217
// Female Series
151-
ui.chart(chartUser, "Female", x2, y2, "Pie");
218+
config.visualizer.chart(chartUser, "Female", x2, y2, "Pie");
152219
}
153220
```
154221

@@ -166,76 +233,71 @@ Y = Y.Concat(new int[] { 2 }).ToArray();
166233
// Y = 12, 14, 2
167234
```
168235

169-
* Chart data from database
236+
* Chart data from database (Dynamic)
170237

171238
```c#
172239
/*
173-
* SERIES SEX
240+
* SERIES 'MALE'
174241
*/
175242
//initialized empty array
176243
string[] X = {};
177244
int[] Y = {};
178245

179-
var reader = db.select("select count(*) as c, sex from users group by sex");
246+
var reader = db.select("select count(*) as c, course from users where sex = 'MALE'");
180247
while (reader.Read())
181248
{
182-
X = X.Concat(new string[] { reader["sex"].ToString() }).ToArray();
183-
Y = Y.Concat(new int[] { Int32.Parse(reader["c"].ToString()) }).ToArray();
249+
X = X.Concat(new string[] { reader["course"].ToString() }).ToArray(); // string[] X = { "BSIT", "BSED", "AB" };
250+
Y = Y.Concat(new int[] { Int32.Parse(reader["c"].ToString()) }).ToArray(); // int[] Y = { 12, 14, 9 };
184251
}
185252
// Chart Methods
186-
// ChartSeries = "Sex"; ChartType = "Pie";
187-
ui.chart(chartUser, "Sex", X, Y, "Pie");
188-
```
253+
// ChartSeries = "MALE"; ChartType = "Bar"; // Chart type is optional, the default is column
254+
config.visualizer.chart(chartUser, "MALE", X, Y, "Bar");
189255

256+
/*
257+
* SERIES 'FEMALE'
258+
*/
259+
//initialized empty array
260+
string[] x2 = {};
261+
int[] y2 = {};
190262

191-
### 5. [Upload](https://github.com/redmalmon/CSharf-Custom-Classes/blob/main/C-Sharf%20Classes/Classes/Upload.cs) class - files & directories
192-
193-
*
263+
var reader = db.select("select count(*) as c, course from users where sex = 'FEMALE'");
264+
while (reader.Read())
265+
{
266+
x2 = x2.Concat(new string[] { reader["course"].ToString() }).ToArray(); // string[] x2 = { "BSIT", "BSED", "AB" };
267+
y2 = y2.Concat(new int[] { Int32.Parse(reader["c"].ToString()) }).ToArray(); // int[] y2 = { 6, 0, 16 };
268+
}
269+
// Chart Methods
270+
// ChartSeries = "FEMALE"; ChartType = "Bar"; // Chart type is optional, the default is column
271+
config.visualizer.chart(chartUser, "FEMALE", x2, y2, "Bar");
272+
```
194273

195-
### 6. [Validations](https://github.com/redmalmon/CSharf-Custom-Classes/blob/main/C-Sharf%20Classes/Classes/Validations.cs) class - keyboard events, mouse events, etc.
196274

197-
* Instantiate
275+
### Using [Validations](https://github.com/redmalmon/csharp-custom-classes/blob/main/Classes/Validation.cs) class - use for validationg inputs (keyboard events, mouse events, etc.)
198276
199-
```c#
200-
Validations validate = new Validations();
201-
```
202277

203-
* The `txtRequired(TextBox[] txt, string msg = "")` or txtTequired() method use to validate required input controls.
278+
* The `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.
204279

205280
```c#
206281
TextBox[] txt = { txtUsername, txtPassword, txtCPassword };
207282
ComboBox[] cmb = { cmbSex };
208-
if (!validate.txtRequired(txt) || !validate.cmbRequired(cmb))
283+
if (!config.validate.txtRequired(txt) || !config.validate.cmbRequired(cmb))
209284
return;
210285
```
211286

212-
* Accepts Numbers & Letters Only
287+
* The `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.
213288

214289
```c#
215290
private void txtUsername_KeyPress(object sender, KeyPressEventArgs e)
216291
{
217-
validate.alphanum(e);
292+
config.validate.alphanum(e);
218293
}
219294
```
220295

221296

222-
## [Config](https://github.com/redmalmon/CSharf-Custom-Classes/blob/main/C-Sharf%20Classes/Config.cs) Class
297+
### 2. [Date_time](https://github.com/redmalmon/csharp-custom-classes/blob/main/Classes/Str_Date_Time.cs) class - for date & time format conversion
223298
224-
* Database Information use as connection string.
225-
226-
```c#
227-
/*
228-
* Database Information
229-
*/
230-
public static string dbname = "dbinformation";
231-
public static string dbuser = "root";
232-
public static string dbpassword = "";
233-
```
299+
*
234300

235-
* Upload Directories & Validations.
301+
### Using [Upload](https://github.com/redmalmon/csharp-custom-classes/blob/main/Classes/Upload.cs) class - working with files & directories
236302
237-
```c#
238-
public static string upload_path = @"c:\C-Sharf Classes\";
239-
public static string allowedImage = "Image Files|*.jpg;*.jpeg;*.png";
240-
public static string allowedFile = "Excel Files(.xls ,.xlsx)| *.xls ;*.xlsx|PDF Files(.pdf)|*.pdf|Text Files(*.txt)|*.txt|Word Files(.docx ,.doc)|*.docx;*.doc";
241-
```
303+
*
6.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)