From 42c97b287337a89e299c4c88b04287019a3e7453 Mon Sep 17 00:00:00 2001 From: zqm Date: Mon, 10 Nov 2025 10:00:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E6=B8=A9=E5=B7=AE=E5=9B=BE=E4=BE=8B?= =?UTF-8?q?=E8=A1=A8=E4=B8=AD=E6=B8=A9=E5=B7=AE=E5=80=BC=E5=88=97=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E4=B8=BA=E5=8F=AF=E7=BC=96=E8=BE=91=E6=95=B0=E5=AD=97?= =?UTF-8?q?=E6=A1=86=EF=BC=8C=E5=B9=B6=E5=AE=9E=E7=8E=B00.1=E5=BA=A6?= =?UTF-8?q?=E5=A2=9E=E9=87=8F=E5=92=8C0-100=E5=BA=A6=E8=8C=83=E5=9B=B4?= =?UTF-8?q?=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CS/Framework4.0/Toprie/Toprie/Setting.cs | 118 +++++++++++++++++- 1 file changed, 116 insertions(+), 2 deletions(-) diff --git a/Windows/CS/Framework4.0/Toprie/Toprie/Setting.cs b/Windows/CS/Framework4.0/Toprie/Toprie/Setting.cs index 0d1e763..8358f48 100644 --- a/Windows/CS/Framework4.0/Toprie/Toprie/Setting.cs +++ b/Windows/CS/Framework4.0/Toprie/Toprie/Setting.cs @@ -103,7 +103,8 @@ namespace JoyD.Windows.CS Name = "tempDiffValue", HeaderText = "温差值", Width = 80, - ReadOnly = true + ReadOnly = false, // 设置为可编辑 + DefaultCellStyle = new DataGridViewCellStyle { Alignment = DataGridViewContentAlignment.MiddleCenter } }; // 添加列:颜色 @@ -123,8 +124,14 @@ namespace JoyD.Windows.CS // 添加单元格绘制事件用于显示颜色块 dataGridViewTempDiff.CellPainting += new DataGridViewCellPaintingEventHandler(DataGridViewTempDiff_CellPainting); - // 添加点击事件用于选择颜色 + // 添加点击事件用于选择颜色和处理温差值列 dataGridViewTempDiff.CellClick += new DataGridViewCellEventHandler(DataGridViewTempDiff_CellClick); + // 添加单元格验证事件用于验证温差值输入 + dataGridViewTempDiff.CellValidating += new DataGridViewCellValidatingEventHandler(DataGridViewTempDiff_CellValidating); + // 添加单元格格式化事件用于显示摄氏度符号 + dataGridViewTempDiff.CellFormatting += new DataGridViewCellFormattingEventHandler(DataGridViewTempDiff_CellFormatting); + // 添加编辑控件显示事件用于设置编辑控件属性 + dataGridViewTempDiff.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(DataGridViewTempDiff_EditingControlShowing); // 添加一些示例数据 AddSampleTempDiffData(); @@ -156,6 +163,108 @@ namespace JoyD.Windows.CS dataGridViewTempDiff.Rows.Add(tempDiffValue, ""); } + private void DataGridViewTempDiff_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) + { + // 检查是否是温差值列 + if (dataGridViewTempDiff.CurrentCell.ColumnIndex == dataGridViewTempDiff.Columns["tempDiffValue"].Index) + { + // 获取TextBox编辑控件 + TextBox tb = e.Control as TextBox; + if (tb != null) + { + // 清除之前的处理程序 + tb.KeyPress -= new KeyPressEventHandler(TempDiffValueTextBox_KeyPress); + // 添加新的KeyPress处理程序 + tb.KeyPress += new KeyPressEventHandler(TempDiffValueTextBox_KeyPress); + + // 去掉°C符号,只保留数值部分 + string currentValue = dataGridViewTempDiff.CurrentCell.Value.ToString(); + if (currentValue.Contains("°C")) + { + tb.Text = currentValue.Replace("°C", ""); + tb.SelectionStart = tb.Text.Length; + } + } + } + } + + private void TempDiffValueTextBox_KeyPress(object sender, KeyPressEventArgs e) + { + // 允许输入数字、小数点和退格键 + if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.') + { + e.Handled = true; + } + + // 只允许一个小数点 + TextBox tb = sender as TextBox; + if (e.KeyChar == '.' && tb != null && tb.Text.Contains(".")) + { + e.Handled = true; + } + + // 检查小数点后最多一位 + if (tb != null && tb.Text.Contains(".") && + tb.Text.Substring(tb.Text.IndexOf(".")).Length > 1 && + !char.IsControl(e.KeyChar)) + { + e.Handled = true; + } + } + + private void DataGridViewTempDiff_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) + { + // 只验证温差值列 + if (e.ColumnIndex == dataGridViewTempDiff.Columns["tempDiffValue"].Index && e.RowIndex >= 0) + { + string value = e.FormattedValue.ToString().Trim(); + + // 尝试解析输入为浮点数 + if (!float.TryParse(value, out float tempValue)) + { + dataGridViewTempDiff.Rows[e.RowIndex].ErrorText = "请输入有效的温度值"; + e.Cancel = true; + return; + } + + // 验证温度范围(0-100度) + if (tempValue < 0 || tempValue > 100) + { + dataGridViewTempDiff.Rows[e.RowIndex].ErrorText = "温度值必须在0到100度之间"; + e.Cancel = true; + return; + } + + // 清除错误信息 + dataGridViewTempDiff.Rows[e.RowIndex].ErrorText = string.Empty; + + // 更新数据模型 + if (e.RowIndex < tempDiffData.Count) + { + tempDiffData[e.RowIndex]["tempDiffValue"] = $"{tempValue}°C"; + } + } + } + + private void DataGridViewTempDiff_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) + { + // 只处理温差值列 + if (e.ColumnIndex == dataGridViewTempDiff.Columns["tempDiffValue"].Index && e.RowIndex >= 0) + { + // 确保值不为空 + if (e.Value != null && !string.IsNullOrEmpty(e.Value.ToString())) + { + string value = e.Value.ToString(); + // 确保值包含°C符号 + if (!value.Contains("°C")) + { + e.Value = $"{value}°C"; + e.FormattingApplied = true; + } + } + } + } + private void DataGridViewTempDiff_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { // 只处理颜色列 @@ -217,6 +326,11 @@ namespace JoyD.Windows.CS } } } + // 处理温差值列点击 - 当点击温差值列时,启动编辑模式 + else if (e.ColumnIndex == dataGridViewTempDiff.Columns["tempDiffValue"].Index) + { + dataGridViewTempDiff.BeginEdit(true); + } } }