在AdjustToolStripDimensions方法中添加EnsureControlsTogether方法,确保lblRegionNumber和txtRegionNumber控件作为整体移动
This commit is contained in:
@@ -3971,6 +3971,7 @@ namespace JoyD.Windows.CS
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 根据toolStripContainer宽度动态调整toolStrip和TopToolStripPanel的尺寸
|
/// 根据toolStripContainer宽度动态调整toolStrip和TopToolStripPanel的尺寸
|
||||||
|
/// 确保当空间不足时,lblRegionNumber和txtRegionNumber作为整体移动到下一行
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void AdjustToolStripDimensions()
|
private void AdjustToolStripDimensions()
|
||||||
{
|
{
|
||||||
@@ -3985,21 +3986,36 @@ namespace JoyD.Windows.CS
|
|||||||
// 设置toolStrip的宽度与container一致
|
// 设置toolStrip的宽度与container一致
|
||||||
toolStrip.Width = containerWidth;
|
toolStrip.Width = containerWidth;
|
||||||
|
|
||||||
// 计算可见按钮的数量
|
// 计算可见项的数量(将lblRegionNumber和txtRegionNumber视为一个整体)
|
||||||
int visibleButtonCount = 0;
|
int visibleItemCount = 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控件作为整体移动
|
||||||
// 假设每个按钮和分隔符大约占用60像素宽度
|
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; // 按钮高度加上边距
|
||||||
@@ -4021,5 +4037,64 @@ 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user