none
Problems with a Public Property RRS feed

  • Question

  • Hello! Thanks for entering and sorry for my bad english.

    I'm trying to build a user control. The problem is this has a property and I want that when it changes, an event is called, but this never happens. So I thought the problem might be the event itself. But not.

    Within the Set of property, I wrote all kinds of codes, like MsgBox, debug.Write and no one works.

    The code is the following:

    Public Class Demo
        Inherits Control
        Private G As Graphics
        Private _Titles As New List(Of title)
        Public Event titlesListChanged As EventHandler
        'other variables
    
        Class title
            Property text As String
            Property size As Integer = 0
        End Class
    
        <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
        Public Property Titles As List(Of title)
            Get
                Return _Titles
            End Get
            Set(value As List(Of title))
                '------------------------------------------
                Debug.Write("try")
                _Titles = value
                Debug.Write("try")
                RaiseEvent titlesListChanged(Me, EventArgs.Empty)
                 '------------------------------------------
                 'All I wrote above not work
            End Set
        End Property
    
      
        Private Sub OnTitlesListChanged Handles Me.titlesListChanged
            MsgBox("add")
        End Sub


    I tried removing the "DesignerSerializationVisibility" but it didn't works.

    I created another Property as example, a String, and this time all code worked.

    An interesting fact too, is that even if I remove the "_Titles = value" the Set still working, and the _Titles variable still worth the same as property Titles.

    Any solutions? Thanks in advance!



    • Edited by SalvadorB Wednesday, February 14, 2018 12:46 AM
    Wednesday, February 14, 2018 12:41 AM

Answers

  • I want to know when the list changes. I thought changing the list will change the property.

    That code doesn't change the list.  It only changes an item in the list.   The list object is still the same object.

    If you want to raise an event for a change in a list item then you need a custom list class that inherits from List and overloads the Add method to raise the ItemAdded event, or whatever other list methods you want to raise an event for.  The custom list event will be handled in your custom class, and that class will raise the titlesListChanged event that the user can use.

    • Marked as answer by SalvadorB Wednesday, February 14, 2018 3:02 AM
    Wednesday, February 14, 2018 2:55 AM

All replies

  • I'm trying to build a user control. The problem is this has a property and I want that when it changes, an event is called, but this never happens.

    What is the change you are making?  Changing an item in the list will not be seen as a change in the property.

    Wednesday, February 14, 2018 1:25 AM
  • Oh, I see.

    Anyway, I'm trying to put code there, and it don't do anything, do you know why?

    Thanks for answer!

    Wednesday, February 14, 2018 2:00 AM
  • Anyway, I'm trying to put code there, and it don't do anything, do you know why?

    Insert a breakpoint in the property setter - does the code execute to the breakpoint?  If not, the property is not being changed.  That will happen if you are only changing an item in the list, not the list.   You haven't shown any code that changes anything that might cause the event to happen.

    Wednesday, February 14, 2018 2:12 AM
  • As you say, I inserted a breakpoint, first in the getter, and it worked, after in the setter and this time not. 

    I want to know when the list changes. I thought changing the list will change the property.

    How can I do this?

    The way I did (wrong, I see now), is the following: 

     Dim item As New Demo.title
     With item
       .size = CInt(TextBox3.Text)
       .text = TextBox4.Text
     End With
     Demo1.Titles.Add(item)

    Wednesday, February 14, 2018 2:38 AM
  • I want to know when the list changes. I thought changing the list will change the property.

    That code doesn't change the list.  It only changes an item in the list.   The list object is still the same object.

    If you want to raise an event for a change in a list item then you need a custom list class that inherits from List and overloads the Add method to raise the ItemAdded event, or whatever other list methods you want to raise an event for.  The custom list event will be handled in your custom class, and that class will raise the titlesListChanged event that the user can use.

    • Marked as answer by SalvadorB Wednesday, February 14, 2018 3:02 AM
    Wednesday, February 14, 2018 2:55 AM
  • Thanks! I will try it
    Wednesday, February 14, 2018 3:03 AM