将事件传递给父表单 [英] Passing event to the parent form

查看:29
本文介绍了将事件传递给父表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有一个小问题.我正在尝试将拥有的表单的事件转移/传递/提升给他的父母.让我们看看我的例子:

I have a little problem here. I'm trying to transfer/pass/raise the events of an owned form to his parent. Lets look at my example:

假设我有一个初始化 CustomPanel 的表单(只是一个继承自 System.Windows.Forms.Panel 的类).它还有一个事件处理程序(可能是其他事件,不一定是点击事件):

Lets say i have a form that initialize a CustomPanel (simply a class that inherits from System.Windows.Forms.Panel). It also have an event handler (it could be an other event, not necessarily a click event):

Public Sub New()
    Me.Size = New Size(1000,1000)

    Dim pnl1 As New CustomPanel()
    pnl1.Location = New Point(0,0)
    pnl1.size = New Size(100,100)
    Me.Controls.Add(pnl1)
End Sub

Private Sub form1_Click(sender As Object, e As EventArgs) Handles Me.Click
    MsgBox("I got it!")
End Sub

我做了类似的事情,当我点击 CustomPanel (pnl1) 时,父容器 (form1) 没有收到点击事件......这是可以理解的.我试图查看 CustomPanel (pnl1) 的属性,如果我能找到一些有趣的东西,比如点击"或向父级提出事件"(我在这里很绝望)但没有成功.我说好的,我将处理需要在 CustomPanel 类中传递给父级的事件,但我在这里也找不到解决方案:

I did something similar and when I clicked on the CustomPanel (pnl1) the parent container (form1) did not receive a click event ... which is understandable. I tried to look in the properties of the CustomPanel (pnl1) if i could find something interesting like "click through" or "raise event to parent" (I was desperate here) but without success. I said alright, I will handle the events that I need to pass to parent in the CustomPanel class but I cant find a solution here neither:

Imports System.Windows.Forms

Public Class CustomPanel
    Inherits Panel

    Public Sub New()

    End Sub

    Private Sub CustomPanel_Click(sender As Object, e As EventArgs) Handles Me.Click
        'What to put here?
        'Me.Parent.?
    End Sub

End Class

我只想知道是否可以向父级抛出/引发/传递事件.有一件事是肯定的,我不应该这样做,而且我不能向父表单添加任何其他内容.原因很简单,我可以在这个父窗体中有 100 多个控件,并且可以动态添加它们.最重要的是,这些控件内部还可以有自己的控件!所以我可以有类似的东西:

I just want to know if its possible to throw/raise/pass events to the parent. One thing is sure, its that i shouldn't have to and i cannot add anything else to the parent form. The reason is simple, i could have over 100 controls in this parent form and they could be added dynamically. And on top of that, these controls could also have their own controls inside! So i could have something like:

pnl99 call parent click -> pnl98 call parent click -> ... until the parent of the control really handle the click event ... -> form1 perform click event

也许很难理解,但如果您能帮助我,我将不胜感激.

Maybe its hard to understand but if you can help me I would appreciate.

推荐答案

使用自定义事件,拥有面板的表单订阅.引发事件

Using a custom event, that the form owning the panel subscribes to. Raise Event

Public Sub New()
 Me.Size = New Size(1000,1000)

 Dim pnl1 As New CustomPanel()
 pnl1.Location = New Point(0,0)
 pnl1.size = New Size(100,100)
 Addhandler pnl1.MyClickEvent, AddressOf pl_Click
 Me.Controls.Add(pnl1)
End Sub

Private Sub pl_Click()
 MsgBox("I got it!")
End Sub

自定义面板:

Public Class CustomPanel
 Inherits Panel
 Public Event MyClickEvent

 Private Sub CustomPanel_Click(sender As Object, e As EventArgs) Handles Me.Click
   RaiseEvent MyClickEvent()
 End Sub

End Class

这篇关于将事件传递给父表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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