mrN
mrN

Reputation: 3774

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

Upvotes: 1

Views: 14347

Answers (2)

Juan Carlos Garrido
Juan Carlos Garrido

Reputation: 1

This works

Imports Microsoft.VisualBasic.PowerPacks Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim C As New ShapeContainer
    Dim R As New RectangleShape
    C.Parent = Me
    R.Parent = C
    R.Name = "SHR1"
    R.Width = 100
    R.Height = 100
    R.Top = 10
    R.Left = 10
    R.FillColor = Color.Green
    R.FillStyle = FillStyle.Solid
    Me.Controls.Add(C)
End Sub

Upvotes: 0

Jay Riggs
Jay Riggs

Reputation: 53595

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

Upvotes: 1

Related Questions