如何在vb.net winforms中以编程方式将powerpack linehape添加到现有面板?

时间:2017-05-04 17:33:13

标签: vb.net winforms

查看问题/回复24490437我看到可以通过编程方式创建线形并将其应用于形状容器。然而,那里的解决方案不允许在现有面板上放置新的线形。如何在现有面板上放置新的线条形状(vb.net winforms)?

    ' example solution from 24490437
    Dim startx As Integer = 0
    Dim starty As Integer = 0
    Dim endx As Integer = 100
    Dim endy As Integer = 100
    Dim yourline As New LineShape(startx, starty, endx, endy)

    ' this section places "yournewline" on a canvas.
    Dim yourcanvas As ShapeContainer
    canvas.Parent = formName
    yourline.Parent = canvas

这是我尝试编码:

Private Sub frmbig_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    ' define a new line shape
    Dim startx As Integer = 0
    Dim starty As Integer = 0
    Dim endx As Integer = 100
    Dim endy As Integer = 100
    Dim yourline As New LineShape(startx, starty, endx, endy)

    ' attempt to place the line shape on the existing panel1 which is on frmbig
    yourline.Parent = Panel1
    ' !! blue underline under Panel1 when highlighted states:
    ' Value of type 'System.Windows.Forms.Panel' cannot be converted to 
    ' 'Microsoft.VisualBasic.PowerPacks.ShapeContainer'.

End Sub

1 个答案:

答案 0 :(得分:1)

您必须添加ShapeContainer:

Dim startx As Integer = 0
Dim starty As Integer = 0
Dim endx As Integer = 100
Dim endy As Integer = 100
Dim yourline As New LineShape(startx, starty, endx, endy)

Dim sc As New ShapeContainer
yourline.Parent = sc

Panel1.Controls.Add(sc)