Generate event by clicking on a form vb.net

0

I have an application on vb.net with visual studio 2017 and I need to click on any part of the form to run a sub with code. I tried to do it like this:

Public Sub Principal_Click() Handles Me.MouseClick
    minuto = TimeOfDay.Minute
End Sub

To do it, I relied on the KeyPress event, and I thought it would work but it has not been like that. No matter how many times I click on any part of the form, I do not run that sub. I thank you in advance for the help you can give me!

    
asked by walkerdeath 10.01.2018 в 20:48
source

1 answer

1

Greetings, walkerdeath.

As I understand your question and the comments, your answer should be here: link

Basically it's going through all the controles of formulario in the event Load and addressing the event MouseClick of each of those controls to the event MouseClick of formulario .

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Each control As Control In Controls
            AddHandler control.MouseClick, AddressOf Form1_Click
        Next
    End Sub

    Private Sub Form1_Click(sender As Object, e As EventArgs) Handles MyBase.Click
        ' Aquí ejecutas el código
        Console.WriteLine("¡Hola, Stack Overflow!") ' código de ejemplo
    End Sub

End Class

e.g .: (Pay attention to the output console)

    
answered by 25.01.2018 / 05:50
source