2011-11-26 92 views
2

我正在使用Visual Studio 2010,我想在Windows Form C#应用程序中从VB PowerPack创建几个OvalShapes,但我不想将它们从工具箱中拖出来,而是想手动创建它们,问题是,如果我宣布他们为变量,它们不会出现在表单,我怎样才能使他们出现,谢谢...Visual Basic电源包

代码:

using System; 
using System.Collections.Generic; 
System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using Microsoft.VisualBasic.PowerPacks; 
using System.Windows.Forms; 

namespace VB_PP 
{ 
    public partial class Form1 : Form 
    { 
    OvalShape[] OS_Arr; 
    public Form1() 
    { 
    InitializeComponent(); 
    OS_Arr = new OvalShape[15]; //I will do some coding on the array of those OvalShapes,like move them with a Timer... 
    } 
    } 
} 
+1

哟哟你忘了添加他们作为一个孩子的控制?我假设你想在设计时添加它们,而不是运行时?使用表单设计器添加一个,然后打开form.designer.cs文件并复制/粘贴这些行并进行调整。 – Mario

+0

不,我想在运行时添加它们,非常感谢... – YMELS

回答

3

你想要什么东西像这样:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using Microsoft.VisualBasic.PowerPacks; 

namespace VBPowerPack 
{ 
    public partial class Form1 : Form 
    { 
     private ShapeContainer shapeContainer; //Container that you're gonna place into your form 
     private Shape[] shapes;     //Contains all the shapes you wanna display 

     public Form1() 
     { 
      InitializeComponent(); 

      shapes = new Shape[5];    //Let's say we want 5 different shapes 

      int posY = 0; 
      for (int i = 0; i < 5; i++) 
      { 
       OvalShape ovalShape = new OvalShape();  //Create the shape you want with it's properties 
       ovalShape.Location = new Point(50, posY); 
       ovalShape.Size = new Size(75, 25); 
       shapes[i] = ovalShape;      //Add the shape to the array 

       posY += 30; 
      } 

      shapeContainer = new ShapeContainer(); 
      shapeContainer.Shapes.AddRange(shapes);   //Add the array of shapes to the ShapeContainer 
      this.Controls.Add(shapeContainer);    //Add the ShapeContainer to your form 
     } 
    } 
}