获取点击的按钮名称! [英] Get the buttom name that is clicked on!

查看:57
本文介绍了获取点击的按钮名称!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我打算制作一个监控鼠标事件的程序,每次发生mousedown事件时,都必须返回名称点击事件发生在它上面的按钮(被按下的按钮)。



我不知道怎么做。

Any关于这样做的想法绝对有用。



提前感谢。



我是什么尝试过:



正确知道我正在读msdn上的鼠标事件。

解决方案

< blockquote>如果我理解你的意思你可以(例如)这样做:



首先你创建一个具有Button-Click代码的模块-Handler:

 模块 TestModul 

公共 Sub ClickHandler(发件人 As 对象,e As System.EventArgs)

Dim mySender As Control = sender
MessageBox.Show(mySender.Name + vbCrLf + mySender.Parent.Name)

< span class =code-keyword>结束 Sub

结束 模块





现在,您需要在每个表单上具有以下脚本:

 受保护的 覆盖  Sub  OnVisibleChanged(e  As  System.EventArgs)
MyBase .OnVisibleChanged(e)

如果 .Visible 然后 ' 在显示表格时显示每个按钮的处理程序(变得可见)
对于 每个 myControl 作为控制 .Controls
如果 myControl。 GetType GetType (按钮)然后
AddHandler myControl.Click, AddressOf TestModul.ClickHandler
< span class =code-keyword>结束 如果
下一步
其他 ' 删除手当表格离开时(每个按钮变得不可见),从每个按钮获取
对于 每个 myControl 作为控制 .Controls
如果 myControl。 GetType GetType (按钮)然后
RemoveHandler myControl.Click, AddressOf TestModul.ClickHandler
End 如果
下一步
结束 如果

结束 Sub





当然还有其他几种解决方案可供使用。

这也是一个好主意具有此方法的基本形式,项目中的所有表单都派生自此基本形式。在这种情况下,它们会自动获得此功能。



TestModul中的Handler-Method将执行您想要完成的操作...


Hi,

I'm planning to make a program that monitors the mouse events, each time the mousedown event occured, it has to return the name of the buttom which the click event has occurd on it(the buttom that is pressed).

I have no idea how to that.
Any ideas about doing it would be absolutely useful.

thanks in advance.

What I have tried:

Right know I'm reading about mouse events on msdn.

解决方案

If I understood well what you meant you could (for example) do it like this :

First you create a Module which has the code for the Button-Click-Handler :

Module TestModul

    Public Sub ClickHandler(sender As Object, e As System.EventArgs)

        Dim mySender As Control = sender
        MessageBox.Show(mySender.Name + vbCrLf + mySender.Parent.Name)

    End Sub

End Module



Now, on each of your Forms you need to have the following script :

Protected Overrides Sub OnVisibleChanged(e As System.EventArgs)
    MyBase.OnVisibleChanged(e)

    If Me.Visible Then ' create Handler to each Button when Form is shown (becomes visible)
          For Each myControl As Control In Me.Controls
            If myControl.GetType Is GetType(Button) Then
                AddHandler myControl.Click, AddressOf TestModul.ClickHandler
            End If
        Next
    Else ' remove Handler from each Button when Form is left (becomes unvisible)
            For Each myControl As Control In Me.Controls
            If myControl.GetType Is GetType(Button) Then
                RemoveHandler myControl.Click, AddressOf TestModul.ClickHandler
            End If
        Next
    End If

End Sub



Of course severall other solutions are possible.
Also it could be a good idea to build a baseform which has this method and all forms in your project derive from this baseform. In this case they automaticly get this function.

The Handler-Method in TestModul will do what you want to be done ...


这篇关于获取点击的按钮名称!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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