Results 1 to 9 of 9

Thread: [RESOLVED] [2008] Drawing rounded rectangle.

  1. #1

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Resolved [RESOLVED] [2008] Drawing rounded rectangle.

    Hi All,

    I have a porblem with drawing a rounded rectangle.
    The function gives me an error:

    Function 'GetRoundedRectPath' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.
    Here's my code;

    Code:
    Public Class Form1
        Dim g As Graphics
        Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
            Application.Exit()
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    
            Dim width As Integer = Me.ClientRectangle.Width
            Dim heigth As Integer = Me.ClientRectangle.Height
            Dim rect As Rectangle = New Rectangle(10, 20, width - 20, heigth - 20)
            Dim path As GraphicsPath = getRoundedRectPath(rect, CInt(width / 10))
            g.FillPath(Brushes.LavenderBlush, path)
            g.DrawPath(Pens.Peru, path)
    
    
        End Sub
        Function GetRoundedRectPath(rect As Rectangle, ByVal radius As Integer) As GraphicsPath
            Dim diameter As Integer = 2 * radius
            Dim arcrect As Rectangle = New Rectangle(rect.Location, New Size(diameter, diameter))
            Dim path As GraphicsPath = New GraphicsPath()
    
            ' top left
            path.AddArc(arcrect, 180, 90)
    
            ' top right
            path.AddArc(arcrect, 270, 90)
    
            ' bottem right
            path.AddArc(arcrect, 0, 90)
    
            ' bottem left
            path.AddArc(arcrect, 90, 90)
    
            path.CloseFigure()
    
        End Function
    End Class
    Thanks in advance,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] Drawing rounded rectangle.

    Your function is not returning anything.

    Add

    Return path

    at the end of the Function.

  3. #3
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2008] Drawing rounded rectangle.

    shouldn't you use a return path in struction in the 'GetRoundedRectPath' function? Mayybe i'm missing something.

  4. #4

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2008] Drawing rounded rectangle.

    Mendhak,

    Thanks for your quick reply.
    But now, I'm having trouble with this line:

    Code:
     g.FillPath(Brushes.LavenderBlush, Path) ' Object reference not set to an instance of an object.
    Thanks,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Drawing rounded rectangle.

    How exactly does your code look right now?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2008] Drawing rounded rectangle.

    your global variable g as not been initialize before you use in the paint event of the form

  7. #7

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2008] Drawing rounded rectangle.

    Quote Originally Posted by Atheist
    How exactly does your code look right now?
    Hi,

    Here's my changed code;

    Code:
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            Dim g As Graphics = e.Graphics ' this was missing as object reference!
            Dim width As Integer = Me.ClientRectangle.Width
            Dim heigth As Integer = Me.ClientRectangle.Height
            Dim rect As Rectangle = New Rectangle(10, 10, width - 20, heigth - 20)
            Dim path As GraphicsPath = GetRoundedRectPath(rect, (width / 10))
    
            g.FillPath(Brushes.Yellow, Path)
            g.DrawPath(Pens.Black, path)
    
    
        End Sub
        Function GetRoundedRectPath(ByVal rect As Rectangle, ByVal radius As Integer) As GraphicsPath
    
            Dim diameter As Integer = 20 * radius
            Dim arcrect As Rectangle = New Rectangle(rect.Location, New Size(diameter, diameter))
            Dim path As GraphicsPath = New GraphicsPath()
            ' top left
            path.AddArc(arcrect, 180, 90)
            ' top right
            path.AddArc(arcrect, 270, 90)
            ' bottem right
            path.AddArc(arcrect, 0, 90)
            ' bottem left
            path.AddArc(arcrect, 90, 90)
    
            path.CloseFigure()
    
            Return path
        End Function
    The result however isn't the one I need.
    It draws an ellipise instead of a roundedrectangle.
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] Drawing rounded rectangle.

    try this:

    vb Code:
    1. Public Function roundedRectangle(ByVal X As Integer, ByVal Y As Integer, _
    2.         ByVal Width As Integer, ByVal Height As Integer, ByVal diameter As Integer) As System.Drawing.Drawing2D.GraphicsPath
    3.  
    4.      ''the 'diameter' parameter changes the size of the rounded region
    5.  
    6.      Dim graphics_path As New System.Drawing.Drawing2D.GraphicsPath
    7.  
    8.      Dim BaseRect As New RectangleF(X, Y, Width, Height)
    9.      Dim ArcRect As New RectangleF(BaseRect.Location, New SizeF(diameter, diameter))
    10.  
    11.      'top left Arc
    12.      graphics_path.AddArc(ArcRect, 180, 90)
    13.      graphics_path.AddLine(X + CInt(diameter / 2), _
    14.      Y, X + Width - CInt(diameter / 2), Y)
    15.  
    16.      ' top right arc
    17.      ArcRect.X = BaseRect.Right - diameter
    18.      graphics_path.AddArc(ArcRect, 270, 90)
    19.      graphics_path.AddLine(X + Width, _
    20.         Y + CInt(diameter / 2), X + Width, _
    21.                          Y + Height - CInt(diameter / 2))
    22.  
    23.      ' bottom right arc
    24.      ArcRect.Y = BaseRect.Bottom - diameter
    25.      graphics_path.AddArc(ArcRect, 0, 90)
    26.      graphics_path.AddLine(X + CInt(diameter / 2), _
    27.         Y + Height, X + Width - CInt(diameter / 2), _
    28.                          Y + Height)
    29.  
    30.      ' bottom left arc
    31.      ArcRect.X = BaseRect.Left
    32.      graphics_path.AddArc(ArcRect, 90, 90)
    33.      graphics_path.AddLine(X, Y + CInt(diameter / 2), _
    34.         X, Y + Height - CInt(diameter / 2))
    35.  
    36.      Return graphics_path
    37.  
    38. End Function

  9. #9

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2008] Drawing rounded rectangle.

    Thanks to your example .paul. , I found that I didn't draw the arcrect path.
    I've changed my code in that way and now I have a good looking roundedrectangle.
    Here's my code:

    Code:
    Imports System.Drawing
    Imports System.Drawing.Drawing2D
    Public Class Form1
        Inherits System.Windows.Forms.Form
        Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
            Application.Exit()
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            Dim g As Graphics = e.Graphics
            Dim width As Integer = Me.ClientRectangle.Width
            Dim heigth As Integer = Me.ClientRectangle.Height
            Dim rect As Rectangle = New Rectangle(10, 20, width - 50, heigth - 50)
            Dim path As GraphicsPath = GetRoundedRectPath(rect, (width / 10))
    
            g.FillPath(Brushes.Yellow, Path)
            g.DrawPath(Pens.Black, path)
            path.Dispose()
    
        End Sub
        Function GetRoundedRectPath(ByVal rect As Rectangle, ByVal radius As Integer) As GraphicsPath
    
            Dim diameter As Integer = 2 * radius
            Dim arcrect As Rectangle = New Rectangle(rect.Location, New Size(diameter, diameter))
            Dim path As GraphicsPath = New GraphicsPath()
    
            ' top left
            path.AddArc(arcrect, 180, 90)
    
            ' top right
            arcrect.X = rect.Right - diameter
            path.AddArc(arcrect, 270, 90)
    
            ' bottom right
            arcrect.Y = rect.Bottom - diameter
            path.AddArc(arcrect, 0, 90)
    
            ' bottom left
            arcrect.X = rect.Left
            path.AddArc(arcrect, 90, 90)
            path.CloseFigure()
    
            Return path
        End Function
    End Class
    Thanks for the hints,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

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