locked
Coding Button created from a loop RRS feed

  • Question

  • Hello,

    I need to use a loop to create a dynamic number of buttons depended on a data from sql DB my problem will be in coding the click event for them how to make it possible? i'm thinking of a function in class maybe, or there is another way?



    • Edited by AbanoubZak Thursday, August 8, 2019 1:59 AM
    Thursday, August 8, 2019 1:59 AM

Answers

  • Hi,

    try my code:

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For i = 1 To 5
                Dim mybutton As New Button With {.Text = "Button" & i.ToString(), .Name = "Button" & i.ToString(), .Location = New Point(0, i * 40)}
                Me.Controls.Add(mybutton)
                If i = 1 Then
                    AddHandler mybutton.Click, AddressOf ButtonClick1
                End If
                If i = 2 Then
                    AddHandler mybutton.Click, AddressOf ButtonClick2
                End If
            Next
    
        End Sub
        Public Sub ButtonClick1(sender As Object, e As EventArgs)
            MsgBox(CType(sender, Button).Name)
        End Sub
        Public Sub ButtonClick2(sender As Object, e As EventArgs)
            MsgBox(CType(sender, Button).Name)
        End Sub
    
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by AbanoubZak Thursday, August 8, 2019 2:38 AM
    Thursday, August 8, 2019 2:23 AM
  • Hi

    An alternative:

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim pan As New Panel
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Size = New Size(500, 300)
        With pan
          .Location = New Point(5, 5)
          .Size = New Size(ClientSize.Width - 20, 220)
          .BackColor = Color.Beige
          .BorderStyle = BorderStyle.FixedSingle
        End With
        Controls.Add(pan)
        SetButtons(18)
      End Sub
      Sub SetButtons(num As Integer)
        Dim x As Integer = 2
        Dim y As Integer = 5
        Dim yStep As Integer = 30
        Dim cs As Integer = pan.Height
        For i As Integer = 1 To num
          Dim mybutton As New Button With {.Text = "Button" & i.ToString(), .Name = "Button" & i.ToString(), .Location = New Point(x, y)}
          pan.Controls.Add(mybutton)
          AddHandler mybutton.Click, AddressOf ButtonClick
          y += yStep
          If y + yStep >= cs Then
            x += 100
            y = 5
          End If
        Next
      End Sub
      Public Sub ButtonClick(sender As Object, e As EventArgs)
        Dim butt As Button = DirectCast(sender, Button)
        MessageBox.Show("Selected Button = " & butt.Name)
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by AbanoubZak Friday, August 9, 2019 6:45 PM
    Thursday, August 8, 2019 2:57 AM

All replies

  • Hi,

    try my code:

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For i = 1 To 5
                Dim mybutton As New Button With {.Text = "Button" & i.ToString(), .Name = "Button" & i.ToString(), .Location = New Point(0, i * 40)}
                Me.Controls.Add(mybutton)
                If i = 1 Then
                    AddHandler mybutton.Click, AddressOf ButtonClick1
                End If
                If i = 2 Then
                    AddHandler mybutton.Click, AddressOf ButtonClick2
                End If
            Next
    
        End Sub
        Public Sub ButtonClick1(sender As Object, e As EventArgs)
            MsgBox(CType(sender, Button).Name)
        End Sub
        Public Sub ButtonClick2(sender As Object, e As EventArgs)
            MsgBox(CType(sender, Button).Name)
        End Sub
    
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by AbanoubZak Thursday, August 8, 2019 2:38 AM
    Thursday, August 8, 2019 2:23 AM
  • Hi

    An alternative:

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim pan As New Panel
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Size = New Size(500, 300)
        With pan
          .Location = New Point(5, 5)
          .Size = New Size(ClientSize.Width - 20, 220)
          .BackColor = Color.Beige
          .BorderStyle = BorderStyle.FixedSingle
        End With
        Controls.Add(pan)
        SetButtons(18)
      End Sub
      Sub SetButtons(num As Integer)
        Dim x As Integer = 2
        Dim y As Integer = 5
        Dim yStep As Integer = 30
        Dim cs As Integer = pan.Height
        For i As Integer = 1 To num
          Dim mybutton As New Button With {.Text = "Button" & i.ToString(), .Name = "Button" & i.ToString(), .Location = New Point(x, y)}
          pan.Controls.Add(mybutton)
          AddHandler mybutton.Click, AddressOf ButtonClick
          y += yStep
          If y + yStep >= cs Then
            x += 100
            y = 5
          End If
        Next
      End Sub
      Public Sub ButtonClick(sender As Object, e As EventArgs)
        Dim butt As Button = DirectCast(sender, Button)
        MessageBox.Show("Selected Button = " & butt.Name)
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by AbanoubZak Friday, August 9, 2019 6:45 PM
    Thursday, August 8, 2019 2:57 AM