2017-05-04 38 views
1

查看問題/回覆24490437我看到人們可以通過編程創建線條形狀並將其應用於形狀容器。但是,那裏的解決方案不允許在現有面板上放置新的線條形狀。如何在現有面板上放置新的線條形狀(vb.net winforms)?如何在vb.net winforms中以編程方式向現有面板添加powerpack線型?

' 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 
+0

給我們的鏈接,會吧? yourcanvas不是什麼。畫布應該是你的畫布嗎? formName是不同的形式嗎? – LarsTech

+0

http://stackoverflow.com/questions/24490437/add-lineshape-programatically-in-vb-net-winforms/24504322 – bartj

+0

我在我的問題中顯示了鏈接示例。我想簡單地將新創建的「yourline」放置在存在的panel1上,並且其父項是frmbig。 – bartj

回答

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) 
+0

謝謝。你的解決方案有效 – bartj

相關問題