使用vb.net对第三方应用程序表单事件作出响应



我想知道我应该如何对VB.net应用程序进行编码,以对第三方应用程序中的表单加载事件做出反应(也用VB.net编写)

为了测试,我创建了两个基本程序,一个有两个表单(程序A),另一个(程序B)尝试侦听程序A的适当表单加载事件。我尝试过使用WithEvents,但当程序的第二个表单加载时,它不会被激发。

这是程序A的代码:

Public Class StartPage
  Public WithEvents loadtimer As New System.Windows.Forms.Timer
  Private Sub StartPage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    loadtimer.Interval = 1000
    loadtimer.Enabled = True
    loadtimer.Start()
  End Sub
  Private Sub loadtimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles loadtimer.Tick
    loadtimer.Stop()
    SystemStatus.Show()
  End Sub
End Class

Public Class SystemStatus
  Inherits StartPage
  Private Sub StartPage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      Me.Label1.Text = "This is the form that I want to listen for the load event"
      Me.loadtimer.Enabled = False
  End Sub
End Class

这是程序B:的代码

Imports Program_A
Public Class ListeningForm
  Dim WithEvents testlisten As New Program_A.SystemStatus
  Private Sub testlisten_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles testlisten.Load
    Label1.Text = "SystemStatus form loaded"
  End Sub
  Private Sub ListeningForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Label1.Text = "Waiting for SystemStatus load event..."
  End Sub
End Class

当谈到编程时,我是个新手,所以这可能是不可能的,或者我只是没有理解我读到的内容。无论如何,请告诉我下一步应该采取什么行动

非常感谢,
光学

这将满足您的要求。

Imports Program_A
Public Class ListeningForm
 Dim WithEvents testlisten As New SystemStatus
 Private Sub testlisten_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles testlisten.Load
  ' now this event will fire
  Label1.Text = "SystemStatus form loaded"
 End Sub
 Private Sub ListeningForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  Label1.Text = "Waiting for SystemStatus load event..."
  ' the load event will not fire until you call this
  testlisten.Show() 
 End Sub
End Class

相关内容

  • 没有找到相关文章

最新更新