如何在彼此之下有序地生成控件?

发布于 2024-09-06 17:04:19 字数 875 浏览 5 评论 0原文

我有一个生成表单和控件的类。控件有按钮、单选控件、复选框和标签等。这个想法是整齐地创建表单,并在每个控件上方添加一个标签。问题是我似乎找不到公式或方法来整齐地组织/间隔它们。当我只有文本框时它工作得很好,但我不确定如何处理更大的控件,例如复选框列表。下面是我如何处理文本框的示例:

            case "Text":
                TextBox tbx = new TextBox();
                tbx.Name = df.Value.Name;
                tbx.Text = (df.Value.DefValue != null) ? df.Value.DefValue : "";
                tbx.Location = new Point(lbl.Location.X, lbl.Location.Y + 20);
                f.Controls.Add(tbx);
                break;

请注意,这一切都在 foreach 循环中。这是它前面的部分(标签):

        if (i == 0)
        {
            lbl.Location = new Point(10, 10);
        }
        else
        {
            lbl.Location = new Point(10, (i * 50) + 10);
        }

这会以均匀的间距整齐地对文本框和标签进行排序。谁能给我一些关于如何处理不同控件的建议?我想将它们放置在彼此下方,但从每个控件的底部到下一个标签的顶部至少保持 10 像素的间距。

先感谢您。

I have a class which generates a form and controls. The controls vary between buttons, radio controls, check boxes and labels. The idea is to neatly create the form with a label above each control. The problem is that I cannot seem to find the formula or way to neatly organise/space them. It works fine when I have just text boxes, but I'm not sure how to handle larger controls, like check box lists. Here's an example of how I handle text boxes:

            case "Text":
                TextBox tbx = new TextBox();
                tbx.Name = df.Value.Name;
                tbx.Text = (df.Value.DefValue != null) ? df.Value.DefValue : "";
                tbx.Location = new Point(lbl.Location.X, lbl.Location.Y + 20);
                f.Controls.Add(tbx);
                break;

Mind that this is all in a foreach loop. This is the part that precedes it (label):

        if (i == 0)
        {
            lbl.Location = new Point(10, 10);
        }
        else
        {
            lbl.Location = new Point(10, (i * 50) + 10);
        }

This neatly sorts the text boxes and labels out with an even spacing. Can anyone offer me some advice on how to handle different controls? I want to place them underneath eachother but keep at least 10 pixels spacing from the bottom of each control to the top of the next label.

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞笑我太脆弱 2024-09-13 17:04:19

使用表格布局。你可以在代码隐藏文件中看看设计者是如何做到的。

对于间距,请填写控件的“边距”属性。顶部 5 个和底部 5 个应该可以。

Use a tablelayout. You can take a look at how the designer does it in the code behind file.

For the spacing, fill in the "margin" property of your controls. 5 on top and 5 at the bottom should do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文