0

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!

user3740891
  • 33
  • 2
  • 4
  • 11
  • which version of .net you are using ? – H. Mahida Jun 30 '14 at 12:52
  • Controls are always added programmatically. If you want to find out how the designer does it, so you'll have an idea how to do it yourself, then just look at the code it generates. Click the "Show All Files" toolbar button in the Solution Explorer window and open the form's Designer.vb file. – Hans Passant Jun 30 '14 at 15:16

1 Answers1

0

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

Forrest4096
  • 159
  • 1
  • 8