Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 12-Jul-16 5:14am
Comments
Sujith Karivelil 12-Jul-16 3:24am    
You can use MouseDown event of the controls to identify them
m.r.m.40 12-Jul-16 3:34am    
Let's say it's a module that is being added to a program which already has about 1000 buttons.
Ralf Meier 12-Jul-16 3:37am    
Each Event gives you the Sender (as object). The Sender is the control which raised the Event.
m.r.m.40 12-Jul-16 3:47am    
I think I got your point.
Can you give me more information.
Thanks.
Ralf Meier 12-Jul-16 3:40am    
So ... each of those Buttons should refer to the same Method with the Event you want to have.
This could be done with a Loop.
To give you a solution I you should give me more Information ...

What programming language do you use ? C# or VB ?

1 solution

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 :
VB
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 :
VB
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 ...
 
Share this answer
 
Comments
m.r.m.40 16-Jul-16 2:34am    
Well, the code that you gave me here works fine.
now it lead me to another question which it seems that will be my last question.
also in the same time I'm also searching for its solution.
:
How can I implement a function to all of my forms?
Ralf Meier 16-Jul-16 15:50pm    
In the moment I don't understand your problem. Please explain more ...
Also I can't understand the connection from this point to your other question (the control in the usercontrol) ...
m.r.m.40 17-Jul-16 1:30am    
I'm sorry It worked properly,
I tested it and implemented it on my forms.
About the other question, I'll make more explanation under that.

Thanks,
m.r.m.40 18-Jul-16 2:13am    
This code works good on Forms but for using it n usercontrols it doesn't work. bucause of the difference between Forms and UserControls. So it counts as good answer.
Thanks,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900