C# Dosyalama
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Collections;
namespace WindowsFormsApplication34
{
public partial class Form1 : Form
{
public ArrayList _tellist = new ArrayList();
public char[] karakter = new char[1];
//Düzenleme yapılacağı zaman seçilen satır bu değişkende tutulur
int secilenSatir = -1;
public string[] dizi = new string[4];
public Form1()
{
InitializeComponent();
}
private void Goster()
{
listBox1.Items.Clear();
foreach (Ogrenci _tr in _tellist)
{
listBox1.Items.Add(_tr.AdSoyad +""+ _tr.Vize +""+ _tr.Final+""+_tr.OgrenciNo);
}
}
private void Form1_Load(object sender, EventArgs e)
{
//Dosya yoksa oluşturulur
if (!File.Exists("ogr.text"))
{
var myFile = File.Create("ogr.text");
myFile.Close();
}
StreamReader oku = new StreamReader("ogr.text");
String str = "";
while (!oku.EndOfStream)
{
str = oku.ReadLine();
listBox1.Items.Add(str);
}
oku.Close();
textBox9.Enabled = false;
textBox8.Enabled = false;
textBox7.Enabled = false;
textBox5.Enabled = false;
}
public string karakterduzenle(string kelime, int db)
{
/*string goster="";
foreach (char harf in kelime)
{
goster = goster + harf;
if (kelime.Length < db)
break;
}
return goster;*/
string karakterler="";
for (int i = 0; i < db; i++)
{
if (kelime.Length < db)
{
karakterler = kelime;
break;
}
karakterler = karakterler + kelime[i];
}
return karakterler.ToString();
}
private void button5_Click(object sender, EventArgs e)
{
int vize, final, sonuc;
vize = Convert.ToInt32(textBox2.Text);
final = Convert.ToInt32(textBox3.Text);
sonuc = (vize * 30) / 100 + (final * 70) / 100;
label6.Text = sonuc.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
if (degerlerKontrol() == 0)
{
/*Eğer degerlerKontrol fonksiyonda textboxlara girilen
degerler istediğimiz formatta değilse program else girmez
* yani sadece hata verir ve kalır
*/
}
else
{
Ogrenci asd = new Ogrenci();
asd.AdSoyad = karakterduzenle(textBox1.Text, 5);
asd.Vize = Convert.ToInt32(karakterduzenle(textBox2.Text, 3));
asd.Final = Convert.ToInt32(karakterduzenle(textBox3.Text, 3));
asd.OgrenciNo = Convert.ToInt32(karakterduzenle(textBox4.Text, 5));
StreamWriter SW = File.AppendText("ogr.text");
SW.WriteLine(asd.AdSoyad + "," + asd.OgrenciNo + "," + asd.Vize + "," + asd.Final);
SW.Close();
_tellist.Add(asd);
listBox1.Items.Clear();
StreamReader oku = new StreamReader("ogr.text");
String str = "";
while (!oku.EndOfStream)
{
str = oku.ReadLine();
listBox1.Items.Add(str);
}
oku.Close();
textBox9.Enabled = false;
textBox8.Enabled = false;
textBox7.Enabled = false;
textBox5.Enabled = false;
}
secilenSatir = -1;
listBox1.SelectedIndex = -1;
}
private int degerlerKontrol()
{
if (textBox1.Text == "")
{
MessageBox.Show("adsoyad kısmı boş geçilemez");
return 0;
}
if (textBox2.Text == "")
{
MessageBox.Show("vize kısmı boş geçilemez");
return 0;
}
if (textBox3.Text == "")
{
MessageBox.Show("final kısmı boş geçilemez");
return 0;
}
if (textBox4.Text == "")
{
MessageBox.Show("ogrencino kısmı boş geçilemez");
return 0;
}
if (Convert.ToInt32(textBox2.Text) > 100 || Convert.ToInt32(textBox3.Text) > 100)
{
MessageBox.Show("100 den büyük girilemez");
return 0;
}
StreamReader oku = new StreamReader("ogr.text");
String str = "";
while (!oku.EndOfStream)
{
str = oku.ReadLine();
string[] ogrenciNoDizi = str.Split(',');
if (ogrenciNoDizi.Length > 0)
{
try
{
if (ogrenciNoDizi[1].Equals(textBox4.Text.ToString()))
{
MessageBox.Show("Girdiğiniz Öğrenci no sisteme kayıtlı!" + " (" + textBox4.Text.ToString() + ")");
oku.Close();
return 0;
}
}
catch
{
}
}
}
oku.Close();
return 1;
}
private void button2_Click(object sender, EventArgs e)
{
StreamReader oku = new StreamReader("ogr.text");
String[] str = new String[200];
String gecici = "";
int i=0;
int j = 0;
while (!oku.EndOfStream)
{
if (listBox1.SelectedIndex != i)
{
str[j] = oku.ReadLine();
j++;
}
else
{
gecici = oku.ReadLine();
}
i++;
}
oku.Close();
StreamWriter sw = new StreamWriter(("ogr.text"));
for (int x = 0; x < j; x++)
{
sw.WriteLine(str[x]);
}
sw.Close();
listBox1.Items.Clear();
StreamReader oku2 = new StreamReader("ogr.text");
String str2 = "";
listBox1.Items.Clear();
while (!oku2.EndOfStream)
{
str2 = oku2.ReadLine();
listBox1.Items.Add(str2);
}
oku2.Close();
//Satır seçimi sıfırlandığı varsayılır
secilenSatir = -1;
textBox9.Enabled = false;
textBox8.Enabled = false;
textBox7.Enabled = false;
textBox5.Enabled = false;
}
private void button4_Click(object sender, EventArgs e)
{
StreamReader oku = new StreamReader("ogr.text");
String str = "";
int i = 0;
bool kontrol = true;
while (!oku.EndOfStream)
{
str = oku.ReadLine();
string[] ogrenciDizi = str.Split(',');
if (ogrenciDizi[1].Equals(textBox6.Text))
{
MessageBox.Show("Aranılan Öğrenci Listede Var!");
secilenSatir = i;
textBox9.Text = ogrenciDizi[0];
textBox5.Text = ogrenciDizi[1];
textBox8.Text = ogrenciDizi[2];
textBox7.Text = ogrenciDizi[3];
kontrol = false;
textBox9.Enabled = true;
textBox8.Enabled = true;
textBox7.Enabled = true;
textBox5.Enabled = true;
}
i++;
}
if(kontrol)
MessageBox.Show("Aradığınız kullanıcı listede yok!");
oku.Close();
}
private void button3_Click(object sender, EventArgs e)
{
Ogrenci _tr = new Ogrenci();
_tr.AdSoyad = textBox1.Text;
_tr.Vize = Convert.ToInt32(textBox2.Text);
_tr.Final = Convert.ToInt32(textBox3.Text);
_tr.OgrenciNo = Convert.ToInt32(textBox4.Text);
_tellist[listBox1.SelectedIndex] = _tr;
Goster();
int kayit = listBox1.Items.Count;
if (kayit > 0)
{
for (int i = 0; i < kayit; i++)
{
if (!File.Exists("pes.txt"))
{
StreamWriter d = new StreamWriter("pes.txt");
d.WriteLine(listBox1.Items[i].ToString());
d.Close();
}
else
{
StreamWriter n = File.AppendText("pes.txt");
n.WriteLine("" + listBox1.Items[i].ToString());
n.Close();
}
}
}
File.Delete("ogr.txt");
FileInfo degis = new FileInfo("pes.txt");
degis.MoveTo("ogr.txt");
listBox1.ClearSelected();
}
private void button6_Click(object sender, EventArgs e)
{
FileStream fs;
StreamReader sr;
string dosyaadi;
karakter[0] = ',';
string satır;
dosyaadi = "ogr.txt";
fs = new FileStream(dosyaadi, FileMode.Open, FileAccess.Read);
sr = new StreamReader(fs);
while ((satır = sr.ReadLine()) != null)
{
dizi = satır.Split(karakter);
//listBox1.Items.Add(dizi[0].ToString()+","+dizi[1].ToString()+","+dizi[2].ToString()+","+dizi[3].ToString());
Ogrenci _tr = new Ogrenci();
_tr.AdSoyad = dizi[0].ToString();
_tr.Vize =Convert.ToInt32(dizi[1]);
_tr.Final = Convert.ToInt32(dizi[2]);
_tr.OgrenciNo = Convert.ToInt32(dizi[3].ToString());
_tellist.Add(_tr);
Goster();
}
sr.Close();
fs.Close();
}
private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
string[] ogrenciDizi = listBox1.GetItemText(listBox1.SelectedItem).Split(',');
secilenSatir = listBox1.SelectedIndex;
try
{
if (ogrenciDizi.Length > 0)
{
textBox9.Text = ogrenciDizi[0];
textBox5.Text = ogrenciDizi[1];
textBox8.Text = ogrenciDizi[2];
textBox7.Text = ogrenciDizi[3];
textBox9.Enabled = true;
textBox8.Enabled = true;
textBox7.Enabled = true;
textBox5.Enabled = true;
}
}
catch
{
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void button6_Click_1(object sender, EventArgs e)
{
if (secilenSatir != -1)
{
if (degerlerKontrol2() == 0)
{
}
else
{
StreamReader oku = new StreamReader("ogr.text");
String[] str = new String[200];
int i = 0;
int j = 0;
String gecici = "";
while (!oku.EndOfStream)
{
if (secilenSatir != i)
{
str[j] = oku.ReadLine();
j++;
}
else
{
gecici = oku.ReadLine();
Ogrenci asd = new Ogrenci();
asd.AdSoyad = karakterduzenle(textBox9.Text, 5);
asd.Vize = Convert.ToInt32(karakterduzenle(textBox8.Text, 3));
asd.Final = Convert.ToInt32(karakterduzenle(textBox7.Text, 3));
asd.OgrenciNo = Convert.ToInt32(karakterduzenle(textBox5.Text, 5));
str[j] = asd.AdSoyad + "," + asd.OgrenciNo + "," + asd.Vize + "," + asd.Final;
j++;
}
i++;
}
oku.Close();
StreamWriter sw = new StreamWriter(("ogr.text"));
for (int x = 0; x < j; x++)
{
sw.WriteLine(str[x]);
}
sw.Close();
listBox1.Items.Clear();
StreamReader oku2 = new StreamReader("ogr.text");
String str2 = "";
listBox1.Items.Clear();
while (!oku2.EndOfStream)
{
str2 = oku2.ReadLine();
listBox1.Items.Add(str2);
}
oku2.Close();
secilenSatir = -1;
textBox9.Enabled = false;
textBox8.Enabled = false;
textBox7.Enabled = false;
textBox5.Enabled = false;
}
}
else
{
MessageBox.Show("Seçilen öğrenci yok!");
}
}
private int degerlerKontrol2()
{
if (textBox9.Text == "")
{
MessageBox.Show("adsoyad kısmı boş geçilemez");
return 0;
}
if (textBox8.Text == "")
{
MessageBox.Show("vize kısmı boş geçilemez");
return 0;
}
if (textBox7.Text == "")
{
MessageBox.Show("final kısmı boş geçilemez");
return 0;
}
if (textBox5.Text == "")
{
MessageBox.Show("ogrencino kısmı boş geçilemez");
return 0;
}
if (Convert.ToInt32(textBox8.Text) > 100 || Convert.ToInt32(textBox7.Text) > 100)
{
MessageBox.Show("100 den büyük girilemez");
return 0;
}
StreamReader oku = new StreamReader("ogr.text");
String str = "";
while (!oku.EndOfStream)
{
str = oku.ReadLine();
string[] ogrenciNoDizi = str.Split(',');
if (ogrenciNoDizi.Length > 0)
{
try
{
if (ogrenciNoDizi[1].Equals(textBox5.Text.ToString()))
{
MessageBox.Show("Girdiğiniz Öğrenci no sisteme kayıtlı!" + " (" + textBox4.Text.ToString() + ")");
oku.Close();
return 0;
}
}
catch
{
}
}
}
oku.Close();
return 1;
}
}
}
|
Öğrenci Sınıfı
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication34
{
class Ogrenci
{
public string AdSoyad;
public int Vize;
public int Final;
public int OgrenciNo;
}
}
|
Projenin tamamını BURADAN indirebilirsiniz.
Leave a reply