我有一个像下面这样的类,其中包含我的全局变量作为属性。我将这个“ ModelFullPath”从变量转换为属性,因为我不知道如何通过变量更改引发事件。(如果您有更多逻辑建议,我将不胜感激。)
class ForgotPwdTest(APITestCase):
""" Test module for initiating forgot password """
def setUp(self):
self.valid_payload = {
'email': 'valid@gmail.com'
}
self.invalid_email_payload = {
'email': 'invalid@yahoo.com'
}
self.blocked_user_payload = {
'email': 'blocked@gmail.com'
}
self.deleted_user_payload = {
'email': 'deleted@gmail.com'
}
user_valid = {'first_name': 'John', 'email': 'valid@gmail.com','password' : 'password', 'status': 1}
self.user_valid = User.objects.create_user(**user_valid)
# create users for other test cases
user_blocked = {'first_name': 'John', 'email': 'blocked@gmail.com','password' : 'password', 'status' : 0}
self.user_valid = User.objects.create_user(**user_blocked)
user_deleted = {'first_name': 'John', 'email': 'deleted@gmail.com','password' : 'password', 'status' : 2}
self.user_valid = User.objects.create_user(**user_deleted)
self.url = reverse('forgotpwd')
def test_valid_forgotpwd(self):
"""
Ensure a valid user can start a forgot password process: WORKS
"""
response = self.client.post(self.url , self.valid_payload, format='json')
self.assertEqual(response.status_code, 200)
'''
def test_missing_email(self):
""" The email is not registered in the system : WORKS """
response = self.client.post(
self.url,
self.invalid_email_payload
)
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json()['detail'], _('errorNoAccount'))
'''
def test_blocked_user(self):
""" Blocked user cannot initiate forgot password token """
response = self.client.post(
self.url ,
self.blocked_user_payload
)
self.assertEqual(response.status_code, 400) # fails response is string
self.assertContains(response.json()['detail'], _('userBlocked'))
def test_deleted_user(self):
""" Deleted user cannot initiate forgot password token """
response = self.client.post(
self.url ,
self.deleted_user_payload,
content_type='application/json'
)
self.assertEqual(response.status_code, 400) # fails response is string
self.assertEqual(response['detail'], _('userMissing'))
在另一个类中,我有“ Button2”,它获取字符串值文本框,并根据textbox1.Text值设置Globals的ModelFullPath属性。另一方面,Button1正在将Globals.ModelFullPath属性写入label1.text值。
在这里,如果ModelFullPath发生更改,我想发出一个事件,我想进行一些操作,例如更改工具的背景颜色等。目前,我将其设置为带有消息框的“ It Changed”。但是主要的问题是我无法从下面的另一个类中处理它。
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
如何处理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
事件?因为它无法识别此事件。
答案 0 :(得分:2)
您需要使用AddHandler()来组织活动。您的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