简体   繁体   English

在另一个类中处理Control中的事件

[英]Handles an Event from Control in another class

I want to fire an event in another class. 我想在另一个班级里触发一个事件。 And my problem is I don't know how to make it. 我的问题是我不知道该怎么做。

I'm trying to use Inherits statement to my Form and add my class name to it, and it works as I hope: 我正在尝试将Inherits语句用于我的Form并将其添加到我的类名,并且它可以按我的期望工作:

Public Class Frm_Main_Copy

    Inherits ToolStripMenuApp 
     'I have a ToolStripMenu that has declared before on my class and it sounds like this:
     'Public Shared WithEvents Cat000x86_64App As ToolStripMenuItem
    ...
    Private Sub IsClicked(ByVal sender As Object, ByVal e As EventArgs) Handles Cat000x86_64App.Click

    End Sub

End Class

but the designer form messed up(returns a fatal error) and I should delete the Inherits statement, and the others. 但是设计器表单搞砸了(返回致命错误),我应该删除Inherits语句,以及其他。

Tried to act as form designer script(Trying to put this code to my class): 试图充当表单设计器脚本(尝试将此代码放到我的课程中):

Friend WithEvents BlahBlah As RadioButton 'For example

It didn't worked, 没用

Declaring a variable for my class and It didn't worked too 为我的课程声明了一个变量,但也没有用

Searched on the Internet and it seems likely more complicated than I thought... 在互联网上搜索,似乎比我想象的要复杂...

Anyone can help? 有人可以帮忙吗? Any help is appreciated. 任何帮助表示赞赏。

A form is not a form unless it inherits, either directly or indirectly, the Form class. 除非表单直接或间接继承Form类,否则它不是Form You cannot inherit any type that is not itself a form and expect your type to be a form. 您不能继承本身不是表单的任何类型,并且不能期望您的类型是表单。 With that code, if ToolStripMenuApp is not a form then Frm_Main_Copy is not a form either, hence no form designer. 使用该代码,如果ToolStripMenuApp不是表单,则Frm_Main_Copy也不是表单,因此Frm_Main_Copy没有表单设计器。

If what you're actually saying is that you have an instance of that ToolStripMenuApp that contains a TooStripMenuItem whose Click event you want to handle in Frm_Main_Copy then the first step is to not declare Cat000x86_64App as Shared . 如果您实际上要说的是您拥有该ToolStripMenuApp的实例,该实例包含要在Frm_Main_Copy处理其Click事件的TooStripMenuItem ,则第一步是不将Cat000x86_64App声明为Shared Frm_Main_Copy needs to declare a method capable of handling that event: Frm_Main_Copy需要声明一个能够处理该事件的方法:

Private Sub IsClicked(ByVal sender As Object, ByVal e As EventArgs)
    '...
End Sub

Note that there is no Handles clause because there's no WithEvents variable in that class whose event you are handling. 请注意,没有Handles子句,因为该类中没有正在处理其事件的WithEvents变量。

Next, Frm_Main_Copy must have access to the appropriate instance of ToolStripMenuApp . 接下来, Frm_Main_Copy必须有权访问ToolStripMenuApp的适当实例。 It's impossible for us to say how best to do that based on the information provided but it might be as simple as this: 我们无法根据所提供的信息说出如何最好地做到这一点,但这可能就这么简单:

Dim tsma As New ToolStripMenuApp

You then register your method as a handler for the appropriate event: 然后,将您的方法注册为相应事件的处理程序:

AddHandler tsma.Cat000x86_64App.Click, AddressOf IsClicked

If you use AddHandler , make sure to use RemoveHandler when you're done with either object. 如果您使用AddHandler ,请确保在处理RemoveHandler任何一个对象后都使用RemoveHandler I suggest that you do some reading based on this information. 我建议您根据此信息进行一些阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM