mrN mrN - 6 months ago 72
Vb.net Question

How to add a rectange shape to a windows form and display it?

I created a rectangle shape and tried to show it at my form like this

Imports Microsoft.VisualBasic.PowerPacks
Public Class frmBoard

Dim baseDice As RectangleShape

Private Sub frmBoard_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
baseDice.CornerRadius = 5
baseDice.Height = 50
baseDice.Width = 50
baseDice.BackColor = Color.Blue

Me.components.Add(baseDice)

End Sub
End Class


This didn't work. I missed something, but I dont know what...

Updated Code, after @Jay Riggs answer



Imports Microsoft.VisualBasic.PowerPacks
Public Class frmBoard

Dim baseDice As RectangleShape
Dim shapeContainer As ShapeContainer

Private Sub frmBoard_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
shapeContainer.Parent = Me
baseDice.Parent = shapeContainer
baseDice.CornerRadius = 5
baseDice.Height = 50
baseDice.Width = 50
baseDice.BackColor = Color.Blue
baseDice.Left = 50
baseDice.Top = 50
'Me.components.Add(baseDice)



End Sub
End Class


This also didn't work

Answer

The MSDN documentation for the RectangleShape Class demonstrates what you need to do: set the RectangleShape's Left and Top properties.

Private Sub DrawRectangle()
    Dim canvas As New Microsoft.VisualBasic.PowerPacks.ShapeContainer
    Dim rect1 As New Microsoft.VisualBasic.PowerPacks.RectangleShape
    ' Set the form as the parent of the ShapeContainer.
    canvas.Parent = Me
    ' Set the ShapeContainer as the parent of the RectangleShape.
    rect1.Parent = canvas
    ' Set the location and size of the rectangle.
    rect1.Left = 10
    rect1.Top = 10
    rect1.Width = 300
    rect1.Height = 100
End Sub

EDIT
Update your code to instantiate your ShapeContainer and RectangleShape objects:

Dim baseDice As RectangleShape = New RectangleShape
Dim shapeContainer As ShapeContainer = New ShapeContainer