|
-
Apr 4th, 2023, 07:29 AM
#1
Thread Starter
Hyperactive Member
[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?
-
Apr 4th, 2023, 07:34 AM
#2
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)
-
Apr 4th, 2023, 09:09 AM
#3
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
 
-
Apr 4th, 2023, 10:08 AM
#4
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
-
Apr 5th, 2023, 11:16 AM
#5
Thread Starter
Hyperactive Member
Re: How to get/throw handler on a Property?
 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...
Last edited by pourkascheff; Apr 5th, 2023 at 11:21 AM.
-
Apr 5th, 2023, 11:32 AM
#6
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
-
Apr 5th, 2023, 01:41 PM
#7
Re: How to get/throw handler on a Property?
 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.
My usual boring signature: Nothing
 
-
Apr 5th, 2023, 08:51 PM
#8
Re: How to get/throw handler on a Property?
 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.
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
-
Apr 6th, 2023, 01:31 AM
#9
Thread Starter
Hyperactive Member
Re: How to get/throw handler on a Property?
 Originally Posted by Shaggy Hiker
Are you from New England?
I'm afraid not.
 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.)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|