处理在另一个类/文件中定义的对象的事件 [英] Handle Events of Object defined in another class/file

查看:142
本文介绍了处理在另一个类/文件中定义的对象的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个VB.NET程序,我有多个窗体和一些变量,我想访问所有窗体,所以我创建了一个包含一些公共变量的模块文件。我发现,尽管这些变量已被声明为 WithEvents ,但是它们的事件无法在没有首先复制到局部变量的情况下处理。



发生的事情的伪代码:



在Main.vb(模块文件)中

  Public WithEvents foo As VarType1 

在Someform中。 vb(Windows窗体)

  Private Sub fooEventHandler(发件人为System.Object,e As fooEventArgs)处理foo.fooEvent 

我收到一个错误,表示Handles子句要求在包含类型或其基本类型之一定义的WithEvents变量。这不是我做的吗或者我错过了一些东西?

解决方案

关键是消息的一部分:需要一个WithEvents变量在包含类型中定义。您的 foo 不是定义在包含类型(在这种情况下您的表单)。有两种方法可以实现。



使用模块/全局声明为Foo对象提供范围:

 公共mainFoo作为FooBar 

它真的只需要朋友,但既然没有订阅这里的活动,它不需要是 WithEvents 。只需要访问 Foo (而不是事件)的表单/对象可以引用这个 mainFoo 对象。



接下来,任何希望订阅Foo事件的表单或类都需要一个本地的 WithEvents 变量设置为全局对象:

  Private WithEvents myFoo As FooBar'变量声明

myFoo = mainFoo'set myFoo引用真实对象

这种方法的优点是在表单代码中,你应该能够从左侧的菜单中选择myFoo,然后从右侧菜单中的FooEvent VB / VS插入正确的事件处理程序,就像控件事件一样:

  Private Sub myFoo_FooChanged(sender As Object,newFoo As String)_ 
处理myFoo.FooChanged






这个其他方法稍微简单一些,只需使用AddHandler手动连接该主变量:

  AddHandler mainFoo.FooChanged,AddressOf sub_FooChanged 

它阻止不必创建本地的 WithEvents 变量,但也可以防止VS为您创建事件过程。


I have a VB.NET program where I have multiple forms and some variables that I want to access on all the forms, so I've created a module file that contains some public variables. What I'm finding is that though these variables have been declared WithEvents, their events cannot be handled on forms without first copying to a local variable.

Pseudo-Code for what's going on:

In Main.vb (module file)

Public WithEvents foo As VarType1

In Someform.vb (Windows Form)

Private Sub fooEventHandler(sender as System.Object, e As fooEventArgs) Handles foo.fooEvent

I get an error that says "Handles clause requires a WithEvents variable defined in the containing type or one of its base types". Isn't this what I've done? Or am I missing something?

解决方案

The key is the part of the message: requires a WithEvents variable defined in the containing type. Your foo is not defined in the containing type (your Form in this case). There are two ways to do this.

Use the module/global declaration to provide scope foro the Foo object:

Public mainFoo As FooBar

It really only needs to be Friend, but since there is nothing to subscribe to events here, it doesnt need to be WithEvents. Forms/objects which just need access to Foo (not the events) can reference this mainFoo object.

Next, any form or class which wishes to subscribe to Foo events, does need a local WithEvents variable set to the global object:

Private WithEvents myFoo As FooBar    ' variable declaration

myFoo = mainFoo       ' set myFoo to reference the real object

The advantage to this method is that in the form code, you should be able to select myFoo from the menu on the left, then the FooEvent from the menu on the right for VB/VS to insert the correct event handler as it does with control events:

Private Sub myFoo_FooChanged(sender As Object, newFoo As String) _
       Handles myFoo.FooChanged


This other method is slightly simpler, just use AddHandler to manually hook up that main variable:

AddHandler mainFoo.FooChanged, AddressOf sub_FooChanged

It prevents having to create a local WithEvents variable, but it also prevents VS from creating the Event procedures for you.

这篇关于处理在另一个类/文件中定义的对象的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆