-
Notifications
You must be signed in to change notification settings - Fork 0
/
Daftar Pegawai ListBox.cs
120 lines (104 loc) · 3.79 KB
/
Daftar Pegawai ListBox.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace kelompok
{
public partial class Daftar_Pegawai_ListBox : Form
{
public Daftar_Pegawai_ListBox()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string kodePegawai = textBox1.Text;
string namaPegawai = textBox2.Text;
string jenisKelamin = radioButton1.Checked ? "Pria" : "Wanita";
string departemen = comboBox1.SelectedItem?.ToString() ?? "";
if (string.IsNullOrEmpty(kodePegawai) || string.IsNullOrEmpty(namaPegawai) || string.IsNullOrEmpty(departemen))
{
MessageBox.Show("Harap isi semua field dengan benar.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
using (StreamWriter sw = new StreamWriter("Pegawai.txt", true))
{
sw.WriteLine($"{kodePegawai}|{namaPegawai}|{jenisKelamin}|{departemen}");
}
listBox1.Font = new Font("Courier New", listBox1.Font.Size);
string formattedOutput =
kodePegawai.PadRight(8) + "|" +
namaPegawai.PadRight(10) + "|" +
jenisKelamin.PadRight(10) + "|" +
departemen.PadRight(12);
listBox1.Items.Add(formattedOutput);
ClearInputFields();
MessageBox.Show("Data pegawai berhasil ditambahkan.", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button2_Click(object sender, EventArgs
e)
{
TampilkanData("");
}
private void button3_Click(object sender, EventArgs e)
{
TampilkanData("Pria");
}
private void button4_Click(object sender, EventArgs e)
{
TampilkanData("Wanita");
}
private void TampilkanData(string filterJenisKelamin)
{
listBox1.Items.Clear();
try
{
listBox1.Font = new Font("Courier New", listBox1.Font.Size);
string[] lines = File.ReadAllLines("Pegawai.txt");
foreach (string line in lines)
{
string[] data = line.Split('|');
if (string.IsNullOrEmpty(filterJenisKelamin) || data[2] == filterJenisKelamin)
{
string formattedOutput =
data[0].PadRight(8) +"|"+
data[1].PadRight(10) + "|" +
data[2].PadRight(10) + "|" +
data[3].PadRight(12);
listBox1.Items.Add(formattedOutput);
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ClearInputFields()
{
textBox1.Clear();
textBox2.Clear();
radioButton1.Checked = false;
radioButton2.Checked = false;
comboBox1.SelectedIndex = -1;
textBox1.Focus();
}
private void Daftar_Pegawai_ListBox_Load(object sender, EventArgs e)
{
TampilkanData("");
}
}
}