2012-03-20 13 views
0
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。请帮助。将控件放置在面板中的C#

回答

3

看来你正在将这些控件添加到表单控件集合中。
相反,你应该使用面板控件集合,如:

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

尝试Panel.Controls.Add(thmbNail [thmbNailCnt])

另外一个提示,使您的代码运行得更快,更易于阅读:

// 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);