Mouse Click

di il
2 risposte

Mouse Click

Buongiorno,

In visual Basic Net ho implementato in un form un panel con 40 Buttons: nel testo inserisco una lettera o un numero

Private Sub CreateButtons()
    Dim xPos As Integer = 0
    Dim yPos As Integer = 0
    Dim n As Integer = 0, k As Integer
    Dim m As Integer = 0       '
    ' Declare and Initialize one variable
    Dim btnArray(41) As System.Windows.Forms.Button

    For i As Integer = 0 To 40
        btnArray(i) = New System.Windows.Forms.Button
    Next i

    While (n < 40)
        With (btnArray(n))
            .Tag = n + 1 ' Tag of button
            .Width = 24 ' Width of button
            .Height = 20 ' Height of button
            If (n = 22) Then ' Location of second line of buttons:
                xPos = 0 : yPos = 20
            End If

            .Left = xPos : .Top = yPos ' Location of button:
            ' Add buttons to a Panel:
            pnlButtons.Controls.Add(btnArray(n)) ' Let panel hold the Buttons
            xPos = xPos + .Width ' Left of next button
            'Ricerca il carattere
            For k = m To 90
                If Not (String.IsNullOrEmpty(TabGen(k).Lett)) Then  ' se TabGen(k).Lett non vuoto
                    .Text = TabGen(k).Lett
                    m = k + 1
                    Exit For
                End If
            Next
            AddHandler .Click, AddressOf Me.ClickButton ' for Event of click Button
            n += 1
        End With
    End While

End Sub

La seguente Sub elabora l'evento Click (Tasto sinistro)

Public Sub ClickButton(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
    
    Dim btn As Button = sender
    MessageBox.Show("You clicked character [" + btn.Text + "]")
End Sub

Vorrei aggiungere anche l'elaborazione dell'evento Click  (Tasto destro); leggento i post in rete dovrei gestire MouseMove ma non riesco  a risolvere il problema.

Mi andrebbe bene anche il DoubleClick  al posto del Click  (Tasto destro)

Mi potete dare una mano?

Franco

2 Risposte

  • Re: Mouse Click

    Per gestire il click destro devi usare l'evento MouseUp (e non MouseMove)

    Ad esempio:

        Private Sub Gestione_MouseUp(sender As Object, e As MouseEventArgs)
           If e.Button = MouseButtons.Right Then
               ' elaboro il tasto destro del mouse
               ' .........
           End If
       End Sub

    Occhio però che quando assegni gli eventi usando AddHandler non devi usare "Handles" nella routine stessa.
    Per cui, dove hai la definizione della sub ClickButton devi togliere il  "Handles MyBase.Click"

    Ciao ciao
    Sergio

  • Re: Mouse Click

    Grazie Sergio, ho applicato le tue correzioni e tutto funziona

    Franco

Devi accedere o registrarti per scrivere nel forum
2 risposte