奇怪的WithEvents的事情

时间:2016-11-05 08:18:02

标签: vb.net winforms

很简单的情况。
Button内的一个Form类,用于触发MsgBox

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MessageBox.Show("TEST")
    End Sub
End Class

然后我创建一个新类Class1并将Sub Button1_Click移动到该类

Public Class Form1

End Class
Public Class Class1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Form1.Button1.Click
        MessageBox.Show("TEST")
    End Sub
End Class

我得到了失败

  

失败BC30506 Handles子句需要在包含类型或其基类型之一中定义的WithEvents变量。

然后我修改了这样:

Public Class Class1
    Public WithEvents Form1.Button As EventThrower()
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Form1.Button1.Click
        MessageBox.Show("TEST")
    End Sub
End Class

现在我得到2次失败

  

错误BC30412'WithEvents'变量必须有as子句。

     

错误BC31412类中的“句柄”必须指定WithEvents变量,   'MyBase','MyClass'或'Me'使用单个标识符进行限定。

有人可以用简单的方式解释我吗?

更新1:
来自Visual Vincent的解决方案删除了​​错误,但是通过单击Form1上的Button1,MessageBox不会被触发,所以实际上它不起作用。

Public Class Class1
    Public WithEvents Button1 As Button = Form1.Button1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MessageBox.Show("TEST")
    End Sub
End Class

如何在Class1中触发Form1上的Button1?

3 个答案:

答案 0 :(得分:1)

但是通过单击Form1上的Button1,MessageBox不会被触发,所以实际上它不起作用。

Public WithEvents Button1 As Button = Form1.Button1

根据您的代码Form1是一个类。 Form1.Button1表示类Form1具有静态变量Button1的位置。

显然,您的Button1课程中有Form1,只能通过Form1的实例访问。要获得Form1的正确实例,您需要将该实例作为参数传递给Class1

然后你的代码:

    Public WithEvents Form1.Button As EventThrower() 
  1. 变量名称不能包含点(如@Visual Vincent所述)
  2. 您只能使用WithEvents声明单个变量 - 而不是数组 ... As EventThrower()声明了EventThrower
  3. 类型的数组

    因此,您需要将该行更改为EventThrower

    的类型
    Public WithEvents MyEventThrower As EventThrower
    

    您尝试使用按钮实例并在另一个类中处理Click事件的代码的主要问题,这不是一个好习惯。

    如果您希望将Click处理程序的代码移动到另一个类,请使用还原依赖项来执行此操作。您的表单将依赖于包含逻辑的类

    Public Class LogicClass
        Public Function ExecuteSomeLogicAndReturnValue() As String
            Return "TEST"
        End Sub
    End Class
    
    Public Class MyForm Inherits Form
        Private _Logic As New LogicClass()
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Me.MyButton.Click
            Dim value As String = _Logic.ExecuteSomeLogicAndReturnValue()
            MessageBox.Show(value)
        End Sub
    End Class
    

答案 1 :(得分:1)

我不知道EventThrower是什么,但您的变量声明无效。在访问修饰符(在本例中为Public)之后,需要一个名称,名称不能包含点。

我建议你做这样的事情:

Public Class Class1
    Public WithEvents Button1 As Button = Form1.Button1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MessageBox.Show("TEST")
    End Sub
End Class

要在没有类实例的情况下完成此工作,您可以将所有内容标记为Shared编辑:我怀疑你必须至少访问一次课程,否则处理程序不会被附加。调用一个空方法就足够了:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Class1.Initialize()
    End Sub
End Class

Public Class Class1
    Public Shared WithEvents Button1 As Button = Form1.Button1
    Private Shared Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MessageBox.Show("TEST")
    End Sub

    Public Shared Sub Initialize()
    End Sub
End Class

答案 2 :(得分:1)

试试这个:

Public Class Form1

'Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'    MessageBox.Show("TEST")
'End Sub

   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      AddHandler Button1.Click, AddressOf Class1.ButtonClick
  End Sub

End Class

Public Class Class1

   Public Shared Sub ButtonClick(sender As Object, e As EventArgs)
      MessageBox.Show("Clicked from ButtonClick")
   End Sub

End Class