VS2010 中动态形式的奇怪行为

发布于 2024-10-28 13:36:55 字数 1857 浏览 1 评论 0原文

以下代码应该生成 5 个相同的组框元素,每个元素填充一个表单、一个按钮和一个进度条:

private void DrawControls()
    {
        for(int i=0; i<5; i++)
        {
            // define controls
            GroupBox gbxAttachmentName = new GroupBox();
            gbxAttachmentName.Text = "Datei.jpg";
            gbxAttachmentName.Font = new Font(gbxAttachmentName.Font, FontStyle.Bold);

            TextBox tbxAttachmentLabel = new TextBox();
            tbxAttachmentLabel.Text = "Bezeichnung";
            tbxAttachmentLabel.Font = new Font(tbxAttachmentLabel.Font, FontStyle.Regular);

            Button btnUploadAttachment = new Button();
            btnUploadAttachment.Text = "übertragen";
            btnUploadAttachment.Font = new Font(btnUploadAttachment.Font, FontStyle.Regular);

            ProgressBar pbUploadProgress = new ProgressBar();
            pbUploadProgress.Step = 1;                

            // position controls
            gbxAttachmentName.Size = new Size(500, 75);
            gbxAttachmentName.Location = new Point(10, 10 + (i * 85));

            tbxAttachmentLabel.Size = new Size(375, 20);
            tbxAttachmentLabel.Location = new Point(10, 20 + (i * 85));

            btnUploadAttachment.Size = new Size(100, 22);
            btnUploadAttachment.Location = new Point(390, 19 + (i * 85));

            pbUploadProgress.Size = new Size(480, 20);
            pbUploadProgress.Location = new Point(10, 45 + (i * 85));

            // add controls to groupbox
            gbxAttachmentName.Controls.Add(tbxAttachmentLabel);
            gbxAttachmentName.Controls.Add(pbUploadProgress);
            gbxAttachmentName.Controls.Add(btnUploadAttachment);

            // add groupbox to form
            flpMain.Controls.Add(gbxAttachmentName);
        }

    }

相反,我只得到 1 个正确填充的组框。另外4个是空的。

有人有解决方案吗?

提前致谢

Following code should produce 5 identical groupbox elements filled with each a form, a button and a progress bar:

private void DrawControls()
    {
        for(int i=0; i<5; i++)
        {
            // define controls
            GroupBox gbxAttachmentName = new GroupBox();
            gbxAttachmentName.Text = "Datei.jpg";
            gbxAttachmentName.Font = new Font(gbxAttachmentName.Font, FontStyle.Bold);

            TextBox tbxAttachmentLabel = new TextBox();
            tbxAttachmentLabel.Text = "Bezeichnung";
            tbxAttachmentLabel.Font = new Font(tbxAttachmentLabel.Font, FontStyle.Regular);

            Button btnUploadAttachment = new Button();
            btnUploadAttachment.Text = "übertragen";
            btnUploadAttachment.Font = new Font(btnUploadAttachment.Font, FontStyle.Regular);

            ProgressBar pbUploadProgress = new ProgressBar();
            pbUploadProgress.Step = 1;                

            // position controls
            gbxAttachmentName.Size = new Size(500, 75);
            gbxAttachmentName.Location = new Point(10, 10 + (i * 85));

            tbxAttachmentLabel.Size = new Size(375, 20);
            tbxAttachmentLabel.Location = new Point(10, 20 + (i * 85));

            btnUploadAttachment.Size = new Size(100, 22);
            btnUploadAttachment.Location = new Point(390, 19 + (i * 85));

            pbUploadProgress.Size = new Size(480, 20);
            pbUploadProgress.Location = new Point(10, 45 + (i * 85));

            // add controls to groupbox
            gbxAttachmentName.Controls.Add(tbxAttachmentLabel);
            gbxAttachmentName.Controls.Add(pbUploadProgress);
            gbxAttachmentName.Controls.Add(btnUploadAttachment);

            // add groupbox to form
            flpMain.Controls.Add(gbxAttachmentName);
        }

    }

Instead, I only get 1 Groupbox correctly filled. The other 4 are empty ones.

Has anyone a solution for this?

Thanks in advance

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

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

发布评论

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

评论(2

剑心龙吟 2024-11-04 13:36:56

控件实际上位于组框中,只是您看不到它们。您给了他们错误的位置。子控件位置与其父控件相关。修复:

            tbxAttachmentLabel.Location = new Point(10, 20);

并在其他修复中也修复此问题。

The controls are actually in the groupboxes, you just can't see them. You gave them the wrong Location. Child control locations are relative to their parent. Fix:

            tbxAttachmentLabel.Location = new Point(10, 20);

and fix this in the other ones as well.

迎风吟唱 2024-11-04 13:36:56

您还可以考虑重命名您的方法,因为它不会“绘制”控件,而是创建它们。

您还可以考虑使用初始值设定项语法。不仅更加简洁,也更好地展现了父子关系。

已更新

您还可以考虑使用锚点,以便控件可以随其父级一起调整大小。

private static void CreateControls(Control parent)
{
    int baseWidth = parent.ClientSize.Width - 20;

    for (int i = 0; i < 5; i++)
    {
        parent.Controls.Add(
            new GroupBox {
                Text = "Datei.jpg",
                Font = new Font(parent.Font, FontStyle.Bold),
                Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right,
                Size = new Size(baseWidth, 75),
                MinimumSize = new Size(175, -1),
                Location = new Point(10, 10 + (i * 85)),
                Controls = {
                    new TextBox {
                        Text = "Bezeichnung",
                        Font = new Font(parent.Font, FontStyle.Regular),
                        Anchor = AnchorStyles.Left | AnchorStyles.Right,
                        Size = new Size(baseWidth - 100 - 30, 20),
                        Location = new Point(10, 20), },
                    new Button {
                        Text = "übertragen",
                        Font = new Font(parent.Font, FontStyle.Regular),
                        Anchor = AnchorStyles.Right,
                        Size = new Size(100, 22),
                        Location = new Point(baseWidth - 100 - 10, 19), },
                    new ProgressBar {
                        Step = 1,
                        Font = new Font(parent.Font, FontStyle.Regular),
                        Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right,
                        Size = new Size(baseWidth - 20, 20),
                        Location = new Point(10, 45) },
                },
            }
            );
    }
}

You might also consider renaming your method as it does not "draw" controls, it creates them.

You might also consider using initializer syntax. Not only is it more concise, it shows the parent/child relationships better.

Updated

You might also consider using anchors so that the controls can resize along with their parent.

private static void CreateControls(Control parent)
{
    int baseWidth = parent.ClientSize.Width - 20;

    for (int i = 0; i < 5; i++)
    {
        parent.Controls.Add(
            new GroupBox {
                Text = "Datei.jpg",
                Font = new Font(parent.Font, FontStyle.Bold),
                Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right,
                Size = new Size(baseWidth, 75),
                MinimumSize = new Size(175, -1),
                Location = new Point(10, 10 + (i * 85)),
                Controls = {
                    new TextBox {
                        Text = "Bezeichnung",
                        Font = new Font(parent.Font, FontStyle.Regular),
                        Anchor = AnchorStyles.Left | AnchorStyles.Right,
                        Size = new Size(baseWidth - 100 - 30, 20),
                        Location = new Point(10, 20), },
                    new Button {
                        Text = "übertragen",
                        Font = new Font(parent.Font, FontStyle.Regular),
                        Anchor = AnchorStyles.Right,
                        Size = new Size(100, 22),
                        Location = new Point(baseWidth - 100 - 10, 19), },
                    new ProgressBar {
                        Step = 1,
                        Font = new Font(parent.Font, FontStyle.Regular),
                        Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right,
                        Size = new Size(baseWidth - 20, 20),
                        Location = new Point(10, 45) },
                },
            }
            );
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文