如何设置一个形式的单击事件? [英] How do I set a click event for a form?

查看:95
本文介绍了如何设置一个形式的单击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#的形式(姑且称之为MainForm中)有一些关于它自定义控件。我想有MainForm.OnClick()方法随时火有人点击窗体上无论是否点击发生的形式,或者如果点击的自定义控件之一。我在寻找类似的,除了鼠标点击,而不是按键形式的KeyPreview功能的行为。

I have a c# form (let's call it MainForm) with a number of custom controls on it. I'd like to have the MainForm.OnClick() method fire anytime someone clicks on the form regardless of whether the click happened on the form or if the click was on one of the custom controls. I'm looking for behavior similar to the KeyPreview feature of forms except for mouse clicks rather than key presses.

推荐答案

在窗体的ControlAdded事件,添加一个鼠标点击处理程序来控制,与窗体的Click事件的地址。我没有测试过这一点,但它可能工作。

In the form's ControlAdded event, add a MouseClick handler to the control, with the Address of the form's click event. I haven't tested this, but it might work.

Private Sub Example_ControlAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles Me.ControlAdded

    AddHandler e.Control.MouseClick, AddressOf Example_MouseClick
End Sub

Private Sub Example_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
    MessageBox.Show("Click")
End Sub

这篇关于如何设置一个形式的单击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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