Results 1 to 5 of 5

Thread: [RESOLVED] rectangle

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    32

    Resolved [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

  2. #2

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    32

    Re: rectangle

    I'm using WPF application

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Private Sub Window1_Loaded(ByVal sender As Object, _
    2.                            ByVal e As RoutedEventArgs) Handles MyBase.Loaded
    3.     Dim testRect As New Rectangle
    4.     Dim testCanvas As New Canvas
    5.     Dim testGrid As New Grid
    6.  
    7.     testGrid.Children.Add(testCanvas)
    8.     testCanvas.Children.Add(testRect)
    9.     testRect.RadiusX = 3
    10.     testRect.RadiusY = 3
    11.  
    12.     DirectCast(Me.Content, Grid).Children.Add(testGrid)
    13. End Sub
    and saw nothing. I then changed it to this:
    vb.net Code:
    1. Private Sub Window1_Loaded(ByVal sender As Object, _
    2.                            ByVal e As RoutedEventArgs) Handles MyBase.Loaded
    3.     Dim testRect As New Rectangle
    4.     Dim testCanvas As New Canvas
    5.     Dim testGrid As New Grid
    6.  
    7.     testGrid.Children.Add(testCanvas)
    8.     testCanvas.Children.Add(testRect)
    9.     testRect.RadiusX = 3
    10.     testRect.RadiusY = 3
    11.  
    12.     testRect.Stroke = Brushes.Black
    13.     testRect.Height = 100
    14.     testRect.Width = 200
    15.  
    16.     DirectCast(Me.Content, Grid).Children.Add(testGrid)
    17. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: rectangle

    Alternatively if you wanted to create the rectangle using XAML you could just do this:
    xml Code:
    1. <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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    32

    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
  •  



Click Here to Expand Forum to Full Width