VS2010中动态形式的奇怪行为

时间:2011-03-31 20:13:59

标签: visual-studio-2010 dynamic c#-4.0 groupbox

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

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个Groupbox。其他4个是空的。

有没有人可以解决这个问题?

提前致谢

2 个答案:

答案 0 :(得分:1)

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

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

并将其解决其他问题。

答案 1 :(得分:0)

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

您也可以考虑使用初始化程序语法。它不仅更简洁,而且更好地显示了父/子关系。

<强>更新

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

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) },
                },
            }
            );
    }
}