.netvb.netwinformsclassellipse

Why Creating Component Class Ellipse in WinForm Applications make not responding in vb.net


I'm Trying to Creating Component Class Ellipse in WinForm Applications make not responding in vb.net

I've tried it on a different computer, still the same

makes it not responding and also restarts visual studio. Is there something wrong with the code? Please guide me

Thanks

Imports System.ComponentModel
Imports System.Runtime.InteropServices

Public Class Ellipse
    Inherits Component

    <DllImport("gdi32.dll", EntryPoint:="CreateRoundRectRgn")>
    Private Shared Function CreateRoundRectRgn(ByVal nL As Integer, ByVal nT As Integer, ByVal nR As Integer, ByVal nB As Integer, ByVal nWidthEllipse As Integer, ByVal nHeightEllipse As Integer) As IntPtr
    End Function

    Private control As Control
    Private cornerRadius As Integer = 25
    Public Property TargetControl As Control
        Get
            Return control
        End Get
        Set(ByVal value As Control)
            control = value
            AddHandler control.SizeChanged, Sub(sender, eventArgs) control.Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, control.Width, control.Height, cornerRadius, cornerRadius))
        End Set
    End Property
    Public Property CornerRedius As Integer
        Get
            Return CornerRedius
        End Get
        Set(ByVal value As Integer)
            CornerRedius = value
            If control IsNot Nothing Then
                control.Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, control.Width, control.Height, cornerRadius, cornerRadius))
            End If
        End Set
    End Property
End Class

when I changed the cornerradius value from 0 to 25 it became not responding and visual studio restarted.

result not responding ellipse


Solution

  • I think I see the issue. You have this field:

    Private cornerRadius As Integer = 25
    

    and then this property:

    Public Property CornerRedius As Integer
    

    Notice the misspelling in the second case. In your property setter, you have this:

    CornerRedius = value
    

    Every time you set the property, you set the property. That's the infinite loop I mentioned in my comment on the question. VS flagged that as a warning for me, so I would assume that it did the same for you but you ignored it. I also notice that you are returning the property rather than the field in the getter too. That explains why your property was blank in the designer to begin with instead of showing 25, which confused me when looking at your video.

    You obviously intended to set the field rather than the property there, but you probably didn't even notice your misspelling. You seem to have tried to give your field and your property the same name and that would have failed had you not misspelled the property name. Most people will name backing fields such that they are easily identifiable as such, e.g.

    Private _cornerRadius As Integer = 25
    
    Public Property CornerRadius As Integer
        Get
            Return _cornerRadius
        End Get
        Set(ByVal value As Integer)
            _cornerRadius = value
    
            If control IsNot Nothing Then
                control.Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, control.Width, control.Height, _cornerRadius, _cornerRadius))
            End If
        End Set
    End Property
    

    If you don't like using the leading underscore, you could use cornerRadiusValue instead.