none
Structure and Class at Design and run Time.

    Question

  • Hello everyone,
    i am trying to make controls visible at run time and design time in 2 different ways.
    1) Using class i can make visible or not the buttons on run time and not in design time.
    2) Using Structure i can't make any button visible or not in run and design time ONLY In design mode.
    In the example is a usercontroler using both methods
    When i am in the design mode, when i change the visibility in VS property window, i want to see the reaction of it in the usercontrol on form if the control is visible or not.
    I can use both methods on run time through form load and do what i want with them. But not at design time.
    You can find this properties at the Visibility Category.
    How i can do it.
    Download Link
    https://1drv.ms/u/s!AlyczCxAW0y4gxXqnxFA8BSIMQX2
    OneDrive Download Link


    Thx
    Tony

    Sorry for my bad English skills.


    Tuesday, March 28, 2017 10:08 AM

Answers

  • You don't want to use the Structure because you want to be able to change the property values independently, so you want the class version.

    The reason you don't see a change is because you only call your "Reaction()" method when the "PropClassMethod" property value changes.  You want to call Reaction() whenever one of the property values on PropClassMethod changes - so that class needs to implement INotifyPropertyChanged and then you need to handle the PropertyChanged event.

    So your "ClassMethod" class should look like this:

    Imports System.ComponentModel
    
    <TypeConverter(GetType(ExpandableObjectConverter))>
    Public Class ClassMethod
        Implements INotifyPropertyChanged
    
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    
        Protected _ProCls1 As Boolean
        Protected _ProCls2 As Boolean
        Protected _ProCls3 As Boolean
        Protected _ProCls4 As Boolean
        Protected _ProCls5 As Boolean
    
        Public Property ProCls1 As Boolean
            Get
                Return _ProCls1
            End Get
            Set(value As Boolean)
                If Not value = _ProCls1 Then
                    _ProCls1 = value
                    OnPropertyChanged()
                End If
            End Set
        End Property
    
        Public Property ProCls2 As Boolean
            Get
                Return _ProCls2
            End Get
            Set(value As Boolean)
                If Not value = _ProCls2 Then
                    _ProCls2 = value
                    OnPropertyChanged()
                End If
            End Set
        End Property
    
        Public Property ProCls3 As Boolean
            Get
                Return _ProCls3
            End Get
            Set(value As Boolean)
                If Not value = _ProCls3 Then
                    _ProCls3 = value
                    OnPropertyChanged()
                End If
            End Set
        End Property
    
        Public Property ProCls4 As Boolean
            Get
                Return _ProCls4
            End Get
            Set(value As Boolean)
                If Not value = _ProCls4 Then
                    _ProCls4 = value
                    OnPropertyChanged()
                End If
            End Set
        End Property
    
        Public Property ProCls5 As Boolean
            Get
                Return _ProCls5
            End Get
            Set(value As Boolean)
                If Not value = _ProCls5 Then
                    _ProCls5 = value
                    OnPropertyChanged()
                End If
            End Set
        End Property
    
        Private Sub OnPropertyChanged(<Runtime.CompilerServices.CallerMemberName> Optional propertyName As String = Nothing)
            If propertyName IsNot Nothing Then OnPropertyChanged(New PropertyChangedEventArgs(propertyName))
        End Sub
    
        Protected Overridable Sub OnPropertyChanged(e As PropertyChangedEventArgs)
            RaiseEvent PropertyChanged(Me, e)
        End Sub
    End Class

    And then the UserControl code would be something like:

    Imports System.ComponentModel
    
    Public Class UserControl1
    
        Friend WithEvents m_PropClassMethod As New ClassMethod
    
        <Category("Visibility")>
        Public Property PropClassMethod As ClassMethod
            Get
                Return m_PropClassMethod
            End Get
            Set(value As ClassMethod)
                m_PropClassMethod = value
                Reaction()
            End Set
        End Property
    
        Private Sub Reaction()
            Button1.Visible = m_PropClassMethod.ProCls1
            Button2.Visible = m_PropClassMethod.ProCls2
            Button3.Visible = m_PropClassMethod.ProCls3
            Button4.Visible = m_PropClassMethod.ProCls4
            Button5.Visible = m_PropClassMethod.ProCls5
        End Sub
    
        Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Reaction()
        End Sub
    
        Private Sub m_PropClassMethod_PropertyChanged(sender As Object, e As ComponentModel.PropertyChangedEventArgs) Handles m_PropClassMethod.PropertyChanged
            Reaction()
        End Sub
    End Class


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Tuesday, March 28, 2017 1:40 PM
    Moderator

All replies

  • You don't want to use the Structure because you want to be able to change the property values independently, so you want the class version.

    The reason you don't see a change is because you only call your "Reaction()" method when the "PropClassMethod" property value changes.  You want to call Reaction() whenever one of the property values on PropClassMethod changes - so that class needs to implement INotifyPropertyChanged and then you need to handle the PropertyChanged event.

    So your "ClassMethod" class should look like this:

    Imports System.ComponentModel
    
    <TypeConverter(GetType(ExpandableObjectConverter))>
    Public Class ClassMethod
        Implements INotifyPropertyChanged
    
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    
        Protected _ProCls1 As Boolean
        Protected _ProCls2 As Boolean
        Protected _ProCls3 As Boolean
        Protected _ProCls4 As Boolean
        Protected _ProCls5 As Boolean
    
        Public Property ProCls1 As Boolean
            Get
                Return _ProCls1
            End Get
            Set(value As Boolean)
                If Not value = _ProCls1 Then
                    _ProCls1 = value
                    OnPropertyChanged()
                End If
            End Set
        End Property
    
        Public Property ProCls2 As Boolean
            Get
                Return _ProCls2
            End Get
            Set(value As Boolean)
                If Not value = _ProCls2 Then
                    _ProCls2 = value
                    OnPropertyChanged()
                End If
            End Set
        End Property
    
        Public Property ProCls3 As Boolean
            Get
                Return _ProCls3
            End Get
            Set(value As Boolean)
                If Not value = _ProCls3 Then
                    _ProCls3 = value
                    OnPropertyChanged()
                End If
            End Set
        End Property
    
        Public Property ProCls4 As Boolean
            Get
                Return _ProCls4
            End Get
            Set(value As Boolean)
                If Not value = _ProCls4 Then
                    _ProCls4 = value
                    OnPropertyChanged()
                End If
            End Set
        End Property
    
        Public Property ProCls5 As Boolean
            Get
                Return _ProCls5
            End Get
            Set(value As Boolean)
                If Not value = _ProCls5 Then
                    _ProCls5 = value
                    OnPropertyChanged()
                End If
            End Set
        End Property
    
        Private Sub OnPropertyChanged(<Runtime.CompilerServices.CallerMemberName> Optional propertyName As String = Nothing)
            If propertyName IsNot Nothing Then OnPropertyChanged(New PropertyChangedEventArgs(propertyName))
        End Sub
    
        Protected Overridable Sub OnPropertyChanged(e As PropertyChangedEventArgs)
            RaiseEvent PropertyChanged(Me, e)
        End Sub
    End Class

    And then the UserControl code would be something like:

    Imports System.ComponentModel
    
    Public Class UserControl1
    
        Friend WithEvents m_PropClassMethod As New ClassMethod
    
        <Category("Visibility")>
        Public Property PropClassMethod As ClassMethod
            Get
                Return m_PropClassMethod
            End Get
            Set(value As ClassMethod)
                m_PropClassMethod = value
                Reaction()
            End Set
        End Property
    
        Private Sub Reaction()
            Button1.Visible = m_PropClassMethod.ProCls1
            Button2.Visible = m_PropClassMethod.ProCls2
            Button3.Visible = m_PropClassMethod.ProCls3
            Button4.Visible = m_PropClassMethod.ProCls4
            Button5.Visible = m_PropClassMethod.ProCls5
        End Sub
    
        Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Reaction()
        End Sub
    
        Private Sub m_PropClassMethod_PropertyChanged(sender As Object, e As ComponentModel.PropertyChangedEventArgs) Handles m_PropClassMethod.PropertyChanged
            Reaction()
        End Sub
    End Class


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Tuesday, March 28, 2017 1:40 PM
    Moderator
  • Thanks PERFECT.
    Tuesday, March 28, 2017 2:12 PM