Click to See Complete Forum and Search --> : [RESOLVED] rectangle
rene12358
Feb 3rd, 2009, 04:52 AM
Hi,
Anyone know how to create a rectangle using VB code....?
I tried the coding below but the rectangle does not appear:
Dim testRect As New Rectangle
Dim testCanvas As New Canvas
Dim testGrid As New Grid
testGrid.Children.Add(testCanvas)
testCanvas.Children.Add(testRect)
testRect.RadiusX = 3
testRect.RadiusY = 3
Thanks
rene12358
Feb 3rd, 2009, 04:53 AM
I'm using WPF application
jmcilhinney
Feb 3rd, 2009, 05:29 AM
I've never really used WPF but I thought I'd have a go at this to see if I could find an answer. When you create a new Window in a WPF app it has a Grid assigned to its Content property by default. I tried this code:Private Sub Window1_Loaded(ByVal sender As Object, _
ByVal e As RoutedEventArgs) Handles MyBase.Loaded
Dim testRect As New Rectangle
Dim testCanvas As New Canvas
Dim testGrid As New Grid
testGrid.Children.Add(testCanvas)
testCanvas.Children.Add(testRect)
testRect.RadiusX = 3
testRect.RadiusY = 3
DirectCast(Me.Content, Grid).Children.Add(testGrid)
End Suband saw nothing. I then changed it to this:Private Sub Window1_Loaded(ByVal sender As Object, _
ByVal e As RoutedEventArgs) Handles MyBase.Loaded
Dim testRect As New Rectangle
Dim testCanvas As New Canvas
Dim testGrid As New Grid
testGrid.Children.Add(testCanvas)
testCanvas.Children.Add(testRect)
testRect.RadiusX = 3
testRect.RadiusY = 3
testRect.Stroke = Brushes.Black
testRect.Height = 100
testRect.Width = 200
DirectCast(Me.Content, Grid).Children.Add(testGrid)
End Suband lo and behold, a rectangle appeared. If your rectangle has no height and no width, its border has no colour and it's not added to the Window's content, I guess it would be hard to see. :)
chris128
Feb 3rd, 2009, 10:31 AM
Alternatively if you wanted to create the rectangle using XAML you could just do this:
<Rectangle Height="100" Width="200" Fill="Black"/>
Of course you may of needed to create it dynamically in code for a reason so using XAML mat not be suitable but just thought I would mention it :)
rene12358
Feb 3rd, 2009, 10:40 AM
Thanks jmcilhinney and chris. It helps!
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.