vb.netwinformsvb-power-pack

Add LineShape programatically in VB.NET WinForms


How do you add a LineShape programmatically in VB.NET WinForms?

I'm looking to write something like you would for a Label , CheckBox or whatever else:

Dim somelabel as New Label
somelabel.Text = "Whatever"
somelabel.Location = New Point(200, 200)
Me.Controls.Add(somelabel)

Etc.

My purpose is to create thin dividing lines between the rows and columns of 16 Labels that form a 4x4 grid.

I appreciate that, since LineShape is a part of VB PowerPacks, this may present some difficulties, such as having to use Imports ... or, if really necessary, import a .dll. But I'd like to see all your ideas/solutions!


Solution

  • First, import the powerpacks namespace to give you access to the control:

    Imports Microsoft.VisualBasic.PowerPacks
    

    Then you could do it like this:

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

    Where startx = the x starting position, starty = the y starting position, endx = the ending x position and endy = the ending y position. If you want to put it into a canvas, simply:

    Dim yourcanvas As ShapeContainer
    canvas.Parent = formName
    yourline.Parent = canvas
    

    For more information and an API reference, go to: http://msdn.microsoft.com/en-us/library/bb918067.aspx