[VB.NET] 컨트롤 삭제 여부 확인하는 방법

하나를하더라도최선을 2024-05-08 13:22:03 22,381 0 0

컨트롤이 삭제되었는지 확인하기 위해서는 해당 컨트롤이 유효한지 확인해야 합니다. 이를 위해 다음과 같은 VB.NET 코드를 사용할 수 있습니다.


Module ControlExtensions
    <Extension()>
    Public Function IsDisposed(ByVal control As Control) As Boolean
        If control Is Nothing Then Return True
        If control.Disposing OrElse control.IsDisposed Then Return True
        Dim parent = control.Parent
        While parent IsNot Nothing
            If parent.Disposing OrElse parent.IsDisposed Then Return True
            parent = parent.Parent
        End While
        Return False
    End Function
End Module


이 확장 메서드를 사용하여 특정 컨트롤이 삭제되었는지 확인할 수 있습니다. 아래는 예제 코드입니다.


' 사용 예제
If myControl.IsDisposed() Then
    ' 컨트롤이 삭제되었음을 처리
Else
    ' 컨트롤이 여전히 존재함을 처리
End If


위의 예제 코드에서 myControl은 삭제 여부를 확인하려는 컨트롤입니다. 이 코드는 컨트롤이 삭제되었으면 True를 반환하고, 그렇지 않으면 False를 반환합니다.


example:)

If myControl Is Nothing OrElse myControl.IsDisposed Then myControl = New TextBox


댓글 0개

첫 번째 댓글을 작성해보세요!