1

Looking at question/response 24490437 I see that one can programmatically create a line shape and apply it to a shape container. However the solution there does not allow one to place a new line shape on an existing panel. How would one place a new line shape on an existing panel(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

Here is my attempt at coding this:

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
bartj
  • 27
  • 6
  • Give us the link, would ya? yourcanvas isn't anything. Is canvas supposed to be yourcanvas? Is formName a different form? – LarsTech May 04 '17 at 17:38
  • http://stackoverflow.com/questions/24490437/add-lineshape-programatically-in-vb-net-winforms/24504322 – bartj May 04 '17 at 20:19
  • I showed the example from the link in my question. I want to simply place the newly created "yourline" on panel1 which exists and whose parent is frmbig. – bartj May 04 '17 at 20:21
  • Is panel1 a ShapeContainer? Try posting "your" code if you want help with this. – LarsTech May 04 '17 at 21:10
  • I would suggest that you start by doing it in the designer and then looking at the code the designer generates. You can then replicate that code. To access the designer code file, open the Solution Explorer, click the 'Show All Files' button and then expand the node for your form. – jmcilhinney May 05 '17 at 00:12
  • As for the code you posted, have you actually created a `ShapeContainer`? The code you've basically copied from the other thread declares a variable of that type but doesn't create an object. If there is no existing `ShapeContainer` then you have to create one. – jmcilhinney May 05 '17 at 00:14
  • I am wanting to use the existing "Panel1" on the form as a shape container. Note that I can manually place line shapes on this panel in design mode. I want to add more programmmatically. Here is my attempt at coding it: – bartj May 05 '17 at 16:13
  • ' 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 – bartj May 05 '17 at 16:14
  • Put the code in your question. Edit link is underneath it. Your error is self explanatory. – LarsTech May 05 '17 at 16:16

1 Answers1

1

You have to add a 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)
LarsTech
  • 80,625
  • 14
  • 153
  • 225