event does not trigger RRS feed

  • Question

  • Using a control from tool bar is very easy to make it work, but I have created a control not following

    the graphic way.

    In my form (geometry2) i have added 2 buttons; when i click on them nothing happens:

    The code is this:

    Private Sub Form_geometry2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim i As Integer
            Dim OK As New Button
            Dim Esci As New Button


            Me.Left = 100
            Me.Width = Form_base.Width - 200

            OK.Left = 100
            Esci.Left = Me.Width - Esci.Width - 50
            OK.Top = 300
            Esci.Top = 300
            OK.Text = "OK"
            Esci.Text = "EXIT"
            OK.Enabled = True

        End Sub

        Private Sub Ok_click(sender As Object, e As EventArgs) Handles Me.Click
    'I do not reach this point clicking
        End Sub

    Thanks, Enzo

    Sunday, January 26, 2020 11:07 PM

All replies

  • Hi

    You would need to put the declaration for the button at Class level, and also to include WithEvents in the declaration.


    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim WithEvents OK As New Button
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With OK
          .Left = 100
          .Top = 300
          .Text = "OK"
          .Enabled = True
        End With
      End Sub
      Private Sub OK_Click(sender As Object, e As EventArgs) Handles OK.Click
      End Sub
    End Class

    Regards Les, Livingston, Scotland

    • Edited by leshay Sunday, January 26, 2020 11:17 PM
    • Proposed as answer by KareninstructorMVP Monday, January 27, 2020 1:51 AM
    Sunday, January 26, 2020 11:15 PM
  • When you do not prefer the easy ways, you can add the event handlers programmatically:

       AddHandler OK.Click, AddressOf Ok_click2

    Also remove ‘Handles Me.Click’ if it is unneeded.

    • Edited by Viorel_MVP Monday, January 27, 2020 5:45 AM
    Monday, January 27, 2020 5:43 AM
  • Thanks Les, Enzo
    Monday, January 27, 2020 9:20 AM
  • Thanks for help, Enzo
    Monday, January 27, 2020 9:20 AM
  • Thanks for help, Enzo


    If your question has been answered please mark those replies which resolve this question so others with the same question know which one(s) assisted you.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Monday, January 27, 2020 12:09 PM