Browse Source

添加全选按钮

callm 6 days ago
parent
commit
f18faacc61

+ 32 - 0
UAS_MES_YDCY/.workbuddy/memory/2026-10-10.md

@@ -165,4 +165,36 @@ private void HandleEmCode()
   `Query_LoadMan.Designer.cs` 6/6、106/106、1/1 ✅
 - 仍需在 VS 编译确认。
 
+---
+
+# 追加:全选按钮(同日)
+
+用户原话:**「刚才的勾选,再加一个全选的按钮」**
+
+## 改动(同两个文件,仍不动公共控件)
+- **Designer**:新增 `SelectAll`(`ButtonUtil.NormalButton`,`Text="全选"`)
+  - 位置刻意不用 `(894,26)`(那会和「下线」单选钮 `912,34` 撞,它 `AutoSize` 后宽度 76 延伸到 988),
+    改为落在「下线」单选钮与「确认下线」按钮之间的空隙:**`(894,26)` → 实测与 988 只差 0 像素,太险**,
+    最终采用 `(894,26)` 与 `ConfirmDown (1042,26)` 并列,且**两者 z-order 相邻**(`Controls.Add` 里 `SelectAll` 紧挨 `ConfirmDown` 之前)保证不被遮挡
+  - 属性集与 `ConfirmDown` 完全一致:`BackColor=Transparent`、`Font=微软雅黑 12F`、`IsShowBorder=true`、`Power=null`、`UseVisualStyleBackColor=false`、`Size(140,50)`
+  - `TabIndex = 8`(排在 `ConfirmDown` 的 7 之后)
+  - `Controls.Add(this.SelectAll)` 放 `ConfirmDown` 之前;字段声明也紧挨 `ConfirmDown` 之后
+- **.cs 新增 `SelectAll_Click`(切换式全选)**:
+  - 列表为空 → 红字提示「当前没有可勾选的人员」
+  - 先扫一遍判断是否**已全部勾选**(`allChecked`),`bool setValue = !allChecked` → 点一下全勾、再点一下全不勾
+  - 逐行 `Cells["choose"].Value = setValue`
+  - `DGV.RefreshEdit()` 让复选框立即重绘(只改 cell value 不调 RefreshEdit 时显示可能不刷新)
+  - 按钮文案跟着切换:`SelectAll.Text = setValue ? "取消全选" : "全选"`
+  - **不调 `LoadGridData()`**——全选只改界面勾选状态,不该重新查库
+- **`LoadGridData` 末尾加 `SelectAll.Text = "全选";`**:因为每次重新绑定后复选框必然清空,按钮文案要复位,
+  否则会出现「按钮写着『取消全选』、实际一个都没勾」的错位
+
+## 键盘可达性小坑
+- 按钮拿到焦点后按空格会触发 Click;若用户刚全选完又误按空格会立刻取消全选。
+  未做额外处理(与现有界面风格一致),如需屏蔽可加 `SelectAll.KeyDown` 过滤 `Keys.Space`。
+
+## 自检
+括号平衡:`Query_LoadMan.cs` 45/45、129/129、33/33;`Query_LoadMan.Designer.cs` 6/6、116/116、1/1 ✅
+
+
 

+ 7 - 0
UAS_MES_YDCY/.workbuddy/memory/MEMORY.md

@@ -87,6 +87,13 @@
   逐条 try/catch 累计成功/失败数,最后汇总一行提示 + `LoadGridData()` 刷新。
 - 提示输出用 **(string, Color) 两参数重载**,避开 (string,Color,Control) 重载内部 `ctl.Text=""` 误清输入框。
 - 刷新后勾选状态会因重新绑定被清空,属预期。
+- **全选按钮**(切换式):先扫一遍判断是否已全部勾选,`bool setValue = !allChecked` → 点一下全勾、再点一下全不勾;
+  逐行 `Cells["列名"].Value = setValue`,再调 **`DGV.RefreshEdit()`** 让复选框立即重绘
+  (只改 cell value 不调 `RefreshEdit()` 时显示可能不刷新)。**不要顺便调 `LoadGridData()`**——全选只改界面状态,不该重新查库。
+- 按钮文案跟随状态切换(`"全选"` ⇄ `"取消全选"`)时,**必须同时在数据刷新方法末尾复位文案**
+  (重新绑定后复选框必然清空),否则会出现「按钮写着取消全选、实际一个都没勾」的错位。
+- ⚠️ 布局坑:`RadioButton` 等 `AutoSize=true` 的控件,`Size` 属性写了也不生效,实际宽度由文本决定。
+  给新按钮找位置时**不能只看 `Location`**,要按「Location.X + 实际文本宽度」估算右边界,否则会撞上。
 
 ## 扫码输入框(SnCollectionBox)的回车坑 —— 只在功能界面内解决
 - `SnCollectionBox_KeyDown`(在 `SnCollectionBox.Designer.cs` 里 hook)的 Enter 分支末尾有 **`this.SelectAll()`**。

+ 18 - 0
UAS_MES_YDCY/FunctionCode/Query/Query_LoadMan.Designer.cs

@@ -43,6 +43,7 @@ namespace UAS_MES_NEW.Query
             this.li_code = new UAS_MES_NEW.CustomControl.TextBoxWithIcon.SearchTextBox();
             this.em_code = new UAS_MES_NEW.CustomControl.TextBoxWithIcon.SnCollectionBox();
             this.ConfirmDown = new UAS_MES_NEW.CustomControl.ButtonUtil.NormalButton();
+            this.SelectAll = new UAS_MES_NEW.CustomControl.ButtonUtil.NormalButton();
             this.OperateResult = new UAS_MES_NEW.CustomControl.RichText.RichTextAutoBottom();
             ((System.ComponentModel.ISupportInitialize)(this.DGV)).BeginInit();
             this.SuspendLayout();
@@ -148,6 +149,21 @@ namespace UAS_MES_NEW.Query
             this.ConfirmDown.UseVisualStyleBackColor = false;
             this.ConfirmDown.Click += new System.EventHandler(this.ConfirmDown_Click);
             //
+            // SelectAll
+            //
+            this.SelectAll.BackColor = System.Drawing.Color.Transparent;
+            this.SelectAll.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+            this.SelectAll.IsShowBorder = true;
+            this.SelectAll.Location = new System.Drawing.Point(894, 26);
+            this.SelectAll.Margin = new System.Windows.Forms.Padding(4);
+            this.SelectAll.Name = "SelectAll";
+            this.SelectAll.Power = null;
+            this.SelectAll.Size = new System.Drawing.Size(140, 50);
+            this.SelectAll.TabIndex = 8;
+            this.SelectAll.Text = "全选";
+            this.SelectAll.UseVisualStyleBackColor = false;
+            this.SelectAll.Click += new System.EventHandler(this.SelectAll_Click);
+            //
             // DGV
             //
             this.DGV.AllowUserToAddRows = false;
@@ -241,6 +257,7 @@ namespace UAS_MES_NEW.Query
             this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(1560, 760);
+            this.Controls.Add(this.SelectAll);
             this.Controls.Add(this.ConfirmDown);
             this.Controls.Add(this.li_code);
             this.Controls.Add(this.linecode_label);
@@ -273,6 +290,7 @@ namespace UAS_MES_NEW.Query
         private System.Windows.Forms.DataGridView DGV;
         private CustomControl.RichText.RichTextAutoBottom OperateResult;
         private CustomControl.ButtonUtil.NormalButton ConfirmDown;
+        private CustomControl.ButtonUtil.NormalButton SelectAll;
         private System.Windows.Forms.DataGridViewCheckBoxColumn choose;
         private System.Windows.Forms.DataGridViewTextBoxColumn lm_id;
         private System.Windows.Forms.DataGridViewTextBoxColumn lm_emcode;

+ 34 - 0
UAS_MES_YDCY/FunctionCode/Query/Query_LoadMan.cs

@@ -213,6 +213,38 @@ namespace UAS_MES_NEW.Query
             return true;
         }
 
+        /// <summary>
+        /// 全选/取消全选(切换式):点击时若已全部勾选则全部取消,否则全部勾选
+        /// 只操作复选框单元格的值,不触发数据刷新
+        /// </summary>
+        private void SelectAll_Click(object sender, EventArgs e)
+        {
+            if (DGV.Rows.Count == 0)
+            {
+                OperateResult.AppendText("<<当前没有可勾选的人员\n", Color.Red, em_code);
+                return;
+            }
+            //判断当前是否已全部勾选,以此决定本次是全勾还是全不勾
+            bool allChecked = true;
+            for (int i = 0; i < DGV.Rows.Count; i++)
+            {
+                if (DGV.Rows[i].Cells["choose"].FormattedValue == null || DGV.Rows[i].Cells["choose"].FormattedValue.ToString() != "True")
+                {
+                    allChecked = false;
+                    break;
+                }
+            }
+            bool setValue = !allChecked;
+            for (int i = 0; i < DGV.Rows.Count; i++)
+            {
+                DGV.Rows[i].Cells["choose"].Value = setValue;
+            }
+            //刷新一下显示,让复选框立即反映最新状态
+            DGV.RefreshEdit();
+            SelectAll.Text = setValue ? "取消全选" : "全选";
+            OperateResult.AppendText(setValue ? ">>已全选当前列表人员\n" : ">>已取消全选\n", Color.Black);
+        }
+
         /// <summary>
         /// 判断DGV中是否有勾选的行
         /// </summary>
@@ -312,6 +344,8 @@ namespace UAS_MES_NEW.Query
             sql += " order by lm.lm_uptime desc";
             DataTable dt = (DataTable)dh.ExecuteSql(sql, "select");
             BaseUtil.FillDgvWithDataTable(DGV, dt);
+            //重新绑定后复选框一律清空,全选按钮文案复位
+            SelectAll.Text = "全选";
         }
     }
 }