[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?
Re: How to get/throw handler on a Property?
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.
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
Re: How to get/throw handler on a Property?
Quote:
Originally Posted by
dbasnett
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:
Imports System.ComponentModel
Public Class UserControl1
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 = False
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") 'OTHERS KNOW IT BY THIS NAME NOW ON
If SI_UNITValue = True Then
Label1.Text = "TRUE" 'DO THINGS WITH NEW CONDITION
Else
Label1.Text = "FALSE" 'DO THINGS WITH NEW CONDITION
End If
End Set
End Property
End Class
Form code:
vb Code:
Public Class Form1
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
For Each UC In TableLayoutPanel1.Controls.OfType(Of UserControl1)()
UC.SI_UNIT = True
Next
End If
If CheckBox1.Checked = False Then
For Each UC In TableLayoutPanel1.Controls.OfType(Of UserControl1)()
UC.SI_UNIT = False
Next
End If
End Sub
End Class
It is a resolved thread for me but further comments are all still welcome here...
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
Re: How to get/throw handler on a Property?
Quote:
Originally Posted by
pourkascheff
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.
Re: How to get/throw handler on a Property?
Quote:
Originally Posted by
Shaggy Hiker
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.
Re: How to get/throw handler on a Property?
Quote:
Originally Posted by
Shaggy Hiker
Are you from New England?
I'm afraid not.
Quote:
Originally Posted by
passel
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.)