Results 1 to 9 of 9

Thread: [RESOLVED] How to get/throw handler on a Property?

  1. #1

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Resolved [RESOLVED] How to get/throw handler on a Property?

    Consider you have a distant measuring UserControl which in its class at first line you've entered
    Code:
    Public Property SI_UNIT As Boolean = True
    to manage it through main window, "Form1", to be either Imperial or Metric in calculations. (As a simple question. Of course numeric examples would be better)

    1) Is it correct to declare it as "Public"? Why not "Global"? I only know "Private"s which literally means private.
    2) (And the whole idea of this thread) How can we get/generate its event-sub when this specific parameter changes?

  2. #2
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    846

    Re: How to get/throw handler on a Property?

    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: How to get/throw handler on a Property?

    There is no "Global". People talk about global variables, but that's not part of the language. There is Shared, which would make it global, but that is a different thing entirely, as it is barely a part of the form, at that point. However, that might make more sense if you are talking about units. It may well be that all parts of the program will want to see that, so it probably does make more sense to make it Shared.
    My usual boring signature: Nothing

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: How to get/throw handler on a Property?

    This use the INotifyPropertyChanged interface.

    Test class

    Code:
    ' requires Imports System.ComponentModel
    Public Class DemoUnits
        Implements INotifyPropertyChanged
    
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    
        Private Sub NotifyPropertyChanged(ByVal info As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
        End Sub
    
        Private SI_UNITValue As Boolean = True
        Public Property SI_UNIT() As Boolean
            Get
                Return Me.SI_UNITValue
            End Get
            Set(ByVal value As Boolean)
                Me.SI_UNITValue = value
                NotifyPropertyChanged("SI_UNIT")
            End Set
        End Property
    
    End Class
    Test code

    Code:
        Private WithEvents myDemoUnits As New DemoUnits
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            myDemoUnits.SI_UNIT = False
        End Sub
    
        Private Sub myDemoUnits_PropertyChanged(sender As Object,
                                                e As PropertyChangedEventArgs) Handles myDemoUnits.PropertyChanged
            Dim foo As Boolean = DirectCast(GetPropertyValue(myDemoUnits, e.PropertyName), Boolean)
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Re: How to get/throw handler on a Property?

    Quote Originally Posted by dbasnett View Post
    This use the INotifyPropertyChanged interface.
    Wicked. It worked perfectly. Still no idea what does your second part "Test code" do but first part result is satisfying.

    UserControl code:
    vb Code:
    1. Imports System.ComponentModel
    2. Public Class UserControl1
    3.     Implements INotifyPropertyChanged
    4.     Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    5.     Private Sub NotifyPropertyChanged(ByVal info As String)
    6.         RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    7.     End Sub
    8.     Private SI_UNITValue As Boolean = False
    9.     Public Property SI_UNIT() As Boolean
    10.         Get
    11.             Return Me.SI_UNITValue
    12.         End Get
    13.         Set(ByVal value As Boolean)
    14.             Me.SI_UNITValue = value
    15.             NotifyPropertyChanged("SI_UNIT") 'OTHERS KNOW IT BY THIS NAME NOW ON
    16.             If SI_UNITValue = True Then
    17.                 Label1.Text = "TRUE" 'DO THINGS WITH NEW CONDITION
    18.             Else
    19.                 Label1.Text = "FALSE" 'DO THINGS WITH NEW CONDITION
    20.             End If
    21.         End Set
    22.     End Property
    23. End Class
    Form code:
    vb Code:
    1. Public Class Form1
    2.     Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    3.         If CheckBox1.Checked = True Then
    4.             For Each UC In TableLayoutPanel1.Controls.OfType(Of UserControl1)()
    5.                 UC.SI_UNIT = True
    6.             Next
    7.         End If
    8.         If CheckBox1.Checked = False Then
    9.             For Each UC In TableLayoutPanel1.Controls.OfType(Of UserControl1)()
    10.                 UC.SI_UNIT = False
    11.             Next
    12.         End If
    13.     End Sub
    14. End Class
    It is a resolved thread for me but further comments are all still welcome here...
    Last edited by pourkascheff; Apr 5th, 2023 at 11:21 AM.

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [RESOLVED] How to get/throw handler on a Property?

    This is unnecessary:
    Code:
    If CheckBox1.Checked = True Then
    It should be just
    Code:
    If CheckBox1.Checked Then

    Also the end iff.. if .... just make it an else...
    Code:
            If CheckBox1.Checked Then
                For Each UC In TableLayoutPanel1.Controls.OfType(Of UserControl1)()
                    UC.SI_UNIT = True
                Next
            Else
                For Each UC In TableLayoutPanel1.Controls.OfType(Of UserControl1)()
                    UC.SI_UNIT = False
                Next
            End If
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: How to get/throw handler on a Property?

    Quote Originally Posted by pourkascheff View Post
    Wicked.
    Wicked???? Are you from New England? Is that term used in other parts of the world? It's wicked common in the New England area of the US, and I've never heard it used anywhere else.
    My usual boring signature: Nothing

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: How to get/throw handler on a Property?

    Quote Originally Posted by Shaggy Hiker View Post
    Wicked???? Are you from New England? Is that term used in other parts of the world? It's wicked common in the New England area of the US, and I've never heard it used anywhere else.
    I'm pretty sure I've heard Ron Weasley say it often..., so fairly certain it is common in Old England as well.

    p.s. Just did a search on "ron weasley wicked" and it seems it may have become a meme.
    Last edited by passel; Apr 5th, 2023 at 08:54 PM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  9. #9

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Re: How to get/throw handler on a Property?

    Quote Originally Posted by Shaggy Hiker View Post
    Are you from New England?
    I'm afraid not.

    Quote Originally Posted by passel View Post
    I'm pretty sure I've heard Ron Weasley say it often
    Bingo, strictly a HP fan, aye?

    We are getting off the topic lads. I cannot "handle" administrators stare of disappointment. (Cause me to throw exception.)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width