none
Code for 2008 and 2010 versions of Vb.Net. Have you ever wanted an oval, triangle, a pentagon, a hexagon or an octagon for a control or a FORM? ( If you are using an earlier version please read the bold lines at the start of the 1st post . )

    General discussion

  • Hi ALL,

    <edit> On 27th January, 2011
    If you are using VB.Net 2005 or earlier then please see my 14th post in this thread.

    </edit>


    I saw a post earlier on which reminded me about Extension Methods.

    This led me to create the following code which acts on the base class CONTROL for ALL controls!!

    So??!! You might ask what does it do?

    Well using just one line of code you can transform any of your controls
     into the shape you want within the bounds of the original shape of the control.

    So if you have a square control like a square button or picturebox, you will get a regular shape.

    If the dotted rectangle ( that the control is drawn out within ) is wider than it is tall
     or vice-versa than the regular shape is stretched to fit this shape.

    Best explained with an example.

    From the PROJECT menu select ADD MODULE , type in ShapedControls.Vb  in the NAME box and click on OK.

    Then PASTE this code in.>>

     

     

    Option Strict On
    
    Imports System.Runtime.CompilerServices
    
    Module ShapedControls
    
     Public Const Pi As Double = Math.PI
     Public Const DegreesToRadians As Double = 180 / Pi
    
     <Extension()> _
     Public Sub Shape(ByVal ctrl As Control, Optional ByVal NumberOfSides As Integer = 3, Optional ByVal OffsetAngleInDegrees As Double = 0)
    
      If NumberOfSides < 3 Then Throw New Exception("Number of sides can only be 3 or more.")
    
      Dim MyAngle As Double = OffsetAngleInDegrees / DegreesToRadians
    
      Dim radius1 As Integer = ctrl.Height \ 2
      Dim radius2 As Integer = ctrl.Width \ 2
      Dim xInt, yInt As Integer
      Dim xDoub, yDoub As Double
      Dim MyPath As New Drawing2D.GraphicsPath
    
      For angle As Double = MyAngle To ((2 * Pi) + MyAngle) Step ((2 * Pi) / NumberOfSides)
    
       xDoub = radius2 * Math.Cos(angle) + radius2
       yDoub = radius1 * Math.Sin(angle) + radius1
       xInt = CInt(Int(xDoub))
       yInt = CInt(Int(yDoub))
       MyPath.AddLine(New Point(xInt, yInt), New Point(xInt, yInt))
    
      Next
    
      MyPath.CloseFigure()
      ctrl.Region = New Region(MyPath)
      MyPath.Dispose()
    
     End Sub
    
    End Module
    

     


    Have you done that?

    Good.

    Proceed to the next post.


    Regards,

    John
    Thursday, September 03, 2009 8:29 PM

All replies

  • Hi again,

    Now paste this in as your FORM code and run it.>>

    Option Strict On
    Public Class Form1
    
     Dim P1, P2 As New PictureBox
    
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
      Me.WindowState = FormWindowState.Maximized
      P1.Size = New Size(300, 300)
      P1.Location = New Point(20, 20)
      P1.BackColor = Color.LightBlue
      P1.Shape(4)
      Me.Controls.Add(P1)
    
      P2.Size = New Size(300, 300)
      P2.Location = New Point(350, 20)
      P2.BackColor = Color.Yellow
      P2.Shape(8, 0)
      Me.Controls.Add(P2)
    
      Dim Btn1 As New Button
      Btn1.Location = New Point(Me.Width \ 2, Me.Height \ 2)
      Btn1.Size = New Size(300, 150)
      Btn1.TextAlign = ContentAlignment.MiddleCenter
      Btn1.Text = "Hi!!"
      Btn1.Font = New Font("Arial", 30, FontStyle.Underline)
      Btn1.BackColor = Color.Black
      Btn1.ForeColor = Color.White
      Btn1.Shape(6, 30)
      AddHandler Btn1.Click, AddressOf Btn1Click
      Me.Controls.Add(Btn1)
    
     End Sub
    
     Private Sub Btn1Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
      MessageBox.Show("Hi there!!")
      Dim ofd As New OpenFileDialog
      ofd.Filter = "Picture files|*.jpg;*.bmp;*.png"
      ofd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyPictures
      Dim result As DialogResult = ofd.ShowDialog
      P1.BackgroundImageLayout = ImageLayout.Zoom
    
      If result = Windows.Forms.DialogResult.OK Then
       P1.BackgroundImage = Image.FromFile(ofd.FileName)
      End If
    
     End Sub
    
    End Class



    The SHAPE extension for all controls takes two optional parameters or arguments.>>

    E.G:

    Button1.Shape(6,0)

    means 6 sides with an offset angle of zero degrees.

    Enjoy!!

    Oh, and the start point appears to be the right hand point on a circle as if the 0 degrees is EAST on a compass

    Please bear this in mind.

    As the parameters are OPTIONAL you could say.>>

    Button1.Shape()

    or

    Button1.Shape(6, 30)

    or

    Button1.Shape(, 30)

    as it defaults to 3 sides ( the minimum ).

     

    <edit on 14th July, 2010> With a version of the code below this post the minimum is now 2 sides.

    If the containing area is square you end up with a circle, otherwise you will get a pointed oval. </edit>

    The above example creates 2 shaped PictureBoxes and a stretched hexagon shaped button!!

    Like this where the first two shapes are PictureBoxes and the hexagon is a Button. :-)







    Regards,

    John

    Thursday, September 03, 2009 8:38 PM
  • Nice one big John, made it look easy....

    www.dsmyth.net | www.dsmyth.net/wiki
    Thursday, September 03, 2009 9:15 PM
  • Nice one big John, made it look easy....

    www.dsmyth.net | www.dsmyth.net/wiki

    Hi Derek,

    It uses one of a similiar set of mathematical routines I know ( off the top of my head )
     to draw circles and shapes and spirals and tubes and so on which I've known for about 26 years now.

    Some good stuff sticks with me. :-)

    You could of course add a control to a FORM first then simply change its shape
     using the code above with just one extra line of code. :-)

    You might like this early demo I wrote too ( all the maths in real time )!! >>

    http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/4deb242d-ab1f-4990-8e38-c8a11a12a423 


    Regards,

    John
    Thursday, September 03, 2009 9:20 PM
  • Hey John,

    I'll look at the demo another time man. Looks like it got a great response. Too chilled out to muck around in Visual Studio.
    www.dsmyth.net | www.dsmyth.net/wiki
    Thursday, September 03, 2009 9:48 PM
  • Replying to "tag" this for later. I so have to try this out, plus i think i have a use for it too :)

    Thanks for posting!
    Friday, September 04, 2009 10:12 PM
  • Replying to "tag" this for later. I so have to try this out, plus i think i have a use for it too :)

    Thanks for posting!

    Hi FthrJACK,

    Have you tried this out yet?
    I certainly think it looks different having different shape controls on a FORM. :-) 


    Regards,

    John
    Saturday, September 05, 2009 12:40 PM
  • John, have you dabbled with WPF yet?

    What WPF does with controls is it seperates look and behaviour from controls.
    You can draw all sorts of shaped controls without effecting their behaviour.

    You might like it :)


    www.dsmyth.net | www.dsmyth.net/wiki
    Saturday, September 05, 2009 2:22 PM
  • John, have you dabbled with WPF yet?

    What WPF does with controls is it seperates look and behaviour from controls.
    You can draw all sorts of shaped controls without effecting their behaviour.

    You might like it :)


    www.dsmyth.net | www.dsmyth.net/wiki

    Hi Derek,

    No I haven't.

    I have just installed EXPRESSION STUDIO 3 though and I will need to make room on my laptop to reinstall
    Visual Studio 2008 however I was thinking of trying the 2010 beta.

    I may remove the VISUAL BASIC 2008 EXPRESS EDITION, even though
     I like it for typing up and testing quick bits of code for my replies in this forum.


    Regards,

    John
    Saturday, September 05, 2009 7:08 PM
  • Hi ALL,

    I have added to the original code so you can now have controls in the shape of a pointed oval. :-)

    For now this can only be horizontal or vertical.

    If the control size is square such as 300 X 300 then the control ends up as a circle shape. :-)


    Regards,

    John

    Here is the updated module code.>>

    Option Strict On
    
    Imports System.Runtime.CompilerServices
    
    Module ShapedControls
    
        Public Const Pi As Double = Math.PI
        Public Const DegreesToRadians As Double = 180 / Pi
    
    
        <Extension()> _
        Public Sub Shape(ByVal ctrl As Control, Optional ByVal NumberOfSides As Integer = 3, Optional ByVal OffsetAngleInDegrees As Double = 0)
    
            If NumberOfSides < 2 Then Throw New Exception("Number of sides can only be 2 or more.")
    
            Dim MyPath As New Drawing2D.GraphicsPath
            Dim MyAngle As Double = OffsetAngleInDegrees / DegreesToRadians
    
            If NumberOfSides = 2 Then
    
                Dim MyPoints() As Point
                Dim MyPointsList As New List(Of Point)
    
                If ctrl.Width = ctrl.Height Then
                    MyPath.AddEllipse(New Rectangle(0, 0, ctrl.Width, ctrl.Height))
                ElseIf ctrl.Width > ctrl.Height Then
                    MyPointsList.Add(New Point(0, ctrl.Height \ 2))
                    MyPointsList.Add(New Point(ctrl.Width \ 2, 0))
                    MyPointsList.Add(New Point(ctrl.Width, ctrl.Height \ 2))
                    MyPoints = MyPointsList.ToArray
                    MyPath.AddCurve(MyPoints)
                    MyPointsList.Clear()
    
                    MyPointsList.Add(New Point(ctrl.Width, ctrl.Height \ 2))
                    MyPointsList.Add(New Point(ctrl.Width \ 2, ctrl.Height))
                    MyPointsList.Add(New Point(0, ctrl.Height \ 2))
                    MyPoints = MyPointsList.ToArray
                    MyPath.AddCurve(MyPoints)
                    MyPointsList.Clear()
                ElseIf ctrl.Width < ctrl.Height Then
                    MyPointsList.Add(New Point(ctrl.Width \ 2, 0))
                    MyPointsList.Add(New Point(ctrl.Width, ctrl.Height \ 2))
                    MyPointsList.Add(New Point(ctrl.Width \ 2, ctrl.Height))
                    MyPoints = MyPointsList.ToArray
                    MyPath.AddCurve(MyPoints)
                    MyPointsList.Clear()
    
                    MyPointsList.Add(New Point(ctrl.Width \ 2, ctrl.Height))
                    MyPointsList.Add(New Point(0, ctrl.Height \ 2))
                    MyPointsList.Add(New Point(ctrl.Width \ 2, 0))
                    MyPoints = MyPointsList.ToArray
                    MyPath.AddCurve(MyPoints)
                    MyPointsList.Clear()
                End If
            End If
    
            Dim radius1 As Integer = ctrl.Height \ 2
            Dim radius2 As Integer = ctrl.Width \ 2
            Dim xInt, yInt As Integer
            Dim xDoub, yDoub As Double
    
            For angle As Double = MyAngle To ((2 * Pi) + MyAngle) Step ((2 * Pi) / NumberOfSides)
    
                xDoub = radius2 * Math.Cos(angle) + radius2
                yDoub = radius1 * Math.Sin(angle) + radius1
                xInt = CInt(Int(xDoub))
                yInt = CInt(Int(yDoub))
                MyPath.AddLine(New Point(xInt, yInt), New Point(xInt, yInt))
    
            Next
    
            MyPath.CloseFigure()
            ctrl.Region = New Region(MyPath)
            MyPath.Dispose()
    
        End Sub
    
    End Module




    Here is some FORM code which demonstrates it and a screenshot.>>


    Option Strict On
    Public Class Form1
    
        Dim P1, P2 As New PictureBox
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Me.WindowState = FormWindowState.Maximized
            P1.Size = New Size(300, 300)
            P1.Location = New Point(20, 20)
            P1.BackColor = Color.LightBlue
            P1.Shape(4)
            Me.Controls.Add(P1)
    
            P2.Size = New Size(300, 300)
            P2.Location = New Point(350, 20)
            P2.BackColor = Color.Yellow
            P2.Shape(8, 0)
            Me.Controls.Add(P2)
    
            Dim Btn1 As New Button
            Btn1.Location = New Point(Me.Width \ 4, Me.Height \ 2)
            Btn1.Size = New Size(300, 200)
            Btn1.TextAlign = ContentAlignment.MiddleCenter
            Btn1.Text = "Hi!!"
            Btn1.Font = New Font("Arial", 30, FontStyle.Underline)
            Btn1.BackColor = Color.Black
            Btn1.ForeColor = Color.White
            Btn1.Shape(2)
            AddHandler Btn1.Click, AddressOf Btn1Click
            Me.Controls.Add(Btn1)
    
        End Sub
    
        Private Sub Btn1Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
            MessageBox.Show("Hi there!!")
            Dim ofd As New OpenFileDialog
            ofd.Filter = "Picture files|*.jpg;*.bmp;*.png"
            ofd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyPictures
            Dim result As DialogResult = ofd.ShowDialog
            P1.BackgroundImageLayout = ImageLayout.Zoom
    
            If result = Windows.Forms.DialogResult.OK Then
                P1.BackgroundImage = Image.FromFile(ofd.FileName)
            End If
    
        End Sub
    
    End Class





    The blacked pointed oval above is a button the other shapes are Pictureboxes!!


    Regards,

    John
    Monday, September 07, 2009 11:25 PM
  • Hi ALL,

    I have added even more code so you can make any control
    like a button into one of 8 triangle shapes as below.

    E.G: The buttons in the first picture turn into those in the second picture.  :-)    ;-)    >>


    Here is the very short FORM code.>>

    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Button1.MakeTriangular(TriangleDirection.Up)
            Button2.MakeTriangular(TriangleDirection.Down)
            Button3.MakeTriangular(TriangleDirection.Left)
            Button4.MakeTriangular(TriangleDirection.Right)
    
            Button5.MakeTriangular(TriangleDirection.TopRight)
            Button6.MakeTriangular(TriangleDirection.TopLeft)
            Button7.MakeTriangular(TriangleDirection.BottomRight)
            Button8.MakeTriangular(TriangleDirection.BottomLeft)
    
        End Sub
    
    End Class




    Here is the updated MODULE code.>>


    Option Strict On
    
    Imports System.Runtime.CompilerServices
    
    Module ShapedControls
    
        Public Const Pi As Double = Math.PI
        Public Const DegreesToRadians As Double = 180 / Pi
    
        Public Enum TriangleDirection
            Up
            Right
            Down
            Left
            TopRight
            BottomRight
            BottomLeft
            TopLeft
        End Enum
    
        <Extension()> _
        Public Sub Shape(ByVal ctrl As Control, Optional ByVal NumberOfSides As Integer = 3, Optional ByVal OffsetAngleInDegrees As Double = 0)
    
            If NumberOfSides < 2 Then Throw New Exception("Number of sides can only be 2 or more.")
    
            Dim MyPath As New Drawing2D.GraphicsPath
            Dim MyAngle As Double = OffsetAngleInDegrees / DegreesToRadians
    
            If NumberOfSides = 2 Then
    
                Dim MyPoints() As Point
                Dim MyPointsList As New List(Of Point)
    
                If ctrl.Width = ctrl.Height Then
                    MyPath.AddEllipse(New Rectangle(0, 0, ctrl.Width, ctrl.Height))
                ElseIf ctrl.Width > ctrl.Height Then
                    MyPointsList.Add(New Point(0, ctrl.Height \ 2))
                    MyPointsList.Add(New Point(ctrl.Width \ 2, 0))
                    MyPointsList.Add(New Point(ctrl.Width, ctrl.Height \ 2))
                    MyPoints = MyPointsList.ToArray
                    MyPath.AddCurve(MyPoints)
                    MyPointsList.Clear()
    
                    MyPointsList.Add(New Point(ctrl.Width, ctrl.Height \ 2))
                    MyPointsList.Add(New Point(ctrl.Width \ 2, ctrl.Height))
                    MyPointsList.Add(New Point(0, ctrl.Height \ 2))
                    MyPoints = MyPointsList.ToArray
                    MyPath.AddCurve(MyPoints)
                    MyPointsList.Clear()
                ElseIf ctrl.Width < ctrl.Height Then
                    MyPointsList.Add(New Point(ctrl.Width \ 2, 0))
                    MyPointsList.Add(New Point(ctrl.Width, ctrl.Height \ 2))
                    MyPointsList.Add(New Point(ctrl.Width \ 2, ctrl.Height))
                    MyPoints = MyPointsList.ToArray
                    MyPath.AddCurve(MyPoints)
                    MyPointsList.Clear()
    
                    MyPointsList.Add(New Point(ctrl.Width \ 2, ctrl.Height))
                    MyPointsList.Add(New Point(0, ctrl.Height \ 2))
                    MyPointsList.Add(New Point(ctrl.Width \ 2, 0))
                    MyPoints = MyPointsList.ToArray
                    MyPath.AddCurve(MyPoints)
                    MyPointsList.Clear()
                End If
            End If
    
            Dim radius1 As Integer = ctrl.Height \ 2
            Dim radius2 As Integer = ctrl.Width \ 2
            Dim xInt, yInt As Integer
            Dim xDoub, yDoub As Double
    
            For angle As Double = MyAngle To ((2 * Pi) + MyAngle) Step ((2 * Pi) / NumberOfSides)
    
                xDoub = radius2 * Math.Cos(angle) + radius2
                yDoub = radius1 * Math.Sin(angle) + radius1
                xInt = CInt(Int(xDoub))
                yInt = CInt(Int(yDoub))
                MyPath.AddLine(New Point(xInt, yInt), New Point(xInt, yInt))
    
            Next
    
            MyPath.CloseFigure()
            ctrl.Region = New Region(MyPath)
            MyPath.Dispose()
    
        End Sub
    
        <Extension()> _
        Public Sub MakeTriangular(ByVal ctrl As Control, ByVal Triangle_Direction As TriangleDirection)
    
            Dim MyPath As New Drawing2D.GraphicsPath
    
            Select Case Triangle_Direction
                Case Is = TriangleDirection.Up
                    MyPath.AddLine(0, ctrl.Height, 0, ctrl.Height)
                    MyPath.AddLine(0, ctrl.Height, ctrl.Width \ 2, 0)
                    MyPath.AddLine(ctrl.Width \ 2, 0, ctrl.Width, ctrl.Height)
                Case TriangleDirection.Right
                    MyPath.AddLine(0, ctrl.Height, 0, 0)
                    MyPath.AddLine(0, 0, ctrl.Width, ctrl.Height \ 2)
    
                Case TriangleDirection.Down
                    MyPath.AddLine(0, 0, ctrl.Width, 0)
                    MyPath.AddLine(ctrl.Width, 0, ctrl.Width \ 2, ctrl.Height)
    
                Case TriangleDirection.Left
                    MyPath.AddLine(ctrl.Width, 0, ctrl.Width, ctrl.Height)
                    MyPath.AddLine(ctrl.Width, ctrl.Height, 0, ctrl.Height \ 2)
    
                Case TriangleDirection.TopRight
                    MyPath.AddLine(0, 0, ctrl.Width, 0)
                    MyPath.AddLine(ctrl.Width, 0, ctrl.Width, ctrl.Height)
    
                Case TriangleDirection.TopLeft
                    MyPath.AddLine(0, 0, ctrl.Width, 0)
                    MyPath.AddLine(ctrl.Width, 0, 0, ctrl.Height)
    
                Case TriangleDirection.BottomRight
                    MyPath.AddLine(ctrl.Width, 0, ctrl.Width, ctrl.Height)
                    MyPath.AddLine(ctrl.Width, ctrl.Height, 0, ctrl.Height)
    
                Case TriangleDirection.BottomLeft
                    MyPath.AddLine(0, 0, ctrl.Width, ctrl.Height)
                    MyPath.AddLine(ctrl.Width, ctrl.Height, 0, ctrl.Height)
    
            End Select
            MyPath.CloseFigure()
            ctrl.Region = New Region(MyPath)
    
        End Sub
    
    End Module

    Regards,

    John

    P.S. I am thinking of adding one more routine to the above module at a later date.

    I will leave you all guessing what other kind of symmetrical regular shape
     I have not yet catered for in the above code.


    Thursday, September 10, 2009 2:59 AM
  • That's very nice john.

    In the picture  I see some of the Control's Text  is cut off.

    Is there any way to center the text in the triangle and show the full Text "Button5"?

    Asgar
    Thursday, September 10, 2009 5:17 AM
  • That's very nice john.

    In the picture  I see some of the Control's Text  is cut off.

    Is there any way to center the text in the triangle and show the full Text "Button5"?

    Asgar

    Hi _asgar,

    Of course :-)

    In DESIGN view highlight Button5 and set the TextAlign property in the properies window.

    Press the F4 key to see the properties window when you are not running the code.
    >>




    Alternatively in code.>>

    Button5.TextAlign = ContentAlignment.MiddleRight

    as in.>>

    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Button1.MakeTriangular(TriangleDirection.Up)
            Button2.MakeTriangular(TriangleDirection.Down)
            Button3.MakeTriangular(TriangleDirection.Left)
            Button4.MakeTriangular(TriangleDirection.Right)
    
            Button5.MakeTriangular(TriangleDirection.TopRight)
            Button6.MakeTriangular(TriangleDirection.TopLeft)
            Button7.MakeTriangular(TriangleDirection.BottomRight)
            Button8.MakeTriangular(TriangleDirection.BottomLeft)
    
            Button5.TextAlign = ContentAlignment.MiddleRight
            Button6.TextAlign = ContentAlignment.MiddleLeft
            Button7.TextAlign = ContentAlignment.MiddleRight
            Button8.TextAlign = ContentAlignment.MiddleLeft
    
        End Sub
    
    End Class


    Have you tried the SHAPE extension ( also in the same module ) for creating other shapes too?


    Regards,

    John
    Thursday, September 10, 2009 12:49 PM
  • Hi again,

    So here in the picture are four buttons ( Button1 on top of Button2 ) and ( Button3 on top of Button4 ),
    each different colors ( or colours if you are British like myself ) set in different directions like this.>>

    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Button1.MakeTriangular(TriangleDirection.TopRight)
            Button2.MakeTriangular(TriangleDirection.BottomLeft)
            Button3.MakeTriangular(TriangleDirection.TopLeft)
            Button4.MakeTriangular(TriangleDirection.BottomRight)
    
            Button1.TextAlign = ContentAlignment.MiddleRight
            Button2.TextAlign = ContentAlignment.MiddleLeft
            Button3.TextAlign = ContentAlignment.MiddleLeft
            Button4.TextAlign = ContentAlignment.MiddleRight
    
        End Sub
    
    End Class


    Giving this effect.>>




    Just one of a few possibilities. :-)

    Microsoft tell us that; "WINDOWS, is life without walls!!"


    I'm now saying; "CONTROLS, life doesn't have to be squares and rectangles!!"   :-)   ;-)


    Regards,

    John
    Thursday, September 10, 2009 1:37 PM
  • Hi ALL,

    Feel FREE to use the above code in a commercial application.

    I would appreciate some acknowledgement though :-)  and a percentage
     of any profits as I'm a student and I've never had a lot of money.


    Regards,

    John

    I'm currently looking for work in Vb.Net software development. :-)
    Thursday, September 10, 2009 2:29 PM
  • Also of interest might be this tute on youtube, how to make FORMS any shape you like too :)


    http://www.youtube.com/watch?v=ycYxTKNzHa0
    Thursday, September 10, 2009 3:12 PM
  • Also of interest might be this tute on youtube, how to make FORMS any shape you like too :)


    http://www.youtube.com/watch?v=ycYxTKNzHa0

    Hi FthrJACK,

    Thanks for posting the YouTube video url link.

    I already know how to do that though. :-)

    I could add a bit of code to the module above so that it will work with
     the Shape and MakeTriangular methods.

     In fact what an excellent idea. :-)

    >> So here is the FORM code.>>

    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Me.MakeTriangular(TriangleDirection.TopRight)
    
        End Sub
    End Class


    Here is the now longer MODULE code which I now call ShapedThings.  :-)   ;-)


    It doesn't really matter what you name the MODULE code to be for EXTENSION methods. :-)


    Option Strict On
    
    Imports System.Runtime.CompilerServices
    
    Module ShapedThings
    
        Public Const Pi As Double = Math.PI
        Public Const DegreesToRadians As Double = 180 / Pi
    
        Public Enum TriangleDirection
            Up
            Right
            Down
            Left
            TopRight
            BottomRight
            BottomLeft
            TopLeft
        End Enum
    
        <Extension()> _
        Public Sub Shape(ByVal ctrl As Control, Optional ByVal NumberOfSides As Integer = 3, Optional ByVal OffsetAngleInDegrees As Double = 0)
    
            If NumberOfSides < 2 Then Throw New Exception("Number of sides can only be 2 or more.")
    
            Dim MyPath As New Drawing2D.GraphicsPath
            Dim MyAngle As Double = OffsetAngleInDegrees / DegreesToRadians
    
            If NumberOfSides = 2 Then
    
                Dim MyPoints() As Point
                Dim MyPointsList As New List(Of Point)
    
                If ctrl.Width = ctrl.Height Then
                    MyPath.AddEllipse(New Rectangle(0, 0, ctrl.Width, ctrl.Height))
                ElseIf ctrl.Width > ctrl.Height Then
                    MyPointsList.Add(New Point(0, ctrl.Height \ 2))
                    MyPointsList.Add(New Point(ctrl.Width \ 2, 0))
                    MyPointsList.Add(New Point(ctrl.Width, ctrl.Height \ 2))
                    MyPoints = MyPointsList.ToArray
                    MyPath.AddCurve(MyPoints)
                    MyPointsList.Clear()
    
                    MyPointsList.Add(New Point(ctrl.Width, ctrl.Height \ 2))
                    MyPointsList.Add(New Point(ctrl.Width \ 2, ctrl.Height))
                    MyPointsList.Add(New Point(0, ctrl.Height \ 2))
                    MyPoints = MyPointsList.ToArray
                    MyPath.AddCurve(MyPoints)
                    MyPointsList.Clear()
                ElseIf ctrl.Width < ctrl.Height Then
                    MyPointsList.Add(New Point(ctrl.Width \ 2, 0))
                    MyPointsList.Add(New Point(ctrl.Width, ctrl.Height \ 2))
                    MyPointsList.Add(New Point(ctrl.Width \ 2, ctrl.Height))
                    MyPoints = MyPointsList.ToArray
                    MyPath.AddCurve(MyPoints)
                    MyPointsList.Clear()
    
                    MyPointsList.Add(New Point(ctrl.Width \ 2, ctrl.Height))
                    MyPointsList.Add(New Point(0, ctrl.Height \ 2))
                    MyPointsList.Add(New Point(ctrl.Width \ 2, 0))
                    MyPoints = MyPointsList.ToArray
                    MyPath.AddCurve(MyPoints)
                    MyPointsList.Clear()
                End If
            End If
    
            Dim radius1 As Integer = ctrl.Height \ 2
            Dim radius2 As Integer = ctrl.Width \ 2
            Dim xInt, yInt As Integer
            Dim xDoub, yDoub As Double
    
            For angle As Double = MyAngle To ((2 * Pi) + MyAngle) Step ((2 * Pi) / NumberOfSides)
    
                xDoub = radius2 * Math.Cos(angle) + radius2
                yDoub = radius1 * Math.Sin(angle) + radius1
                xInt = CInt(Int(xDoub))
                yInt = CInt(Int(yDoub))
                MyPath.AddLine(New Point(xInt, yInt), New Point(xInt, yInt))
    
            Next
    
            MyPath.CloseFigure()
            ctrl.Region = New Region(MyPath)
            MyPath.Dispose()
    
        End Sub
    
        <Extension()> _
        Public Sub MakeTriangular(ByVal ctrl As Control, ByVal Triangle_Direction As TriangleDirection)
    
            Dim MyPath As New Drawing2D.GraphicsPath
    
            Select Case Triangle_Direction
                Case Is = TriangleDirection.Up
                    MyPath.AddLine(0, ctrl.Height, 0, ctrl.Height)
                    MyPath.AddLine(0, ctrl.Height, ctrl.Width \ 2, 0)
                    MyPath.AddLine(ctrl.Width \ 2, 0, ctrl.Width, ctrl.Height)
                Case TriangleDirection.Right
                    MyPath.AddLine(0, ctrl.Height, 0, 0)
                    MyPath.AddLine(0, 0, ctrl.Width, ctrl.Height \ 2)
    
                Case TriangleDirection.Down
                    MyPath.AddLine(0, 0, ctrl.Width, 0)
                    MyPath.AddLine(ctrl.Width, 0, ctrl.Width \ 2, ctrl.Height)
    
                Case TriangleDirection.Left
                    MyPath.AddLine(ctrl.Width, 0, ctrl.Width, ctrl.Height)
                    MyPath.AddLine(ctrl.Width, ctrl.Height, 0, ctrl.Height \ 2)
    
                Case TriangleDirection.TopRight
                    MyPath.AddLine(0, 0, ctrl.Width, 0)
                    MyPath.AddLine(ctrl.Width, 0, ctrl.Width, ctrl.Height)
    
                Case TriangleDirection.TopLeft
                    MyPath.AddLine(0, 0, ctrl.Width, 0)
                    MyPath.AddLine(ctrl.Width, 0, 0, ctrl.Height)
    
                Case TriangleDirection.BottomRight
                    MyPath.AddLine(ctrl.Width, 0, ctrl.Width, ctrl.Height)
                    MyPath.AddLine(ctrl.Width, ctrl.Height, 0, ctrl.Height)
    
                Case TriangleDirection.BottomLeft
                    MyPath.AddLine(0, 0, ctrl.Width, ctrl.Height)
                    MyPath.AddLine(ctrl.Width, ctrl.Height, 0, ctrl.Height)
    
            End Select
            MyPath.CloseFigure()
            ctrl.Region = New Region(MyPath)
    
        End Sub
    
        <Extension()> _
        Public Sub Shape(ByVal AnyForm As System.Windows.Forms.Form, Optional ByVal NumberOfSides As Integer = 3, Optional ByVal OffsetAngleInDegrees As Double = 0)
    
            If NumberOfSides < 2 Then Throw New Exception("Number of sides can only be 2 or more.")
    
            Dim MyPath As New Drawing2D.GraphicsPath
            Dim MyAngle As Double = OffsetAngleInDegrees / DegreesToRadians
    
            If NumberOfSides = 2 Then
    
                Dim MyPoints() As Point
                Dim MyPointsList As New List(Of Point)
    
                If AnyForm.Width = AnyForm.Height Then
                    MyPath.AddEllipse(New Rectangle(0, 0, AnyForm.Width, AnyForm.Height))
                ElseIf AnyForm.Width > AnyForm.Height Then
                    MyPointsList.Add(New Point(0, AnyForm.Height \ 2))
                    MyPointsList.Add(New Point(AnyForm.Width \ 2, 0))
                    MyPointsList.Add(New Point(AnyForm.Width, AnyForm.Height \ 2))
                    MyPoints = MyPointsList.ToArray
                    MyPath.AddCurve(MyPoints)
                    MyPointsList.Clear()
    
                    MyPointsList.Add(New Point(AnyForm.Width, AnyForm.Height \ 2))
                    MyPointsList.Add(New Point(AnyForm.Width \ 2, AnyForm.Height))
                    MyPointsList.Add(New Point(0, AnyForm.Height \ 2))
                    MyPoints = MyPointsList.ToArray
                    MyPath.AddCurve(MyPoints)
                    MyPointsList.Clear()
                ElseIf AnyForm.Width < AnyForm.Height Then
                    MyPointsList.Add(New Point(AnyForm.Width \ 2, 0))
                    MyPointsList.Add(New Point(AnyForm.Width, AnyForm.Height \ 2))
                    MyPointsList.Add(New Point(AnyForm.Width \ 2, AnyForm.Height))
                    MyPoints = MyPointsList.ToArray
                    MyPath.AddCurve(MyPoints)
                    MyPointsList.Clear()
    
                    MyPointsList.Add(New Point(AnyForm.Width \ 2, AnyForm.Height))
                    MyPointsList.Add(New Point(0, AnyForm.Height \ 2))
                    MyPointsList.Add(New Point(AnyForm.Width \ 2, 0))
                    MyPoints = MyPointsList.ToArray
                    MyPath.AddCurve(MyPoints)
                    MyPointsList.Clear()
                End If
            End If
    
            Dim radius1 As Integer = AnyForm.Height \ 2
            Dim radius2 As Integer = AnyForm.Width \ 2
            Dim xInt, yInt As Integer
            Dim xDoub, yDoub As Double
    
            For angle As Double = MyAngle To ((2 * Pi) + MyAngle) Step ((2 * Pi) / NumberOfSides)
    
                xDoub = radius2 * Math.Cos(angle) + radius2
                yDoub = radius1 * Math.Sin(angle) + radius1
                xInt = CInt(Int(xDoub))
                yInt = CInt(Int(yDoub))
                MyPath.AddLine(New Point(xInt, yInt), New Point(xInt, yInt))
    
            Next
    
            MyPath.CloseFigure()
            AnyForm.Region = New Region(MyPath)
            MyPath.Dispose()
    
        End Sub
    
        <Extension()> _
        Public Sub MakeTriangular(ByVal AnyForm As System.Windows.Forms.Form, ByVal Triangle_Direction As TriangleDirection)
    
            Dim MyPath As New Drawing2D.GraphicsPath
    
            Select Case Triangle_Direction
                Case Is = TriangleDirection.Up
                    MyPath.AddLine(0, AnyForm.Height, 0, AnyForm.Height)
                    MyPath.AddLine(0, AnyForm.Height, AnyForm.Width \ 2, 0)
                    MyPath.AddLine(AnyForm.Width \ 2, 0, AnyForm.Width, AnyForm.Height)
                Case TriangleDirection.Right
                    MyPath.AddLine(0, AnyForm.Height, 0, 0)
                    MyPath.AddLine(0, 0, AnyForm.Width, AnyForm.Height \ 2)
    
                Case TriangleDirection.Down
                    MyPath.AddLine(0, 0, AnyForm.Width, 0)
                    MyPath.AddLine(AnyForm.Width, 0, AnyForm.Width \ 2, AnyForm.Height)
    
                Case TriangleDirection.Left
                    MyPath.AddLine(AnyForm.Width, 0, AnyForm.Width, AnyForm.Height)
                    MyPath.AddLine(AnyForm.Width, AnyForm.Height, 0, AnyForm.Height \ 2)
    
                Case TriangleDirection.TopRight
                    MyPath.AddLine(0, 0, AnyForm.Width, 0)
                    MyPath.AddLine(AnyForm.Width, 0, AnyForm.Width, AnyForm.Height)
    
                Case TriangleDirection.TopLeft
                    MyPath.AddLine(0, 0, AnyForm.Width, 0)
                    MyPath.AddLine(AnyForm.Width, 0, 0, AnyForm.Height)
    
                Case TriangleDirection.BottomRight
                    MyPath.AddLine(AnyForm.Width, 0, AnyForm.Width, AnyForm.Height)
                    MyPath.AddLine(AnyForm.Width, AnyForm.Height, 0, AnyForm.Height)
    
                Case TriangleDirection.BottomLeft
                    MyPath.AddLine(0, 0, AnyForm.Width, AnyForm.Height)
                    MyPath.AddLine(AnyForm.Width, AnyForm.Height, 0, AnyForm.Height)
    
            End Select
            MyPath.CloseFigure()
            AnyForm.Region = New Region(MyPath)
    
        End Sub
    End Module



    Regards,

    John

    I'm currently looking for work in Vb.Net software development. :-)
    Thursday, September 10, 2009 3:32 PM
  • Also of interest might be this tute on youtube, how to make FORMS any shape you like too :)


    http://www.youtube.com/watch?v=ycYxTKNzHa0

    Hi FthrJACK,

    I have two criticisms about the video, other than the speaker is using a 2002 or 2003 version of Vb.Net.

    In the code the speaker writes.>>


    Dim thirdWidth As Int16 = Width / 3


    you can see this at 5:07 in the video.

    1) This won't work with OPTION STRICT ON. Use INTEGER division instead.>>

    Dim thirdWidth As Int32 = Width \ 3

    2) Plus an Int32 or INTEGER should be used as the WIDTH property of a FORM is an INTEGER.

    My code in the MODULE above sets the REGION for you as per any code used with the extensions

    Shape and MakeTriangular above but EXTENSION METHODs will only work in 2008 and 2010 versions.


    Regards,

    John

    P.S. Just so you know, code written with OPTION STRICT ON will also work
     with OPTION STRICT OFF but not always the other way around.


    I'm currently looking for work in Vb.Net software development. :-)
    Thursday, September 10, 2009 4:20 PM
  • Hi ALL,

    For 2002, 2003 and 2005 versions of VB.Net where extension methods can not be used, please see some of the threads below.

    Please also see.>>

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/2ce51cb6-cfca-4778-888b-248f8955ed9d

    To see how to make vertical cross-shaped controls please see.>>

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/a2210be9-5ee8-421a-884b-62262b2ab06c

    To see how to make star-shaped controls please see.>>

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/543c39c0-60ed-4a02-a240-725b4a88ca8e/#ab1b98e1-86cb-430f-99b3-a39f90935743

     


    Regards, John
    Thursday, July 22, 2010 10:54 AM
  • Hi 

    Im an old guy whom very interested in programming in late age

    I tried use your code for different shape command button but it would not work.

    Error message for <Extession> did not declared.

    Im using visual studio 2008 Free trial verson

    I really would like to create oval shape command button or form whatever .

    Thank you very much for your help.

    kwan

    Wednesday, January 26, 2011 7:41 PM
  • Hi kwankwon,

    The majority of the code is meant to be placed into a MODULE .

    Select ADD MODULE from the Project menu and then paste the code in from

    my post at the very top of this thread if you want to use it like this in a Form.>>

     

    Button1.Shape(2,0)

     

    However, if you prefer to have the code all in one place then from any

    version of Vb.Net add one Button to a Form and use the code like this.>>

     

    Shape(Button1, 2, 0)

     

    Here is the entire code in a manner that will work with any version of VB.Net >>

    Public Class Form1
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        Button1.BackColor = Color.White
        Shape(Button1, 2, 0)
    
      End Sub
    
      Public Const Pi As Double = Math.PI
      Public Const DegreesToRadians As Double = 180 / Pi
    
      Public Enum TriangleDirection
        Up
        Right
        Down
        Left
        TopRight
        BottomRight
        BottomLeft
        TopLeft
      End Enum
    
      Public Sub Shape(ByVal ctrl As Control, Optional ByVal NumberOfSides As Integer = 3, Optional ByVal OffsetAngleInDegrees As Double = 0)
    
        If NumberOfSides < 2 Then Throw New Exception("Number of sides can only be 2 or more.")
    
        Dim MyPath As New Drawing2D.GraphicsPath
        Dim MyAngle As Double = OffsetAngleInDegrees / DegreesToRadians
    
        If NumberOfSides = 2 Then
    
          Dim MyPoints() As Point
          Dim MyPointsList As New List(Of Point)
    
          If ctrl.Width = ctrl.Height Then
            MyPath.AddEllipse(New Rectangle(0, 0, ctrl.Width, ctrl.Height))
          ElseIf ctrl.Width > ctrl.Height Then
            MyPointsList.Add(New Point(0, ctrl.Height \ 2))
            MyPointsList.Add(New Point(ctrl.Width \ 2, 0))
            MyPointsList.Add(New Point(ctrl.Width, ctrl.Height \ 2))
            MyPoints = MyPointsList.ToArray
            MyPath.AddCurve(MyPoints)
            MyPointsList.Clear()
    
            MyPointsList.Add(New Point(ctrl.Width, ctrl.Height \ 2))
            MyPointsList.Add(New Point(ctrl.Width \ 2, ctrl.Height))
            MyPointsList.Add(New Point(0, ctrl.Height \ 2))
            MyPoints = MyPointsList.ToArray
            MyPath.AddCurve(MyPoints)
            MyPointsList.Clear()
          ElseIf ctrl.Width < ctrl.Height Then
            MyPointsList.Add(New Point(ctrl.Width \ 2, 0))
            MyPointsList.Add(New Point(ctrl.Width, ctrl.Height \ 2))
            MyPointsList.Add(New Point(ctrl.Width \ 2, ctrl.Height))
            MyPoints = MyPointsList.ToArray
            MyPath.AddCurve(MyPoints)
            MyPointsList.Clear()
    
            MyPointsList.Add(New Point(ctrl.Width \ 2, ctrl.Height))
            MyPointsList.Add(New Point(0, ctrl.Height \ 2))
            MyPointsList.Add(New Point(ctrl.Width \ 2, 0))
            MyPoints = MyPointsList.ToArray
            MyPath.AddCurve(MyPoints)
            MyPointsList.Clear()
          End If
        End If
    
        Dim radius1 As Integer = ctrl.Height \ 2
        Dim radius2 As Integer = ctrl.Width \ 2
        Dim xInt, yInt As Integer
        Dim xDoub, yDoub As Double
    
        For angle As Double = MyAngle To ((2 * Pi) + MyAngle) Step ((2 * Pi) / NumberOfSides)
    
          xDoub = radius2 * Math.Cos(angle) + radius2
          yDoub = radius1 * Math.Sin(angle) + radius1
          xInt = CInt(Int(xDoub))
          yInt = CInt(Int(yDoub))
          MyPath.AddLine(New Point(xInt, yInt), New Point(xInt, yInt))
    
        Next
    
        MyPath.CloseFigure()
        ctrl.Region = New Region(MyPath)
        MyPath.Dispose()
    
      End Sub
    
      Public Sub MakeTriangular(ByVal ctrl As Control, ByVal Triangle_Direction As TriangleDirection)
    
        Dim MyPath As New Drawing2D.GraphicsPath
    
        Select Case Triangle_Direction
          Case Is = TriangleDirection.Up
            MyPath.AddLine(0, ctrl.Height, 0, ctrl.Height)
            MyPath.AddLine(0, ctrl.Height, ctrl.Width \ 2, 0)
            MyPath.AddLine(ctrl.Width \ 2, 0, ctrl.Width, ctrl.Height)
          Case TriangleDirection.Right
            MyPath.AddLine(0, ctrl.Height, 0, 0)
            MyPath.AddLine(0, 0, ctrl.Width, ctrl.Height \ 2)
    
          Case TriangleDirection.Down
            MyPath.AddLine(0, 0, ctrl.Width, 0)
            MyPath.AddLine(ctrl.Width, 0, ctrl.Width \ 2, ctrl.Height)
    
          Case TriangleDirection.Left
            MyPath.AddLine(ctrl.Width, 0, ctrl.Width, ctrl.Height)
            MyPath.AddLine(ctrl.Width, ctrl.Height, 0, ctrl.Height \ 2)
    
          Case TriangleDirection.TopRight
            MyPath.AddLine(0, 0, ctrl.Width, 0)
            MyPath.AddLine(ctrl.Width, 0, ctrl.Width, ctrl.Height)
    
          Case TriangleDirection.TopLeft
            MyPath.AddLine(0, 0, ctrl.Width, 0)
            MyPath.AddLine(ctrl.Width, 0, 0, ctrl.Height)
    
          Case TriangleDirection.BottomRight
            MyPath.AddLine(ctrl.Width, 0, ctrl.Width, ctrl.Height)
            MyPath.AddLine(ctrl.Width, ctrl.Height, 0, ctrl.Height)
    
          Case TriangleDirection.BottomLeft
            MyPath.AddLine(0, 0, ctrl.Width, ctrl.Height)
            MyPath.AddLine(ctrl.Width, ctrl.Height, 0, ctrl.Height)
    
        End Select
        MyPath.CloseFigure()
        ctrl.Region = New Region(MyPath)
        MyPath.Dispose()
    
      End Sub
    
      Public Sub Shape(ByVal AnyForm As System.Windows.Forms.Form, Optional ByVal NumberOfSides As Integer = 3, Optional ByVal OffsetAngleInDegrees As Double = 0)
    
        If NumberOfSides < 2 Then Throw New Exception("Number of sides can only be 2 or more.")
    
        Dim MyPath As New Drawing2D.GraphicsPath
        Dim MyAngle As Double = OffsetAngleInDegrees / DegreesToRadians
    
        If NumberOfSides = 2 Then
    
          Dim MyPoints() As Point
          Dim MyPointsList As New List(Of Point)
    
          If AnyForm.Width = AnyForm.Height Then
            MyPath.AddEllipse(New Rectangle(0, 0, AnyForm.Width, AnyForm.Height))
          ElseIf AnyForm.Width > AnyForm.Height Then
            MyPointsList.Add(New Point(0, AnyForm.Height \ 2))
            MyPointsList.Add(New Point(AnyForm.Width \ 2, 0))
            MyPointsList.Add(New Point(AnyForm.Width, AnyForm.Height \ 2))
            MyPoints = MyPointsList.ToArray
            MyPath.AddCurve(MyPoints)
            MyPointsList.Clear()
    
            MyPointsList.Add(New Point(AnyForm.Width, AnyForm.Height \ 2))
            MyPointsList.Add(New Point(AnyForm.Width \ 2, AnyForm.Height))
            MyPointsList.Add(New Point(0, AnyForm.Height \ 2))
            MyPoints = MyPointsList.ToArray
            MyPath.AddCurve(MyPoints)
            MyPointsList.Clear()
          ElseIf AnyForm.Width < AnyForm.Height Then
            MyPointsList.Add(New Point(AnyForm.Width \ 2, 0))
            MyPointsList.Add(New Point(AnyForm.Width, AnyForm.Height \ 2))
            MyPointsList.Add(New Point(AnyForm.Width \ 2, AnyForm.Height))
            MyPoints = MyPointsList.ToArray
            MyPath.AddCurve(MyPoints)
            MyPointsList.Clear()
    
            MyPointsList.Add(New Point(AnyForm.Width \ 2, AnyForm.Height))
            MyPointsList.Add(New Point(0, AnyForm.Height \ 2))
            MyPointsList.Add(New Point(AnyForm.Width \ 2, 0))
            MyPoints = MyPointsList.ToArray
            MyPath.AddCurve(MyPoints)
            MyPointsList.Clear()
          End If
        End If
    
        Dim radius1 As Integer = AnyForm.Height \ 2
        Dim radius2 As Integer = AnyForm.Width \ 2
        Dim xInt, yInt As Integer
        Dim xDoub, yDoub As Double
    
        For angle As Double = MyAngle To ((2 * Pi) + MyAngle) Step ((2 * Pi) / NumberOfSides)
    
          xDoub = radius2 * Math.Cos(angle) + radius2
          yDoub = radius1 * Math.Sin(angle) + radius1
          xInt = CInt(Int(xDoub))
          yInt = CInt(Int(yDoub))
          MyPath.AddLine(New Point(xInt, yInt), New Point(xInt, yInt))
    
        Next
    
        MyPath.CloseFigure()
        AnyForm.Region = New Region(MyPath)
        MyPath.Dispose()
    
      End Sub
    
      Public Sub MakeTriangular(ByVal AnyForm As System.Windows.Forms.Form, ByVal Triangle_Direction As TriangleDirection)
    
        Dim MyPath As New Drawing2D.GraphicsPath
    
        Select Case Triangle_Direction
          Case Is = TriangleDirection.Up
            MyPath.AddLine(0, AnyForm.Height, 0, AnyForm.Height)
            MyPath.AddLine(0, AnyForm.Height, AnyForm.Width \ 2, 0)
            MyPath.AddLine(AnyForm.Width \ 2, 0, AnyForm.Width, AnyForm.Height)
          Case TriangleDirection.Right
            MyPath.AddLine(0, AnyForm.Height, 0, 0)
            MyPath.AddLine(0, 0, AnyForm.Width, AnyForm.Height \ 2)
    
          Case TriangleDirection.Down
            MyPath.AddLine(0, 0, AnyForm.Width, 0)
            MyPath.AddLine(AnyForm.Width, 0, AnyForm.Width \ 2, AnyForm.Height)
    
          Case TriangleDirection.Left
            MyPath.AddLine(AnyForm.Width, 0, AnyForm.Width, AnyForm.Height)
            MyPath.AddLine(AnyForm.Width, AnyForm.Height, 0, AnyForm.Height \ 2)
    
          Case TriangleDirection.TopRight
            MyPath.AddLine(0, 0, AnyForm.Width, 0)
            MyPath.AddLine(AnyForm.Width, 0, AnyForm.Width, AnyForm.Height)
    
          Case TriangleDirection.TopLeft
            MyPath.AddLine(0, 0, AnyForm.Width, 0)
            MyPath.AddLine(AnyForm.Width, 0, 0, AnyForm.Height)
    
          Case TriangleDirection.BottomRight
            MyPath.AddLine(AnyForm.Width, 0, AnyForm.Width, AnyForm.Height)
            MyPath.AddLine(AnyForm.Width, AnyForm.Height, 0, AnyForm.Height)
    
          Case TriangleDirection.BottomLeft
            MyPath.AddLine(0, 0, AnyForm.Width, AnyForm.Height)
            MyPath.AddLine(AnyForm.Width, AnyForm.Height, 0, AnyForm.Height)
    
        End Select
        MyPath.CloseFigure()
        AnyForm.Region = New Region(MyPath)
        MyPath.Dispose()
    
      End Sub
    
    End Class
    

     

    Have fun!!

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Wednesday, January 26, 2011 10:45 PM
  • Hi kwankwon,

    To prevent spam please delete your email address from the above post.

    I have sent you an email so that you now have an email address to reach me on.

    Please do not add my regular email address to any text that you post in this forum.

    Please acknowledge this post then I will delete this one.

    By the way, as far as I know, you can only use Extension methods in VB.Net 2008 and VB.Net 2010 versions.

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Wednesday, February 09, 2011 10:54 PM
  • You are correct - extension Methods were created in 2008 product, so you cannot use them in 2005 version.

    There is a hack that you can use if you are using VS 2008 to create 2.0 target framework application that allows extension methods to be used - but you would still require the use of VS 2008 product.

     

    Wednesday, February 09, 2011 10:58 PM
  • You are correct - extension Methods were created in 2008 product, so you cannot use them in 2005 version.

    There is a hack that you can use if you are using VS 2008 to create 2.0 target framework application that allows extension methods to be used - but you would still require the use of VS 2008 product.

     


    Hi spotty,

    I'm "interested" to hear about this hack, although I'm not sure if I'd find it useful. Do you have a link to an article?



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Wednesday, February 09, 2011 11:05 PM
  • Hi John

    I am using VS2008 product and this project I started with 2005 version then I copied all code to 2008 

    It does not have any problem to continue the project but I trying to use your code then have a problem

    Is there some I need to use your code

    If I started new project and use it then do not have any problem.

    I hope  you understand what I am trying to say

    Thank you 

    Kwan

    Friday, February 11, 2011 12:10 AM
  • Hi kwankwon,

    If you ignore the bold text at the very top of the 1st thread then if you follow what I have written in

    the 1st two posts of this thread , word-for-word, then it should all work now that you are using VB.Net 2008.

    The only other thing I could possibly do to make this clearer would be to create a video and post it to YouTube.



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Friday, February 11, 2011 1:01 PM
  • John,

    Thank you very much again but I still have same problem (Type 'Extension' is not define message)

    I followed word-for-word as you suggested but I still feel so dumb :) 

    Off course I do not understand a lot of logics since  I am a just beginner try to do too much I guess

    Have wonderful day 

    Oh by the way I live in the USA

    kwan

    Thursday, February 17, 2011 5:09 PM
  • Hi kwan,

    Add one Button to your Form.

    Resize the Button to make it larger if you wish.

    Then just PASTE this lot into your Form1 code window as I have said earlier, then run it.

     

    Public Class Form1
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        Button1.BackColor = Color.White
        Shape(Button1, 2, 0)
    
      End Sub
    
      Public Const Pi As Double = Math.PI
      Public Const DegreesToRadians As Double = 180 / Pi
    
      Public Enum TriangleDirection
        Up
        Right
        Down
        Left
        TopRight
        BottomRight
        BottomLeft
        TopLeft
      End Enum
    
      Public Sub Shape(ByVal ctrl As Control, Optional ByVal NumberOfSides As Integer = 3, Optional ByVal OffsetAngleInDegrees As Double = 0)
    
        If NumberOfSides < 2 Then Throw New Exception("Number of sides can only be 2 or more.")
    
        Dim MyPath As New Drawing2D.GraphicsPath
        Dim MyAngle As Double = OffsetAngleInDegrees / DegreesToRadians
    
        If NumberOfSides = 2 Then
    
          Dim MyPoints() As Point
          Dim MyPointsList As New List(Of Point)
    
          If ctrl.Width = ctrl.Height Then
            MyPath.AddEllipse(New Rectangle(0, 0, ctrl.Width, ctrl.Height))
          ElseIf ctrl.Width > ctrl.Height Then
            MyPointsList.Add(New Point(0, ctrl.Height \ 2))
            MyPointsList.Add(New Point(ctrl.Width \ 2, 0))
            MyPointsList.Add(New Point(ctrl.Width, ctrl.Height \ 2))
            MyPoints = MyPointsList.ToArray
            MyPath.AddCurve(MyPoints)
            MyPointsList.Clear()
    
            MyPointsList.Add(New Point(ctrl.Width, ctrl.Height \ 2))
            MyPointsList.Add(New Point(ctrl.Width \ 2, ctrl.Height))
            MyPointsList.Add(New Point(0, ctrl.Height \ 2))
            MyPoints = MyPointsList.ToArray
            MyPath.AddCurve(MyPoints)
            MyPointsList.Clear()
          ElseIf ctrl.Width < ctrl.Height Then
            MyPointsList.Add(New Point(ctrl.Width \ 2, 0))
            MyPointsList.Add(New Point(ctrl.Width, ctrl.Height \ 2))
            MyPointsList.Add(New Point(ctrl.Width \ 2, ctrl.Height))
            MyPoints = MyPointsList.ToArray
            MyPath.AddCurve(MyPoints)
            MyPointsList.Clear()
    
            MyPointsList.Add(New Point(ctrl.Width \ 2, ctrl.Height))
            MyPointsList.Add(New Point(0, ctrl.Height \ 2))
            MyPointsList.Add(New Point(ctrl.Width \ 2, 0))
            MyPoints = MyPointsList.ToArray
            MyPath.AddCurve(MyPoints)
            MyPointsList.Clear()
          End If
        End If
    
        Dim radius1 As Integer = ctrl.Height \ 2
        Dim radius2 As Integer = ctrl.Width \ 2
        Dim xInt, yInt As Integer
        Dim xDoub, yDoub As Double
    
        For angle As Double = MyAngle To ((2 * Pi) + MyAngle) Step ((2 * Pi) / NumberOfSides)
    
          xDoub = radius2 * Math.Cos(angle) + radius2
          yDoub = radius1 * Math.Sin(angle) + radius1
          xInt = CInt(Int(xDoub))
          yInt = CInt(Int(yDoub))
          MyPath.AddLine(New Point(xInt, yInt), New Point(xInt, yInt))
    
        Next
    
        MyPath.CloseFigure()
        ctrl.Region = New Region(MyPath)
        MyPath.Dispose()
    
      End Sub
    
      Public Sub MakeTriangular(ByVal ctrl As Control, ByVal Triangle_Direction As TriangleDirection)
    
        Dim MyPath As New Drawing2D.GraphicsPath
    
        Select Case Triangle_Direction
          Case Is = TriangleDirection.Up
            MyPath.AddLine(0, ctrl.Height, 0, ctrl.Height)
            MyPath.AddLine(0, ctrl.Height, ctrl.Width \ 2, 0)
            MyPath.AddLine(ctrl.Width \ 2, 0, ctrl.Width, ctrl.Height)
          Case TriangleDirection.Right
            MyPath.AddLine(0, ctrl.Height, 0, 0)
            MyPath.AddLine(0, 0, ctrl.Width, ctrl.Height \ 2)
    
          Case TriangleDirection.Down
            MyPath.AddLine(0, 0, ctrl.Width, 0)
            MyPath.AddLine(ctrl.Width, 0, ctrl.Width \ 2, ctrl.Height)
    
          Case TriangleDirection.Left
            MyPath.AddLine(ctrl.Width, 0, ctrl.Width, ctrl.Height)
            MyPath.AddLine(ctrl.Width, ctrl.Height, 0, ctrl.Height \ 2)
    
          Case TriangleDirection.TopRight
            MyPath.AddLine(0, 0, ctrl.Width, 0)
            MyPath.AddLine(ctrl.Width, 0, ctrl.Width, ctrl.Height)
    
          Case TriangleDirection.TopLeft
            MyPath.AddLine(0, 0, ctrl.Width, 0)
            MyPath.AddLine(ctrl.Width, 0, 0, ctrl.Height)
    
          Case TriangleDirection.BottomRight
            MyPath.AddLine(ctrl.Width, 0, ctrl.Width, ctrl.Height)
            MyPath.AddLine(ctrl.Width, ctrl.Height, 0, ctrl.Height)
    
          Case TriangleDirection.BottomLeft
            MyPath.AddLine(0, 0, ctrl.Width, ctrl.Height)
            MyPath.AddLine(ctrl.Width, ctrl.Height, 0, ctrl.Height)
    
        End Select
        MyPath.CloseFigure()
        ctrl.Region = New Region(MyPath)
        MyPath.Dispose()
    
      End Sub
    
      Public Sub Shape(ByVal AnyForm As System.Windows.Forms.Form, Optional ByVal NumberOfSides As Integer = 3, Optional ByVal OffsetAngleInDegrees As Double = 0)
    
        If NumberOfSides < 2 Then Throw New Exception("Number of sides can only be 2 or more.")
    
        Dim MyPath As New Drawing2D.GraphicsPath
        Dim MyAngle As Double = OffsetAngleInDegrees / DegreesToRadians
    
        If NumberOfSides = 2 Then
    
          Dim MyPoints() As Point
          Dim MyPointsList As New List(Of Point)
    
          If AnyForm.Width = AnyForm.Height Then
            MyPath.AddEllipse(New Rectangle(0, 0, AnyForm.Width, AnyForm.Height))
          ElseIf AnyForm.Width > AnyForm.Height Then
            MyPointsList.Add(New Point(0, AnyForm.Height \ 2))
            MyPointsList.Add(New Point(AnyForm.Width \ 2, 0))
            MyPointsList.Add(New Point(AnyForm.Width, AnyForm.Height \ 2))
            MyPoints = MyPointsList.ToArray
            MyPath.AddCurve(MyPoints)
            MyPointsList.Clear()
    
            MyPointsList.Add(New Point(AnyForm.Width, AnyForm.Height \ 2))
            MyPointsList.Add(New Point(AnyForm.Width \ 2, AnyForm.Height))
            MyPointsList.Add(New Point(0, AnyForm.Height \ 2))
            MyPoints = MyPointsList.ToArray
            MyPath.AddCurve(MyPoints)
            MyPointsList.Clear()
          ElseIf AnyForm.Width < AnyForm.Height Then
            MyPointsList.Add(New Point(AnyForm.Width \ 2, 0))
            MyPointsList.Add(New Point(AnyForm.Width, AnyForm.Height \ 2))
            MyPointsList.Add(New Point(AnyForm.Width \ 2, AnyForm.Height))
            MyPoints = MyPointsList.ToArray
            MyPath.AddCurve(MyPoints)
            MyPointsList.Clear()
    
            MyPointsList.Add(New Point(AnyForm.Width \ 2, AnyForm.Height))
            MyPointsList.Add(New Point(0, AnyForm.Height \ 2))
            MyPointsList.Add(New Point(AnyForm.Width \ 2, 0))
            MyPoints = MyPointsList.ToArray
            MyPath.AddCurve(MyPoints)
            MyPointsList.Clear()
          End If
        End If
    
        Dim radius1 As Integer = AnyForm.Height \ 2
        Dim radius2 As Integer = AnyForm.Width \ 2
        Dim xInt, yInt As Integer
        Dim xDoub, yDoub As Double
    
        For angle As Double = MyAngle To ((2 * Pi) + MyAngle) Step ((2 * Pi) / NumberOfSides)
    
          xDoub = radius2 * Math.Cos(angle) + radius2
          yDoub = radius1 * Math.Sin(angle) + radius1
          xInt = CInt(Int(xDoub))
          yInt = CInt(Int(yDoub))
          MyPath.AddLine(New Point(xInt, yInt), New Point(xInt, yInt))
    
        Next
    
        MyPath.CloseFigure()
        AnyForm.Region = New Region(MyPath)
        MyPath.Dispose()
    
      End Sub
    
      Public Sub MakeTriangular(ByVal AnyForm As System.Windows.Forms.Form, ByVal Triangle_Direction As TriangleDirection)
    
        Dim MyPath As New Drawing2D.GraphicsPath
    
        Select Case Triangle_Direction
          Case Is = TriangleDirection.Up
            MyPath.AddLine(0, AnyForm.Height, 0, AnyForm.Height)
            MyPath.AddLine(0, AnyForm.Height, AnyForm.Width \ 2, 0)
            MyPath.AddLine(AnyForm.Width \ 2, 0, AnyForm.Width, AnyForm.Height)
          Case TriangleDirection.Right
            MyPath.AddLine(0, AnyForm.Height, 0, 0)
            MyPath.AddLine(0, 0, AnyForm.Width, AnyForm.Height \ 2)
    
          Case TriangleDirection.Down
            MyPath.AddLine(0, 0, AnyForm.Width, 0)
            MyPath.AddLine(AnyForm.Width, 0, AnyForm.Width \ 2, AnyForm.Height)
    
          Case TriangleDirection.Left
            MyPath.AddLine(AnyForm.Width, 0, AnyForm.Width, AnyForm.Height)
            MyPath.AddLine(AnyForm.Width, AnyForm.Height, 0, AnyForm.Height \ 2)
    
          Case TriangleDirection.TopRight
            MyPath.AddLine(0, 0, AnyForm.Width, 0)
            MyPath.AddLine(AnyForm.Width, 0, AnyForm.Width, AnyForm.Height)
    
          Case TriangleDirection.TopLeft
            MyPath.AddLine(0, 0, AnyForm.Width, 0)
            MyPath.AddLine(AnyForm.Width, 0, 0, AnyForm.Height)
    
          Case TriangleDirection.BottomRight
            MyPath.AddLine(AnyForm.Width, 0, AnyForm.Width, AnyForm.Height)
            MyPath.AddLine(AnyForm.Width, AnyForm.Height, 0, AnyForm.Height)
    
          Case TriangleDirection.BottomLeft
            MyPath.AddLine(0, 0, AnyForm.Width, AnyForm.Height)
            MyPath.AddLine(AnyForm.Width, AnyForm.Height, 0, AnyForm.Height)
    
        End Select
        MyPath.CloseFigure()
        AnyForm.Region = New Region(MyPath)
        MyPath.Dispose()
    
      End Sub
    
    End Class

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Thursday, February 17, 2011 5:27 PM
  • Hi ALL,

    Here is a thread that uses a lot of code from this thread.

     

    This thread shows how to rotate a control by its REGION if you make it a regular shape.    :-)    ;-)    :-D

    >>

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/753a6e65-a97d-406b-82bf-a699a91c1cf7

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    XNA is coming to VB.Net

    App Hub forums



    Friday, June 24, 2011 9:29 PM