|
-
Feb 3rd, 2009, 04:52 AM
#1
Thread Starter
Member
[RESOLVED] rectangle
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
-
Feb 3rd, 2009, 04:53 AM
#2
Thread Starter
Member
Re: rectangle
I'm using WPF application
-
Feb 3rd, 2009, 05:29 AM
#3
Re: rectangle
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:
vb.net 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 Sub
and saw nothing. I then changed it to this:
vb.net 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 testRect.Stroke = Brushes.Black testRect.Height = 100 testRect.Width = 200 DirectCast(Me.Content, Grid).Children.Add(testGrid) End Sub
and 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.
-
Feb 3rd, 2009, 10:31 AM
#4
Re: rectangle
Alternatively if you wanted to create the rectangle using XAML you could just do this:
xml Code:
<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
-
Feb 3rd, 2009, 10:40 AM
#5
Thread Starter
Member
Re: rectangle
Thanks jmcilhinney and chris. It helps!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|