· 7 years ago · Jan 12, 2019, 08:20 AM
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Windows.Forms;
9using System.IO;
10namespace raFile
11{
12 public partial class Form1 : Form
13 {
14 FileStream raFile;
15 DataTable myTable;
16 public Form1()
17 {
18 InitializeComponent();
19 }
20
21 private void Form1_Load(object sender, EventArgs e)
22 {
23 try
24 {
25 raFile = new FileStream("Clients.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite);
26 if (raFile.Length != 4400)
27 {
28 initializeFile();
29 }
30 }
31 catch(IOException ex)
32 {
33 MessageBox.Show(ex.Message, "Error Creating File");
34 }
35 //listBox1.KeyDown += new KeyEventHandler(listBox1_KeyDown);
36 txtBalance.KeyDown += new KeyEventHandler(txtBalance_KeyDown);
37 txtBalance.ContextMenu = new System.Windows.Forms.ContextMenu();
38
39 txtBalance.KeyPress += new KeyPressEventHandler(txtBalance_KeyPress);
40 txtLName.KeyPress += new KeyPressEventHandler(txtLName_KeyPress);
41 txtLName.ContextMenu = new System.Windows.Forms.ContextMenu();
42 txtFName.KeyPress += new KeyPressEventHandler(txtFName_KeyPress);
43 txtFName.ContextMenu = new System.Windows.Forms.ContextMenu();
44 txtAcctNum.KeyPress += new KeyPressEventHandler(txtAcctNum_KeyPress);
45 txtAcctNum.ContextMenu = new System.Windows.Forms.ContextMenu();
46 dg1.Click += new EventHandler(dg1_Click);
47
48 makeDataTableAndDisplay();
49 for (int i = 0; i < dg1.Columns.Count; i++)
50 {
51 dg1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
52 }
53 dg1.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
54 readFile();
55 }
56
57 void dg1_Click(object sender, EventArgs e)
58 {
59 txtAcctNum.Text = dg1.CurrentRow.Cells["Account"].Value.ToString();
60 txtFName.Text = dg1.CurrentRow.Cells[1].Value.ToString();
61 txtLName.Text = dg1.CurrentRow.Cells[2].Value.ToString();
62 txtBalance.Text = dg1.CurrentRow.Cells[3].Value.ToString();
63 setControlState("u/d");
64 }
65
66 private void makeDataTableAndDisplay()
67 {
68 //create table
69 myTable = TableFactory.makeTable();
70 //bind and display table
71 bindingSource1.DataSource = myTable;
72 dg1.DataSource = bindingSource1;
73 }
74 void txtBalance_KeyDown(object sender, KeyEventArgs e)
75 {
76 if (e.KeyCode == Keys.Escape)
77 {
78 setControlState("w");
79 }
80 }
81
82 void listBox1_KeyDown(object sender, KeyEventArgs e)
83 {
84 if (e.KeyCode == Keys.Escape)
85 {
86 setControlState("w");
87 }
88 }
89 private void initializeFile()
90 {
91 MessageBox.Show("intializeFile() called");
92 AccountRecordRA ra = new AccountRecordRA();
93 try
94 {
95 //position FilePointer
96 raFile.Seek(0, SeekOrigin.Begin);
97 for (int i = 0; i < 100; i++)
98 {
99 ra.Write(raFile);
100 }
101 }
102 catch(IOException ex)
103 {
104 MessageBox.Show(ex.Message, "Error Intializing File");
105 }
106 }
107 private void readFile()
108 {
109 //listBox1.Items.Clear();
110 myTable.Rows.Clear();
111 AccountRecordRA ra = new AccountRecordRA();
112 try
113 {
114 raFile.Seek(0, SeekOrigin.Begin);
115 for (int i = 0; i < 100; i++)
116 {
117 ra.Read(raFile);
118 if(ra.Account > 0)
119 {
120 DataRow dr = myTable.NewRow();
121 dr["Account"] = ra.Account;
122 dr["FirstName"] = ra.FirstName;
123 dr["LastName"] = ra.LastName;
124 dr["Balance"] = ra.Balance.ToString("c");
125 myTable.Rows.Add(dr);
126 //listBox1.Items.Add(ra.Account + "; " + ra.FirstName + ";" + ra.LastName + ";" + ra.Balance.ToString("c"));
127 }
128 }
129 dg1.ClearSelection();
130 }
131 catch (IOException ex)
132 {
133 MessageBox.Show(ex.Message, "Error Reading File");
134 }
135 }
136
137 //private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
138 //{
139 // if (listBox1.SelectedIndex > -1)
140 // {
141 // string record = listBox1.Items[listBox1.SelectedIndex].ToString();
142 // string[] tokens = record.Split(new char[] { ';' });
143 // txtAcctNum.Text = tokens[0];
144 // txtFName.Text = tokens[1];
145 // txtLName.Text = tokens[2];
146 // txtBalance.Text = tokens[3];
147 // setControlState("u/d");
148 // }
149 //}
150
151 private void cmdWrite_Click(object sender, EventArgs e)
152 {
153 if (dataGood())
154 {
155 int acct = Convert.ToInt32(txtAcctNum.Text);
156 if (isValidAccount(acct))
157 {
158 string fn = txtFName.Text;
159 string ln = txtLName.Text;
160 double bal = Convert.ToDouble(txtBalance.Text);
161 AccountRecordRA ra = new AccountRecordRA(acct, fn, ln, bal);
162 try
163 {
164 raFile.Seek((acct - 1) * 44, SeekOrigin.Begin);
165 ra.Write(raFile);
166 readFile();
167 clearText();
168 }
169 catch (IOException ex)
170 {
171 MessageBox.Show(ex.Message, "Error Writing Record");
172 }
173 }
174 else
175 {
176 MessageBox.Show("This Account exists! \n Enter new Account Number!", "Primary Key Violation", MessageBoxButtons.OK, MessageBoxIcon.Error);
177 txtAcctNum.Focus();
178 }
179 }
180 }
181
182 private void cmdUpdate_Click(object sender, EventArgs e)
183 {
184 if (dataGood())
185 {
186 int acct = Convert.ToInt32(txtAcctNum.Text);
187 string fn = txtFName.Text;
188 string ln = txtLName.Text;
189 string sBal = txtBalance.Text;
190 if (sBal[0] == '$')
191 {
192 sBal = sBal.Remove(0, 1);
193 }
194 double bal = Convert.ToDouble(sBal);
195 AccountRecordRA ra = new AccountRecordRA(acct, fn, ln, bal);
196 try
197 {
198 raFile.Seek((acct - 1) * 44, SeekOrigin.Begin);
199 ra.Write(raFile);
200 readFile();
201 }
202 catch (IOException ex)
203 {
204 MessageBox.Show(ex.Message, "Error Updating Record");
205 }
206 setControlState("w");
207 }
208 }
209
210 private void cmdDelete_Click(object sender, EventArgs e)
211 {
212 if (MessageBox.Show("Are you sure you want to delete this record?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
213 {
214 int acct = Convert.ToInt32(txtAcctNum.Text);
215 AccountRecordRA ra = new AccountRecordRA();
216 try
217 {
218 raFile.Seek((acct - 1) * 44, SeekOrigin.Begin);
219 ra.Write(raFile);
220 readFile();
221 }
222 catch (IOException ex)
223 {
224 MessageBox.Show(ex.Message, "Error Updating Record");
225 }
226 }
227 setControlState("w");
228 }
229 private void clearText()
230 {
231 txtAcctNum.Text = "";
232 txtBalance.Text = "";
233 txtFName.Text = "";
234 txtLName.Text = "";
235 //listBox1.ClearSelected();
236 dg1.ClearSelection();
237 txtAcctNum.Focus();
238 }
239 private bool dataGood()
240 {
241 if (txtAcctNum.Text.Length < 1)
242 {
243 MessageBox.Show("The Account Number field must be completed!", "Invalid Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
244 txtAcctNum.Focus();
245 return false;
246 }
247 else if (txtAcctNum.Text.Length > 2 && (Convert.ToInt32(txtAcctNum.Text) >100))
248 {
249 MessageBox.Show("The Account Number must be in the range of 1 to 100.\n Re enter Account Number!", "Account Number out of Range", MessageBoxButtons.OK, MessageBoxIcon.Error);
250 txtAcctNum.Focus();
251 return false;
252 }
253 else if (txtFName.Text.Length < 1)
254 {
255 MessageBox.Show("The First Name field must be completed!", "Invalid Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
256 txtFName.Focus();
257 return false;
258 }
259 else if (txtLName.Text.Length < 1)
260 {
261 MessageBox.Show("The Last Name field must be completed!", "Invalid Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
262 txtLName.Focus();
263 return false;
264 }
265 else if (txtBalance.Text.Length < 1)
266 {
267 MessageBox.Show("The Balance field must be completed!", "Invalid Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
268 txtBalance.Focus();
269 return false;
270 }
271 else if (txtBalance.Text.Length == 1)
272 {
273 if (txtBalance.Text[0] == '$')
274 {
275 MessageBox.Show("The entered value is not a number!\n Re enter Balance!", "Invalid Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
276 txtBalance.Focus();
277 return false;
278 }
279
280 }
281 return true;
282 }
283 private bool isValidAccount(int acct)
284 {
285 //for(int i = 0; i < listBox1.Items.Count; i++)
286 //{
287 // int len = listBox1.Items[i].ToString().IndexOf(";");
288 // string sAcct = listBox1.Items[i].ToString().Substring(0, len);
289 // if(acct == Convert.ToInt32(sAcct))
290 // {
291 // return false;
292 // }
293 //}
294 //for(int i = 0; i < dg1.Rows.Count; i++)
295 //{
296 // int cellAcct = Convert.ToInt32(dg1.Rows[i].Cells[0].Value);
297 // if (acct == cellAcct)
298 // {
299 // return false;
300 // }
301 //}
302 for (int i = 0; i < myTable.Rows.Count; i++)
303 {
304 int cellAcct = Convert.ToInt32(myTable.Rows[i][0]);
305 if(acct == cellAcct)
306 {
307 return false;
308 }
309 }
310 return true;
311 }
312 private void setControlState(string state)
313 {
314 if (state.Equals("w"))
315 {
316 cmdWrite.Enabled = true;
317 cmdUpdate.Enabled = false;
318 cmdDelete.Enabled = false;
319 txtAcctNum.Enabled = true;
320 txtFName.Enabled = true;
321 txtLName.Enabled = true;
322 clearText();
323 lblInfo.Text = "Select a Record to Update or Delete";
324 }
325 else if (state.Equals("u/d"))
326 {
327 cmdWrite.Enabled = false;
328 cmdUpdate.Enabled = true;
329 cmdDelete.Enabled = true;
330 txtAcctNum.Enabled = false;
331 txtFName.Enabled = false;
332 txtLName.Enabled = false;
333 lblInfo.Text = "Press ESCAPE to return to Write Mode";
334 }
335 }
336
337 void txtAcctNum_KeyPress(object sender, KeyPressEventArgs e)
338 {
339 int c = e.KeyChar;
340 int len = ((TextBox)sender).Text.Length;
341 ((TextBox)sender).SelectionStart = len;
342 /*
343 txtAcctnum
344 * 1. 1st digit (1 to 9) ascii [ 48 to 57 ]
345 * 2. After first (0 to 9)
346 * 3. Numeric Digits only
347 */
348 if (c != 8)// back space
349 {
350 if (len == 0)
351 {
352 if (c < 49 || c > 57)// not 1 to 9
353 {
354 e.Handled = true;
355 }
356 }
357 else if (c < 48 || c > 57)// not 0 to 9
358 {
359 e.Handled = true;
360 }
361 }
362 }
363
364 /*
365 * firt | last name
366 * 1. letters only
367 * 2. first char capital
368 * 3. charset after first, lower case
369 * ascii
370 * A-Z [ 65 to 90 ]
371 * a-z [ 97 to 122 ]
372 */
373
374 void txtFName_KeyPress(object sender, KeyPressEventArgs e)
375 {
376 int c = e.KeyChar;
377 int len = ((TextBox)sender).Text.Length;
378 ((TextBox)sender).SelectionStart = len;
379 if (c != 8)
380 {
381 if ((c < 65 || c > 90) && (c < 97 || c > 122))
382 {
383 // not letter
384 e.Handled = true;
385 }
386 else if (len == 0)
387 {
388 // if first letter is lower case convert to upper case
389 if (c > 96 && c < 123)// lower case letter
390 {
391 e.KeyChar = (char) (c - 32);
392 }
393 }
394 else if (len > 0)
395 {
396 if (c > 64 && c < 91)// upper case letter
397 {
398 e.KeyChar = (char)(c + 32);
399 }
400 }
401 }
402 }
403
404 void txtLName_KeyPress(object sender, KeyPressEventArgs e)
405 {
406 int c = e.KeyChar;
407 int len = ((TextBox)sender).Text.Length;
408 ((TextBox)sender).SelectionStart = len;
409 if (c != 8)
410 {
411 if ((c < 65 || c > 90) && (c < 97 || c > 122))
412 {
413 // not letter
414 e.Handled = true;
415 }
416 else if (len == 0)
417 {
418 // if first letter is lower case convert to upper case
419 if (c > 96 && c < 123)// lower case letter
420 {
421 e.KeyChar = (char)(c - 32);
422 }
423 }
424 else if (len > 0)
425 {
426 if (c > 64 && c < 91)// upper case letter
427 {
428 e.KeyChar = (char)(c + 32);
429 }
430 }
431 }
432
433 }
434
435 void txtBalance_KeyPress(object sender, KeyPressEventArgs e)
436 {
437 /*
438 * balance
439 * 1. Decimal Number
440 * a) first char must be a number
441 * 2) 2 digits only after decimal point
442 * if not number
443 * if decimal
444 * if tb contains decimal
445 * kill char
446 * else
447 * kill char
448 *if decimal exists
449 * if 2 digits after decimal
450 * kill char
451 */
452 int c = e.KeyChar;
453 int len = ((TextBox)sender).Text.Length;
454 ((TextBox)sender).SelectionStart = len;
455 if (c != 8)
456 {
457 if (len == 0 && (c < 48 || c > 57))
458 {
459 e.Handled = true;
460 }
461 else if (len > 0)
462 {
463 if (c < 48 || c > 57)// not number
464 {
465 if (c == 46)// decimal point
466 {
467 if (((TextBox)sender).Text.IndexOf(".") > -1)
468 {
469 //textbox contains decimal
470 e.Handled = true;
471 }
472 }
473 else// not number and not decimal point
474 {
475 e.Handled = true;
476 }
477 }
478 else if (((TextBox)sender).Text.IndexOf(".") > -1)
479 {
480 // decimal exists
481 if (len == (((TextBox)sender).Text.IndexOf(".") + 3))
482 {
483 e.Handled = true;
484 }
485 }
486 }
487 }
488 }
489
490 private void txtAcctNum_TextChanged(object sender, EventArgs e)
491 {
492
493 }
494
495 private void txtFName_TextChanged(object sender, EventArgs e)
496 {
497
498 }
499
500 private void txtBalance_TextChanged(object sender, EventArgs e)
501 {
502
503 }
504
505 private void label1_Click(object sender, EventArgs e)
506 {
507
508 }
509
510 }
511}