动态GroupBox始终与顶部对齐

时间:2013-11-27 20:13:38

标签: c# winforms dynamic groupbox

我有这个应用程序,其中几个组框在垂直线上。它们可以单独启用和禁用。我希望始终将组合框对齐到顶部,无论是否正常位于其间的组都已启用。

假设我们有三个分组(1,2,3)。中间的一个(2)被禁用。通常,另外两个盒子(1,3)位置不会受此影响。但我希望最后一个groupbox(3)取代中间groupbox(2)通常所在的位置。

任何想法如何以最简单的方式实现这一目标?

这就是我现在所做的那些不足以进行缩放的内容

        if (isForgeIncluded == "True" | isForgeIncluded == "true")
        {
            forgeBox.Visible = true;
            if (headerPic == "False" | headerPic == "false")
            {
                PictureBox1.Visible = false;
                forgeBox.Location = new Point(6, 5);
                pathBox.Location = new Point(6, 112);
            }
            else
            {
            }
        }
        else
        {
            forgeBox.Visible = false;
            if (headerPic == "False" | headerPic == "false")
            {
                PictureBox1.Visible = false;
                pathBox.Location = new Point(6, 5);
            }
            else
            {
                pathBox.Location = new Point(6, 168);
            }

        }

2 个答案:

答案 0 :(得分:1)

在groupbox2禁用时将groupbox2位置(x,y)设置为groupbox3位置(x,y) - 可能来自特定事件。

答案 1 :(得分:0)

我设法找到了一个解决方案,这是“更动态”但不是100%动态,因为我将不得不手动将新的GroupBox添加到数组。但是,这确实有效,并在开始时处理图片框

int nextBoxLocation = 0;
int boxHeightAdd = 0;
int spacing = 6;

if (headerPic == "True" | headerPic == "true")
{
    PictureBox1.Visible = true;
    nextBoxLocation = PictureBox1.Height + spacing;
}
if (isForgeIncluded == "True" | isForgeIncluded == "true") 
{ 
    forgeBox.Visible = true; 
}

GroupBox[] boxes = {forgeBox , pathBox , typeBox};

foreach(GroupBox box in boxes) 
{
    if (box.Visible == true)
    {
         box.Location = new Point(6, nextBoxLocation);
         boxHeightAdd = box.Height;
    }
    if (box.Name == "pathBox" && (minecraftPathVar == Environment.ExpandEnvironmentVariables("C:\\Users\\" + Environment.UserName + "\\AppData\\Roaming\\.minecraft")))
    {
         defRadioPath.Visible = false;
         minecraftRadioPath.Visible = false;
         box.Height = 75;
         boxHeightAdd = 75;
    }
    nextBoxLocation += boxHeightAdd + spacing;
    boxHeightAdd = 0;
}