简化实现:删除lblRegionNumber控件,移除相关复杂逻辑,为txtRegionNumber添加工具提示

This commit is contained in:
zqm
2025-11-11 15:41:55 +08:00
parent 64adf95d84
commit 1b2163817b
2 changed files with 11 additions and 100 deletions

View File

@@ -201,25 +201,16 @@ namespace JoyD.Windows.CS
// //
// 确保在添加到toolStrip前初始化以下控件 // 确保在添加到toolStrip前初始化以下控件
// //
this.lblRegionNumber = new System.Windows.Forms.ToolStripLabel();
this.lblRegionNumber.Name = "lblRegionNumber";
this.lblRegionNumber.Size = new System.Drawing.Size(65, 22);
this.lblRegionNumber.Text = "区域编号:";
// 使用Padding属性调整文本垂直位置实现垂直居中效果
this.lblRegionNumber.Padding = new System.Windows.Forms.Padding(0, 3, 0, 0);
this.lblRegionNumber.TextDirection = System.Windows.Forms.ToolStripTextDirection.Horizontal;
// 设置非常小的右边距确保与txtRegionNumber紧密相连
this.lblRegionNumber.Margin = new System.Windows.Forms.Padding(0, 2, 1, 2);
// 确保控件不会被拆分到不同行
this.lblRegionNumber.AutoSize = true;
this.txtRegionNumber = new System.Windows.Forms.ToolStripTextBox(); this.txtRegionNumber = new System.Windows.Forms.ToolStripTextBox();
this.txtRegionNumber.Name = "txtRegionNumber"; this.txtRegionNumber.Name = "txtRegionNumber";
this.txtRegionNumber.Size = new System.Drawing.Size(60, 25); this.txtRegionNumber.Size = new System.Drawing.Size(60, 25);
this.txtRegionNumber.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TxtRegionNumber_KeyDown); this.txtRegionNumber.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TxtRegionNumber_KeyDown);
this.txtRegionNumber.TextChanged += new System.EventHandler(this.TxtRegionNumber_TextChanged); this.txtRegionNumber.TextChanged += new System.EventHandler(this.TxtRegionNumber_TextChanged);
// 设置非常小的左边距确保与lblRegionNumber紧密相连 // 设置合适的边距
this.txtRegionNumber.Margin = new System.Windows.Forms.Padding(1, 2, 3, 2); this.txtRegionNumber.Margin = new System.Windows.Forms.Padding(0, 2, 3, 2);
// 添加工具提示信息
this.txtRegionNumber.ToolTipText = "区域编号";
// 确保控件不会被拆分到不同行 // 确保控件不会被拆分到不同行
this.txtRegionNumber.AutoSize = true; this.txtRegionNumber.AutoSize = true;
// //
@@ -245,7 +236,6 @@ namespace JoyD.Windows.CS
this.btnBrushSize15, this.btnBrushSize15,
this.btnBrushSize25, this.btnBrushSize25,
new System.Windows.Forms.ToolStripSeparator(), new System.Windows.Forms.ToolStripSeparator(),
this.lblRegionNumber,
this.txtRegionNumber, this.txtRegionNumber,
new System.Windows.Forms.ToolStripSeparator(), new System.Windows.Forms.ToolStripSeparator(),
this.btnNewTempRegion, this.btnNewTempRegion,
@@ -510,7 +500,6 @@ namespace JoyD.Windows.CS
private System.Windows.Forms.ToolStripButton btnLoadTempDiff; private System.Windows.Forms.ToolStripButton btnLoadTempDiff;
private System.Windows.Forms.ToolStripButton btnSaveTempDiff; private System.Windows.Forms.ToolStripButton btnSaveTempDiff;
private System.Windows.Forms.DataGridView dataGridViewTempDiff; private System.Windows.Forms.DataGridView dataGridViewTempDiff;
private System.Windows.Forms.ToolStripLabel lblRegionNumber;
private System.Windows.Forms.ToolStripTextBox txtRegionNumber; private System.Windows.Forms.ToolStripTextBox txtRegionNumber;
} }
} }

View File

@@ -1070,7 +1070,6 @@ namespace JoyD.Windows.CS
btnLoadTempDiff.Visible = false; btnLoadTempDiff.Visible = false;
btnSaveTempDiff.Visible = false; btnSaveTempDiff.Visible = false;
// 显示区域编号设置控件 // 显示区域编号设置控件
lblRegionNumber.Visible = true;
txtRegionNumber.Visible = true; txtRegionNumber.Visible = true;
// 更新文本框的值为当前选中区域的编号 // 更新文本框的值为当前选中区域的编号
if (_selectedRegionIndex != -1) if (_selectedRegionIndex != -1)
@@ -1100,7 +1099,6 @@ namespace JoyD.Windows.CS
// 在初始状态下隐藏区域编号设置控件 // 在初始状态下隐藏区域编号设置控件
if (state == 0) if (state == 0)
{ {
lblRegionNumber.Visible = false;
txtRegionNumber.Visible = false; txtRegionNumber.Visible = false;
txtRegionNumber.Text = ""; // 清空文本框 txtRegionNumber.Text = ""; // 清空文本框
} }
@@ -3971,7 +3969,6 @@ namespace JoyD.Windows.CS
/// <summary> /// <summary>
/// 根据toolStripContainer宽度动态调整toolStrip和TopToolStripPanel的尺寸 /// 根据toolStripContainer宽度动态调整toolStrip和TopToolStripPanel的尺寸
/// 确保当空间不足时lblRegionNumber和txtRegionNumber作为整体移动到下一行
/// </summary> /// </summary>
private void AdjustToolStripDimensions() private void AdjustToolStripDimensions()
{ {
@@ -3986,36 +3983,20 @@ namespace JoyD.Windows.CS
// 设置toolStrip的宽度与container一致 // 设置toolStrip的宽度与container一致
toolStrip.Width = containerWidth; toolStrip.Width = containerWidth;
// 计算可见的数量将lblRegionNumber和txtRegionNumber视为一个整体 // 计算可见按钮的数量
int visibleItemCount = 0; int visibleButtonCount = 0;
bool countedRegionControls = false;
foreach (ToolStripItem item in toolStrip.Items) foreach (ToolStripItem item in toolStrip.Items)
{ {
if (item.Visible && !(item is ToolStripSeparator)) if (item.Visible && !(item is ToolStripSeparator))
{ {
// 检查是否是区域编号相关控件 visibleButtonCount++;
if ((item == lblRegionNumber || item == txtRegionNumber) && !countedRegionControls)
{
// 将两个控件计为一个整体
visibleItemCount++;
countedRegionControls = true;
}
else if (!(item == lblRegionNumber || item == txtRegionNumber))
{
// 其他控件正常计数
visibleItemCount++;
}
} }
} }
// 定义特殊处理确保lblRegionNumber和txtRegionNumber控件作为整体移动 // 根据可见按钮数量和容器宽度计算需要的行数
EnsureControlsTogether(); int buttonWidth = 25;
int buttonsPerRow = Math.Max(1, containerWidth / buttonWidth);
// 根据可见项数量和容器宽度计算需要的行数 int requiredRows = (int)Math.Ceiling((double)visibleButtonCount / buttonsPerRow);
int buttonWidth = 25; // 平均每项宽度
int itemsPerRow = Math.Max(1, containerWidth / buttonWidth);
int requiredRows = (int)Math.Ceiling((double)visibleItemCount / itemsPerRow);
// 设置TopToolStripPanel的最小高度确保有足够空间显示多行按钮 // 设置TopToolStripPanel的最小高度确保有足够空间显示多行按钮
int buttonHeight = 25; // 按钮高度加上边距 int buttonHeight = 25; // 按钮高度加上边距
@@ -4037,64 +4018,5 @@ namespace JoyD.Windows.CS
Console.WriteLine("调整toolStrip尺寸失败: " + ex.Message); Console.WriteLine("调整toolStrip尺寸失败: " + ex.Message);
} }
} }
/// <summary>
/// 确保lblRegionNumber和txtRegionNumber控件作为整体移动
/// 当空间不足时,两个控件一起移到下一行
/// </summary>
private void EnsureControlsTogether()
{
try
{
if (toolStrip != null && lblRegionNumber != null && txtRegionNumber != null &&
lblRegionNumber.Visible && txtRegionNumber.Visible)
{
// 确保两个控件紧密相连
// 查找两个控件在Items集合中的索引
int labelIndex = -1;
int textBoxIndex = -1;
for (int i = 0; i < toolStrip.Items.Count; i++)
{
if (toolStrip.Items[i] == lblRegionNumber)
labelIndex = i;
else if (toolStrip.Items[i] == txtRegionNumber)
textBoxIndex = i;
}
// 如果标签在文本框后面,交换它们的位置
if (labelIndex > textBoxIndex && labelIndex != -1 && textBoxIndex != -1)
{
// 保存当前位置和属性
int tempIndex = labelIndex;
// 先移除文本框
toolStrip.Items.RemoveAt(textBoxIndex);
// 再移除标签
toolStrip.Items.RemoveAt(tempIndex - 1); // 索引已改变
// 按正确顺序重新添加:先标签,后文本框
toolStrip.Items.Insert(tempIndex - 1, lblRegionNumber);
toolStrip.Items.Insert(tempIndex, txtRegionNumber);
}
// 设置紧凑的边距,确保它们紧密相连
lblRegionNumber.Margin = new Padding(0, 2, 1, 2); // 右边距很小
txtRegionNumber.Margin = new Padding(1, 2, 3, 2); // 左边距很小
// 确保两个控件都启用AutoSize
lblRegionNumber.AutoSize = true;
txtRegionNumber.AutoSize = true;
// 对于Flow布局我们可以通过设置一个特殊的布局引擎或自定义布局来实现控件作为整体移动
// 但在WinForms中最简单的方法是确保它们在Items集合中是连续的并且使用紧凑的边距
// 这样当空间不足时,它们会作为一个整体被推到下一行
}
}
catch (Exception ex)
{
Console.WriteLine("确保控件一起移动失败: " + ex.Message);
}
}
} }
} }