为什么在WinForm应用程序中创建组件类Ellipse在vb.net中没有响应

问题描述 投票:0回答:1

我正在尝试在 WinForm 应用程序中创建组件类椭圆,但在 vb.net 中没有响应

我在不同的电脑上试过了,还是一样

使其不响应并重新启动 Visual Studio。代码有问题吗?请指导我

谢谢

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

当我将角半径值从 0 更改为 25 时,它变得没有响应并且 Visual Studio 重新启动。

.net vb.net winforms class ellipse
1个回答
0
投票

我想我看到了这个问题。你有这个字段:

Private cornerRadius As Integer = 25

然后是这个属性:

Public Property CornerRedius As Integer

注意第二种情况中的拼写错误。在你的属性设置器中,你有这个:

CornerRedius = value

每次设置属性时,都设置了属性。这就是我在对该问题的评论中提到的无限循环。 VS 将此标记为对我的警告,所以我认为它对你做了同样的事情,但你忽略了它。我还注意到您也返回了属性而不是 getter 中的字段。这就解释了为什么你的属性一开始在设计器中是空白的,而不是显示

25
,这让我在观看你的视频时感到困惑。

您显然打算设置字段而不是那里的属性,但您可能甚至没有注意到您的拼写错误。您似乎尝试为您的字段和属性指定相同的名称,如果您没有拼写错误属性名称,那么该操作将会失败。大多数人都会命名支持字段,以便它们很容易被识别,例如

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

如果您不喜欢使用前导下划线,可以使用

cornerRadiusValue
代替。

© www.soinside.com 2019 - 2024. All rights reserved.