Event on Button Made in code? Please help

Hi I'm A beginner at control arrays with only using the code, I'm using vb.net 2003. to help this is also one of my first posts. I'm trying to make a button which I can already do. When I Check box checkBringANewButton I Want to make a new button.

but I cant make a button.click event for all these buttons

Dim Button(1000) as button

Dim I as integer

private sub ButtonProperties()

button(i).height = 50

button(i).width = 50

button(i).location.x = 100

button(i).location.y = 100

button(i).text = ""

button(i).name = "Button" & I

End Sub

Private Sub checkBringANewButton_check(, ByVal e As System.EventArgs) Handles checkbringanewbutton.check

i += 1

buttonproperties()

me.controls.add(button(i))

checkbringanewbutton.checked = false

End Sub

Thank you so much




Answer this question

Event on Button Made in code? Please help

  • ShadowRayz

    you need to declare an event handler and add the click event of the button pointing to the eventhandler. The event handler or rather subroutine used would be this pretty much:

     

    private sub ButtonPressed(byval sender as object, byval e as EventArgs)

       'code here

    end sub

     

    then when creating the buttons, add a handler...

     

    AddHandler button(i).Click, addressof ButtonPressed

     

    does this work for you



  • Philipp Lamp

    That did not work, it says an unhandled exception came into place

  • Blufire48

    The following code will add the 1000 buttons and hook up there event handlers using addhandler command.

    Public Class Form1
    Private Button(1000) As Button

    Private Sub ButtonProperties(ByVal i As Integer)
    Button(i) = New Button
    With Button(i)
    .Height = 50
    .Width = 50
    .Left = 100 + (i * 10)
    .Top = 100
    .Text = ""
    .Name = "Button" & i.ToString
    .Text = i.ToString
    End With
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    For i As Integer = 0 To 1000
    ButtonProperties(i)
    Me.Controls.Add(Button(i))
    AddHandler Me.Controls(Button(i).Name).Click, AddressOf Button_Click
    Next
    End Sub

    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    MsgBox(CType(sender, Button).Name & " clicked")
    End Sub
    End Class


  • Event on Button Made in code? Please help