在C#中将控件放在面板中

时间:2012-03-20 22:41:46

标签: c#

private void newThumbNail(int docType, string fileName)
        {

            thmbNail[thmbNailCnt] = new GroupBox();
            thmbNail[thmbNailCnt].Parent = panel1;            
            thmbNail[thmbNailCnt].Visible = true;            
            thmbNail[thmbNailCnt].Location = new Point(2, 5);
            thmbNail[thmbNailCnt].Size = new Size(222, 50);



            picBox[thmbNailCnt] = new PictureBox();
            picBox[thmbNailCnt].Parent = thmbNail[thmbNailCnt];
            picBox[thmbNailCnt].Visible = true;
            picBox[thmbNailCnt].Location = new Point(6, 13);
            picBox[thmbNailCnt].Size = new Size(31, 31);
            //picBox[thmbNailCnt].Image = new Bitmap("images/excel.png");

            texBox[thmbNailCnt] = new TextBox();
            texBox[thmbNailCnt].Parent = thmbNail[thmbNailCnt];
            texBox[thmbNailCnt].Visible = true;
            texBox[thmbNailCnt].Location = new Point(53, 24);
            texBox[thmbNailCnt].Size = new Size(163, 20);
            texBox[thmbNailCnt].Text = fileName;
            texBox[thmbNailCnt].Enabled = false;

            Controls.Add(thmbNail[thmbNailCnt]);
            Controls.Add(picBox[thmbNailCnt]);
            Controls.Add(texBox[thmbNailCnt]);


        }

这是一个动态添加groupBox并在面板中添加一些控件的函数。不幸的是它没有出现在面板内。该面板是使用c#设计工具预先创建的。它直接放在窗户顶部15,52,大小为279,489。请帮助。

2 个答案:

答案 0 :(得分:3)

您似乎正在将这些控件添加到表单控件集合中 相反,您应该使用面板控件集合,如:

    panel1.Controls.Add(thmbNail[thmbNailCnt]); 
    panel1.Controls.Add(picBox[thmbNailCnt]); 
    panel1.Controls.Add(texBox[thmbNailCnt]); 

答案 1 :(得分:0)

尝试使用Panel.Controls.Add(thumbNail [thumbNail])

还提示让您的代码更快更容易阅读:

// Not modified to use Panel.Controls.Add()
GroupBox box = new GroouBox();
thmbNail[thmbNailCnt] = box;
box.Parent = panel1;            
box.Visible = true;            
box.Location = new Point(2, 5);
box.Size = new Size(222, 50);
相关问题