将温差图例表中温差值列设置为可编辑数字框,并实现0.1度增量和0-100度范围限制
This commit is contained in:
@@ -103,7 +103,8 @@ namespace JoyD.Windows.CS
|
|||||||
Name = "tempDiffValue",
|
Name = "tempDiffValue",
|
||||||
HeaderText = "温差值",
|
HeaderText = "温差值",
|
||||||
Width = 80,
|
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.CellPainting += new DataGridViewCellPaintingEventHandler(DataGridViewTempDiff_CellPainting);
|
||||||
// 添加点击事件用于选择颜色
|
// 添加点击事件用于选择颜色和处理温差值列
|
||||||
dataGridViewTempDiff.CellClick += new DataGridViewCellEventHandler(DataGridViewTempDiff_CellClick);
|
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();
|
AddSampleTempDiffData();
|
||||||
@@ -156,6 +163,108 @@ namespace JoyD.Windows.CS
|
|||||||
dataGridViewTempDiff.Rows.Add(tempDiffValue, "");
|
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)
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user