-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmMainAcc.cs
192 lines (157 loc) · 6.13 KB
/
frmMainAcc.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
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
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace BookShopManager
{
public partial class frmMainAcc : Form
{
public frmMainAcc()
{
InitializeComponent();
}
BookShopDataContext db = new BookShopDataContext();
private void frmMainAcc_Load(object sender, EventArgs e)
{
FormLoad();
}
private void FormLoad()
{
bsAcc.DataSource = db.UserTbls.ToList();
dvAcc.DataSource = bsAcc;
}
private void AddUser()
{
if (txtUser.Text == "" || txtPassword.Text == "" || picAcc.Image == null)
{
MessageBox.Show("Du lieu chua day du");
return;
}
UserTbl user = new UserTbl();
// Lưu hình ảnh
MemoryStream stream = new MemoryStream();
picAcc.Image.Save(stream, ImageFormat.Jpeg);
user.UAvatar = stream.ToArray();
user.UName = txtUser.Text;
user.UYob = dtpYob.Value;
user.UPhone = txtPhone.Text;
user.UAdd = txtAddress.Text;
user.UPass = txtPassword.Text;
user.UType = tcbAdmin.Checked;
db.UserTbls.InsertOnSubmit(user);
db.SubmitChanges();
FormLoad();
//Luu thay doi vao data base
NotificationHelper.ShowNotification("Success", "User Save Successfully", ToolTipIcon.Info);
}
private void EditUser()
{
if (dvAcc.SelectedRows.Count == 0)
return;
string id = dvAcc.SelectedCells[0].Value.ToString();
//Tao Lop de chinh sua nguoi dung
UserTbl user = db.UserTbls.Where(p => p.UId.Equals(id)).FirstOrDefault();
MemoryStream stream = new MemoryStream();
picAcc.Image.Save(stream, ImageFormat.Jpeg);
user.UAvatar = stream.ToArray();
user.UName = txtUser.Text;
user.UPass = txtPassword.Text;
user.UPhone = txtPhone.Text;
user.UAdd = txtAddress.Text;
user.UYob = dtpYob.Value;
user.UType = tcbAdmin.Checked;
db.SubmitChanges();
FormLoad();
}
private void DeleteUser()
{
if (dvAcc.SelectedRows.Count == 0)
return;
int vitri = bsAcc.Position;
// Xac Dinh ID nguoi Dung
//string id = dvUsers.SelectedCells[0].OwningRow.Cells["UId"].Value.ToString();
string id = txtID.Text;
//Tao Lop De Xoa Nguoi Dung Trong CSDL
UserTbl delete = db.UserTbls.Where(p => p.UId.Equals(id)).FirstOrDefault();
db.UserTbls.DeleteOnSubmit(delete);
db.SubmitChanges();
vitri = vitri - 1; // Nhay tới vị trí
bsAcc.Position = vitri;
FormLoad();
}
private void ClearUser()
{
txtID.Text = "";
txtUser.Text = "";
txtPassword.Text = "";
txtPhone.Text = "";
txtAddress.Text = "";
tcbAdmin.Checked = false;
picAcc.Image = null;
}
private void btnDelet_Click(object sender, EventArgs e) => DeleteUser();
private void btnEdit_Click(object sender, EventArgs e) => EditUser();
private void btnClear_Click(object sender, EventArgs e)
{
ClearUser();
}
private void btnAdd_Click(object sender, EventArgs e) { AddUser(); }
private void DisplayUser()
{
txtID.DataBindings.Clear();
txtUser.DataBindings.Clear();
txtPhone.DataBindings.Clear();
txtAddress.DataBindings.Clear();
txtPassword.DataBindings.Clear();
dtpYob.DataBindings.Clear();
tcbAdmin.DataBindings.Clear();
txtID.DataBindings.Add("Text", dvAcc.DataSource, "UId");
txtUser.DataBindings.Add("Text", dvAcc.DataSource, "UName");
txtPhone.DataBindings.Add("Text", dvAcc.DataSource, "UPhone");
txtAddress.DataBindings.Add("Text", dvAcc.DataSource, "UAdd");
txtPassword.DataBindings.Add("Text", dvAcc.DataSource, "UPass");
dtpYob.DataBindings.Add("Value", dvAcc.DataSource, "UYob");
tcbAdmin.DataBindings.Add("Checked", dvAcc.DataSource, "UType");
//doc pic tu coso du lieu trong display
string id = dvAcc.SelectedCells[0].OwningRow.Cells[0].Value.ToString();
UserTbl User = db.UserTbls.Where(p => p.UId.Equals(id)).FirstOrDefault();
if (User.UAvatar != null)
{
MemoryStream stream = new MemoryStream(User.UAvatar.ToArray());
picAcc.Image = Image.FromStream(stream);
}
else picAcc.Image = null;
}
private void btnNextBook_Click(object sender, EventArgs e) => bsAcc.MoveNext();
private void btnLastBook_Click(object sender, EventArgs e) => bsAcc.MoveLast();
private void btnPreviousBook_Click(object sender, EventArgs e) => bsAcc.MovePrevious();
private void btnFirstBook_Click(object sender, EventArgs e) => bsAcc.MoveFirst();
private void bsAcc_PositionChanged(object sender, EventArgs e)
{
DisplayUser();
UpdatePosition();
}
private void UpdatePosition()
{
int index = bsAcc.Position + 1;
int count = bsAcc.Count;
txtIDBooks.Text = $"{index}/{count}";
}
private void btnBrowser_Click(object sender, EventArgs e) => AddPic();
private void AddPic()
{
openPicBook.Filter = "JPEG Files (*.jpg, *.jpeg)|*.jpg;*.jpeg";
DialogResult result = openPicBook.ShowDialog();
if (result == DialogResult.OK)
{
string file = openPicBook.FileName;
if (string.IsNullOrEmpty(file))
return;
Image myImage = Image.FromFile(file);
picAcc.Image = myImage;
}
}
}
}