| | |
| | | using LB_SmartVision.Forms.Pages.ProcessPage; |
| | | using LB_SmartVision.Forms.Pages.CameraPage; |
| | | using LB_SmartVision.Forms.Pages.ProcessPage; |
| | | using LB_SmartVision.Forms.Pages.SettingPage; |
| | | using LB_SmartVision.ProcessRun; |
| | | using LB_SmartVision.SQL; |
| | | using LB_SmartVisionCommon; |
| | | using LB_VisionControl; |
| | | using LB_VisionProcesses; |
| | | using Newtonsoft.Json; |
| | | using Newtonsoft.Json.Serialization; |
| | | using System.Collections.Concurrent; |
| | | using System.Data; |
| | | using System.Text; |
| | | using VisionControl.Forms; |
| | | using LB_SmartVision.Forms.Pages.SettingPage; |
| | | using LB_SmartVisionCommon; |
| | | using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel; |
| | | using LB_SmartVision.Forms.Pages.CameraPage; |
| | | using System.Security.Principal; |
| | | using System.Text; |
| | | using System.Windows.Forms; |
| | | using VisionControl.Forms; |
| | | using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel; |
| | | |
| | | namespace LB_SmartVision.Forms.Pages.UserManagementPage |
| | | { |
| | | public partial class UserManagementEditPage : UserControl |
| | | { |
| | | public Action<string, LogInfoType> LogInfo; |
| | | List<RecordUserData> recordUserDatas = new List<RecordUserData>(); |
| | | |
| | | public UserManagementEditPage() |
| | | { |
| | |
| | | Text = "用户管理设置"; |
| | | |
| | | InitializeComponent(); |
| | | InitializeDataGridView(); |
| | | InitializeComboBox(); |
| | | InitializeDataGridView(); |
| | | |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// </summary> |
| | | private void InitializeDataGridView() |
| | | { |
| | | this.dataGridViewUM.DataSource = recordUserDatas; |
| | | // 设置DataGridView列宽 |
| | | dataGridViewUM.ColumnCount = 4; |
| | | |
| | | int totalWidth = dataGridViewUM.ClientSize.Width; |
| | | int columnCount = dataGridViewUM.ColumnCount; |
| | | int columnWidth = totalWidth / columnCount; |
| | | |
| | | // 设置最小宽度 |
| | | int minWidth = 100; // 最小宽度 |
| | | if (columnWidth < minWidth) |
| | | { |
| | | columnWidth = minWidth; |
| | | } |
| | | |
| | | for (int i = 0; i < columnCount; i++) |
| | | { |
| | | dataGridViewUM.Columns[i].Width = columnWidth; |
| | | } |
| | | |
| | | // 设置列标题 |
| | | dataGridViewUM.Columns[0].Name = "用户名"; |
| | | //dataGridViewUM.Columns[1].Name = "密码"; |
| | | dataGridViewUM.Columns[1].Name = "姓名"; |
| | | dataGridViewUM.Columns[2].Name = "工号"; |
| | | dataGridViewUM.Columns[3].Name = "权限"; |
| | | |
| | | dataGridViewUM.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; |
| | | |
| | | // 禁止编辑单元格(可选) |
| | | dataGridViewUM.ReadOnly = true; |
| | | |
| | | // 允许多行选择(可选) |
| | | dataGridViewUM.MultiSelect = false; |
| | | |
| | | // 显示行标题(可选) |
| | | dataGridViewUM.RowHeadersVisible = true; |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | comboBoxPermission.SelectedIndex = 0; |
| | | textBoxUsername.Focus(); // 将焦点设置回用户名输入框 |
| | | } |
| | | |
| | | #region 添加用户按钮 |
| | | private void btnAdd_Click(object sender, EventArgs e) |
| | | { |
| | | // 验证输入 |
| | |
| | | return; |
| | | } |
| | | |
| | | // 创建新行数据 |
| | | string[] row = new string[] |
| | | RecordUserData user = new RecordUserData(); |
| | | user.EmployeeNumber = this.textBoxEmployeeID.Text; |
| | | user.EmployeeAccount = this.textBoxUsername.Text; |
| | | user.EmployeePassword = this.textBoxPassword.Text; |
| | | user.EmployeeName = this.textBoxName.Text; |
| | | user.EmployeePermission = (UserPermission)this.comboBoxPermission.SelectedIndex; |
| | | // 添加到 UserManager |
| | | bool success = UserManager.Instance.AddUser(user); |
| | | |
| | | if (success) |
| | | { |
| | | textBoxUsername.Text, |
| | | //textBoxPassword.Text, // 实际应用中密码应该加密 |
| | | textBoxName.Text, |
| | | textBoxEmployeeID.Text, |
| | | comboBoxPermission.SelectedItem.ToString() |
| | | }; |
| | | recordUserDatas.Add(user); |
| | | |
| | | // 设置整个DataGridView的默认字体和颜色 |
| | | dataGridViewUM.DefaultCellStyle.Font = new Font("宋体", 12); |
| | | dataGridViewUM.DefaultCellStyle.ForeColor = Color.Black; // 字体颜色 |
| | | dataGridViewUM.DefaultCellStyle.BackColor = Color.White; // 背景颜色 |
| | | //MessageBox.Show("用户添加成功!", "提示", |
| | | // MessageBoxButtons.OK, MessageBoxIcon.Information); |
| | | |
| | | // 添加新行到DataGridView |
| | | dataGridViewUM.Rows.Add(row); |
| | | // 清空输入框 |
| | | ClearInputFields(); |
| | | } |
| | | else |
| | | { |
| | | MessageBox.Show("添加用户失败!", "错误", |
| | | MessageBoxButtons.OK, MessageBoxIcon.Error); |
| | | } |
| | | |
| | | // 清空输入框 |
| | | ClearInputFields(); |
| | | } |
| | | #endregion |
| | | |
| | | #region 删除用户按钮 |
| | | private void btnDel_Click(object sender, EventArgs e) |
| | | { |
| | | if (dataGridViewUM.SelectedRows.Count > 0) |
| | | { |
| | | // 获取选中的行 |
| | | DataGridViewRow selectedRow = dataGridViewUM.SelectedRows[0]; |
| | | |
| | | // 确认删除 |
| | | DialogResult result = MessageBox.Show("确定要删除选中的行吗?", |
| | | "确认删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question); |
| | | |
| | | if (result == DialogResult.Yes) |
| | | { |
| | | foreach (DataGridViewRow row in dataGridViewUM.SelectedRows) |
| | | try |
| | | { |
| | | dataGridViewUM.Rows.Remove(row); |
| | | // 先获取要删除的数据 |
| | | string employeeNumber = selectedRow.Cells[0].Value?.ToString(); |
| | | |
| | | if (!string.IsNullOrEmpty(employeeNumber)) |
| | | { |
| | | // 1. 从数据源删除 |
| | | var dataSource = dataGridViewUM.DataSource as List<RecordUserData>; |
| | | if (dataSource != null) |
| | | { |
| | | var itemToRemove = dataSource.FirstOrDefault(u => u.EmployeeNumber == employeeNumber); |
| | | if (itemToRemove != null) |
| | | { |
| | | dataSource.Remove(itemToRemove); |
| | | } |
| | | } |
| | | |
| | | // 2. 从用户管理器删除 |
| | | UserManager.Instance.DeleteUser(employeeNumber); |
| | | |
| | | // 3. 刷新数据绑定 |
| | | dataGridViewUM.DataSource = null; |
| | | dataGridViewUM.DataSource = dataSource; |
| | | |
| | | //MessageBox.Show("删除成功!", "提示", |
| | | // MessageBoxButtons.OK, MessageBoxIcon.Information); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | MessageBox.Show($"删除失败: {ex.Message}", "错误", |
| | | MessageBoxButtons.OK, MessageBoxIcon.Error); |
| | | } |
| | | } |
| | | } |
| | |
| | | MessageBoxButtons.OK, MessageBoxIcon.Information); |
| | | } |
| | | } |
| | | #endregion |
| | | private int editingRowIndex = -1; |
| | | private bool isEditingMode = false; |
| | | private string originalButtonText = "修改"; |
| | | #region 修改用户信息 |
| | | private void btnEdit_Click(object sender, EventArgs e) |
| | | { |
| | | // 单元格可编辑 |
| | |
| | | // 清空输入框 |
| | | ClearInputFields(); |
| | | |
| | | MessageBox.Show("修改完成!", "提示", |
| | | MessageBoxButtons.OK, MessageBoxIcon.Information); |
| | | RecordUserData user = new RecordUserData(); |
| | | user.EmployeeNumber = this.textBoxEmployeeID.Text; |
| | | user.EmployeeAccount = this.textBoxUsername.Text; |
| | | user.EmployeePassword = this.textBoxPassword.Text; |
| | | user.EmployeeName = this.textBoxName.Text; |
| | | user.EmployeePermission = (UserPermission)this.comboBoxPermission.SelectedIndex; |
| | | UserManager.Instance.UpdateUser(user); |
| | | |
| | | //MessageBox.Show("修改完成!", "提示", |
| | | // MessageBoxButtons.OK, MessageBoxIcon.Information); |
| | | } |
| | | } |
| | | |
| | |
| | | //textBoxPassword.PlaceholderText = "如需修改密码请填写"; |
| | | } |
| | | } |
| | | |
| | | private void UpdateRowData(int rowIndex) |
| | | { |
| | | if (rowIndex >= 0 && rowIndex < dataGridViewUM.Rows.Count) |
| | |
| | | row.Cells[3].Value = comboBoxPermission.SelectedItem.ToString(); |
| | | } |
| | | } |
| | | #endregion |
| | | |
| | | |
| | | #region 查询用户信息 |
| | | private void btnFind_Click(object sender, EventArgs e) |
| | | { |
| | | List<RecordUserData> recordUserDatas = UserManager.Instance.GetAllUsers(); |
| | | this.dataGridViewUM.DataSource = recordUserDatas; |
| | | this.dataGridViewUM.AutoGenerateColumns = true; |
| | | } |
| | | #endregion |
| | | |
| | | } |
| | | } |