简化实现:删除lblRegionNumber控件,移除相关复杂逻辑,为txtRegionNumber添加工具提示
This commit is contained in:
@@ -201,25 +201,16 @@ namespace JoyD.Windows.CS
|
||||
//
|
||||
// 确保在添加到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.Name = "txtRegionNumber";
|
||||
this.txtRegionNumber.Size = new System.Drawing.Size(60, 25);
|
||||
this.txtRegionNumber.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TxtRegionNumber_KeyDown);
|
||||
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;
|
||||
//
|
||||
@@ -245,7 +236,6 @@ namespace JoyD.Windows.CS
|
||||
this.btnBrushSize15,
|
||||
this.btnBrushSize25,
|
||||
new System.Windows.Forms.ToolStripSeparator(),
|
||||
this.lblRegionNumber,
|
||||
this.txtRegionNumber,
|
||||
new System.Windows.Forms.ToolStripSeparator(),
|
||||
this.btnNewTempRegion,
|
||||
@@ -510,7 +500,6 @@ namespace JoyD.Windows.CS
|
||||
private System.Windows.Forms.ToolStripButton btnLoadTempDiff;
|
||||
private System.Windows.Forms.ToolStripButton btnSaveTempDiff;
|
||||
private System.Windows.Forms.DataGridView dataGridViewTempDiff;
|
||||
private System.Windows.Forms.ToolStripLabel lblRegionNumber;
|
||||
private System.Windows.Forms.ToolStripTextBox txtRegionNumber;
|
||||
}
|
||||
}
|
||||
@@ -1070,7 +1070,6 @@ namespace JoyD.Windows.CS
|
||||
btnLoadTempDiff.Visible = false;
|
||||
btnSaveTempDiff.Visible = false;
|
||||
// 显示区域编号设置控件
|
||||
lblRegionNumber.Visible = true;
|
||||
txtRegionNumber.Visible = true;
|
||||
// 更新文本框的值为当前选中区域的编号
|
||||
if (_selectedRegionIndex != -1)
|
||||
@@ -1100,7 +1099,6 @@ namespace JoyD.Windows.CS
|
||||
// 在初始状态下隐藏区域编号设置控件
|
||||
if (state == 0)
|
||||
{
|
||||
lblRegionNumber.Visible = false;
|
||||
txtRegionNumber.Visible = false;
|
||||
txtRegionNumber.Text = ""; // 清空文本框
|
||||
}
|
||||
@@ -3971,7 +3969,6 @@ namespace JoyD.Windows.CS
|
||||
|
||||
/// <summary>
|
||||
/// 根据toolStripContainer宽度动态调整toolStrip和TopToolStripPanel的尺寸
|
||||
/// 确保当空间不足时,lblRegionNumber和txtRegionNumber作为整体移动到下一行
|
||||
/// </summary>
|
||||
private void AdjustToolStripDimensions()
|
||||
{
|
||||
@@ -3986,36 +3983,20 @@ namespace JoyD.Windows.CS
|
||||
// 设置toolStrip的宽度与container一致
|
||||
toolStrip.Width = containerWidth;
|
||||
|
||||
// 计算可见项的数量(将lblRegionNumber和txtRegionNumber视为一个整体)
|
||||
int visibleItemCount = 0;
|
||||
bool countedRegionControls = false;
|
||||
|
||||
// 计算可见按钮的数量
|
||||
int visibleButtonCount = 0;
|
||||
foreach (ToolStripItem item in toolStrip.Items)
|
||||
{
|
||||
if (item.Visible && !(item is ToolStripSeparator))
|
||||
{
|
||||
// 检查是否是区域编号相关控件
|
||||
if ((item == lblRegionNumber || item == txtRegionNumber) && !countedRegionControls)
|
||||
{
|
||||
// 将两个控件计为一个整体
|
||||
visibleItemCount++;
|
||||
countedRegionControls = true;
|
||||
}
|
||||
else if (!(item == lblRegionNumber || item == txtRegionNumber))
|
||||
{
|
||||
// 其他控件正常计数
|
||||
visibleItemCount++;
|
||||
}
|
||||
visibleButtonCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// 定义特殊处理,确保lblRegionNumber和txtRegionNumber控件作为整体移动
|
||||
EnsureControlsTogether();
|
||||
|
||||
// 根据可见项数量和容器宽度计算需要的行数
|
||||
int buttonWidth = 25; // 平均每项宽度
|
||||
int itemsPerRow = Math.Max(1, containerWidth / buttonWidth);
|
||||
int requiredRows = (int)Math.Ceiling((double)visibleItemCount / itemsPerRow);
|
||||
// 根据可见按钮数量和容器宽度计算需要的行数
|
||||
int buttonWidth = 25;
|
||||
int buttonsPerRow = Math.Max(1, containerWidth / buttonWidth);
|
||||
int requiredRows = (int)Math.Ceiling((double)visibleButtonCount / buttonsPerRow);
|
||||
|
||||
// 设置TopToolStripPanel的最小高度,确保有足够空间显示多行按钮
|
||||
int buttonHeight = 25; // 按钮高度加上边距
|
||||
@@ -4037,64 +4018,5 @@ namespace JoyD.Windows.CS
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user