修改温差图例管理相关方法,确保添加和修改图例时温度和颜色都没有重复
This commit is contained in:
@@ -613,13 +613,22 @@ namespace JoyD.Windows.CS
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查温度值是否已存在(排除当前正在编辑的行)
|
||||
string tempString = $"{tempValue}°C";
|
||||
if (IsTempValueExists(tempString, e.RowIndex))
|
||||
{
|
||||
dataGridViewTempDiff.Rows[e.RowIndex].ErrorText = "该温度值已存在";
|
||||
e.Cancel = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// 清除错误信息
|
||||
dataGridViewTempDiff.Rows[e.RowIndex].ErrorText = string.Empty;
|
||||
|
||||
// 更新数据模型
|
||||
if (e.RowIndex < tempDiffData.Count)
|
||||
{
|
||||
tempDiffData[e.RowIndex]["tempDiffValue"] = $"{tempValue}°C";
|
||||
tempDiffData[e.RowIndex]["tempDiffValue"] = tempString;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -804,6 +813,14 @@ namespace JoyD.Windows.CS
|
||||
if (colorDialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
Color newColor = colorDialog.Color;
|
||||
|
||||
// 检查颜色是否已存在(排除当前正在编辑的行)
|
||||
if (IsColorExists(newColor, e.RowIndex))
|
||||
{
|
||||
MessageBox.Show("该颜色已存在,请选择其他颜色", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
return;
|
||||
}
|
||||
|
||||
// 更新颜色数据
|
||||
tempDiffData[e.RowIndex]["color"] = newColor;
|
||||
// 刷新单元格以显示新颜色
|
||||
@@ -1199,10 +1216,27 @@ namespace JoyD.Windows.CS
|
||||
{
|
||||
try
|
||||
{
|
||||
// 添加新的温差图例,提供默认值和颜色
|
||||
int defaultTemp = tempDiffData.Count > 0 ? tempDiffData.Count * 10 : 10;
|
||||
Color defaultColor = GetNextDefaultColor(tempDiffData.Count);
|
||||
AddTempDiffRow($"{defaultTemp}°C", defaultColor);
|
||||
// 查找可用的温度值,确保不重复
|
||||
int tempValue = 10;
|
||||
while (true)
|
||||
{
|
||||
string tempString = $"{tempValue}°C";
|
||||
if (!IsTempValueExists(tempString))
|
||||
break;
|
||||
tempValue += 10;
|
||||
}
|
||||
|
||||
// 查找可用的颜色,确保不重复
|
||||
int colorIndex = 0;
|
||||
Color defaultColor;
|
||||
do
|
||||
{
|
||||
defaultColor = GetNextDefaultColor(colorIndex);
|
||||
colorIndex++;
|
||||
} while (IsColorExists(defaultColor));
|
||||
|
||||
// 添加新的温差图例
|
||||
AddTempDiffRow($"{tempValue}°C", defaultColor);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -1210,6 +1244,46 @@ namespace JoyD.Windows.CS
|
||||
}
|
||||
}
|
||||
|
||||
// 检查温度值是否已存在(忽略单位,只比较数值部分)
|
||||
private bool IsTempValueExists(string tempValue, int excludeRowIndex = -1)
|
||||
{
|
||||
// 提取数值部分
|
||||
string numericValue = System.Text.RegularExpressions.Regex.Replace(tempValue, @"[^0-9.-]", "");
|
||||
if (!float.TryParse(numericValue, out float currentTemp))
|
||||
return false;
|
||||
|
||||
// 遍历所有数据行进行比较
|
||||
for (int i = 0; i < tempDiffData.Count; i++)
|
||||
{
|
||||
// 跳过被排除的行(用于修改操作)
|
||||
if (i == excludeRowIndex) continue;
|
||||
|
||||
string existingTempValue = tempDiffData[i]["tempDiffValue"].ToString();
|
||||
string existingNumericValue = System.Text.RegularExpressions.Regex.Replace(existingTempValue, @"[^0-9.-]", "");
|
||||
if (float.TryParse(existingNumericValue, out float existingTemp))
|
||||
{
|
||||
if (Math.Abs(currentTemp - existingTemp) < 0.001) // 浮点数比较,使用容差
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查颜色是否已存在
|
||||
private bool IsColorExists(Color color, int excludeRowIndex = -1)
|
||||
{
|
||||
for (int i = 0; i < tempDiffData.Count; i++)
|
||||
{
|
||||
// 跳过被排除的行(用于修改操作)
|
||||
if (i == excludeRowIndex) continue;
|
||||
|
||||
Color existingColor = (Color)tempDiffData[i]["color"];
|
||||
if (existingColor.ToArgb() == color.ToArgb())
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 获取下一个默认颜色
|
||||
private Color GetNextDefaultColor(int index)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user