C# Visual Basic电源组
我正在使用visual studio 2010,我想在Windows窗体C#应用程序中从VB PowerPacks创建几个椭圆形,但我不想从工具箱中拖动它们,而是想手动创建它们,问题是如果我将它们声明为变量,它们将不会出现在窗体中,我如何使它们出现,谢谢 代码:C# Visual Basic电源组,c#,visual-studio-2010,windows-forms-designer,C#,Visual Studio 2010,Windows Forms Designer,我正在使用visual studio 2010,我想在Windows窗体C#应用程序中从VB PowerPacks创建几个椭圆形,但我不想从工具箱中拖动它们,而是想手动创建它们,问题是如果我将它们声明为变量,它们将不会出现在窗体中,我如何使它们出现,谢谢 代码: 你想要的是这样的: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System
你想要的是这样的:
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
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用Microsoft.VisualBasic.PowerPacks;
命名空间VBPowerPack
{
公共部分类Form1:Form
{
私有ShapeContainer ShapeContainer;//要放入表单中的容器
私有形状[]形状;//包含所有要显示的形状
公共表格1()
{
初始化组件();
shapes=新形状[5];//假设我们想要5种不同的形状
int-posY=0;
对于(int i=0;i<5;i++)
{
OvalShape OvalShape=新的OvalShape();//使用其属性创建所需的形状
椭圆形位置=新点(50,位置);
卵形尺寸=新尺寸(75,25);
shapes[i]=ovalShape;//将形状添加到数组中
posY+=30;
}
shapeContainer=新的shapeContainer();
shapeContainer.Shapes.AddRange(Shapes);//将形状数组添加到shapeContainer
this.Controls.Add(shapeContainer);//将shapeContainer添加到表单中
}
}
}
您是否忘记将它们添加为子控件?我假设您希望在设计时添加它们,而不是在运行时?使用表单设计器添加一行,然后打开form.designer.cs文件,复制/粘贴行并进行调整。不,我想在运行时添加行,非常感谢。。。
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
}
}
}