locked
Add event handlers to controls RRS feed

  • Question

  • hello,my friends, I have a problem, in Winform or mobile developing, When I newed some buttons in my code(never drew on the form), how can I add the click events to them and use it?
    Tuesday, April 27, 2010 9:59 AM

Answers

  • Hi Hawking502 ,

    Public Class Form1
     Private WithEvents _NewBtn As New Button
    
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Dim Loc As System.Drawing.Point
      Loc.X = 0 : Loc.Y = 0
      _NewBtn.Location = Loc
    
      Dim Sz As System.Drawing.Size
      Sz.Width = 100 : Sz.Height = 100
      _NewBtn.Size = Sz
    
      _NewBtn.Text = "New Button "
      _NewBtn.Visible = True
      Me.Controls.Add(_NewBtn)
    
      'Attach the _NewBtn MouseEnter event with the NewEvent Sub.
      AddHandler _NewBtn.MouseEnter, AddressOf NewEvent
    
     End Sub
    
     'This is a ready attached procedure with _NewBtn.Click event in design time.
     Private Sub _NewBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _NewBtn.Click
      MsgBox("You Click _NewBtn")
     End Sub
    
     'This is a procedure you attach it with the control in the run time.
     Private Sub NewEvent()
      MsgBox("You Entered _NewBtn")
     End Sub
    
    End Class

     

    Now you can create the control in RunTime and attach its events with Predefined procedures to handle these events.

     

    I hope that helps.

    Regards: MrAlshahawy

     


    Please remember to mark the replies as answers if they help ,Don't forget to Vote the answer as Helpful if it helps
    • Proposed as answer by Ashraf Alshahawy Wednesday, April 28, 2010 3:19 PM
    • Marked as answer by warrentang Friday, April 30, 2010 4:14 AM
    Wednesday, April 28, 2010 3:48 AM

All replies

  • mark: by Vb.net in .net CF 2.0 and WM6.0

    Tuesday, April 27, 2010 10:02 AM
  • Hi Hawking502 ,

    Public Class Form1
     Private WithEvents _NewBtn As New Button
    
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Dim Loc As System.Drawing.Point
      Loc.X = 0 : Loc.Y = 0
      _NewBtn.Location = Loc
    
      Dim Sz As System.Drawing.Size
      Sz.Width = 100 : Sz.Height = 100
      _NewBtn.Size = Sz
    
      _NewBtn.Text = "New Button "
      _NewBtn.Visible = True
      Me.Controls.Add(_NewBtn)
    
      'Attach the _NewBtn MouseEnter event with the NewEvent Sub.
      AddHandler _NewBtn.MouseEnter, AddressOf NewEvent
    
     End Sub
    
     'This is a ready attached procedure with _NewBtn.Click event in design time.
     Private Sub _NewBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _NewBtn.Click
      MsgBox("You Click _NewBtn")
     End Sub
    
     'This is a procedure you attach it with the control in the run time.
     Private Sub NewEvent()
      MsgBox("You Entered _NewBtn")
     End Sub
    
    End Class

     

    Now you can create the control in RunTime and attach its events with Predefined procedures to handle these events.

     

    I hope that helps.

    Regards: MrAlshahawy

     


    Please remember to mark the replies as answers if they help ,Don't forget to Vote the answer as Helpful if it helps
    • Proposed as answer by Ashraf Alshahawy Wednesday, April 28, 2010 3:19 PM
    • Marked as answer by warrentang Friday, April 30, 2010 4:14 AM
    Wednesday, April 28, 2010 3:48 AM
  • I think double click the button is ok(if you are using VS).
    the same problem with Y.tks
    Wednesday, April 28, 2010 6:14 AM