[英]Handling property changed from another class in vb.net
I have a class like below which contains my global variables as property. 我有一个像下面这样的类,其中包含我的全局变量作为属性。 I made this "ModelFullPath" from variable to property because i dont know how to raise events with the variable changes.(If you have more logic suggestions i'll appreciate.)
我将这个“ ModelFullPath”从变量转换为属性,因为我不知道如何通过变量更改引发事件。(如果您有更多逻辑建议,我将不胜感激。)
Public Class Globals
Private Shared _modelfullpath As String = String.Empty
Public Shared Event ModelPathChanged(ByVal _modelfullpath As String)
Public Shared Property ModelFullPath() As String
Get
Return _modelfullpath
End Get
Set(ByVal value As String)
_modelfullpath = value
RaiseEvent ModelPathChanged(_modelfullpath)
End Set
End Property
Public Shared Sub TestIt() Handles MyClass.ModelPathChanged
' Some codes in here
MessageBox.Show("It Changed")
End Sub
End Class
In my other class i have "Button2" which gets the string value textbox and sets the my Globals' ModelFullPath property according to textbox1.Text value. 在我的另一个类中,我有“ Button2”,它获取字符串值文本框并根据textbox1.Text值设置my Globals的ModelFullPath属性。 On the other hand Button1 is writing Globals.ModelFullPath property to a label1.text value.
另一方面,Button1正在将Globals.ModelFullPath属性写入label1.text值。
In here i'd like to put an event if ModelFullPath is changed, i'd like to make some actions like changing tool's background colors etc. Currently i set it the show "It Changed" with Message Box. 如果要更改ModelFullPath,我想在这里放置一个事件,我想执行一些操作,例如更改工具的背景色等。当前,我将其设置为带有消息框的“ It Changed”显示。 But the main problem is i cant handle it from another class like below.
但是主要的问题是我无法从下面的另一个类中处理它。
Public Class MainTool
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label1.Text = Globals.ModelFullPath
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Globals.ModelFullPath = TextBox1.Text
End Sub
Private Sub VariableChanged() Handles Globals.VariableChanged
Globals.TestIt()
End Sub
End Class
How can handle Globals.VariableChanged
event? 如何处理
Globals.VariableChanged
事件? Because it doesnt recognize this event. 因为它无法识别此事件。
You need to use AddHandler() to wire up the event. 您需要使用AddHandler()来关联事件。 The Load() event of your Form is a good place to do that:
Form的Load()事件是执行此操作的好地方:
Public Class MainTool
Private Sub MainTool_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler Globals.ModelPathChanged, AddressOf Globals_ModelPathChanged
End Sub
Private Sub Globals_ModelPathChanged(_modelfullpath As String)
TextBox1.Text = _modelfullpath
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Globals.ModelFullPath = "Hello!"
End Sub
End Class
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.