Hi all,

Hopefully someone can help me out with this one.

I have a form which gets populated by 15 buttons dynamically at runtime (these need to be dynamic as there will be a user config file at some point)

What I need these buttons to do is to open a specific form when clicked. Which form that opens will be dealt with in the config file.

Public Class frm_MainScreen

    Private Sub frm_MainScreen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim count As Integer
        Dim ImageDir As String
        Dim xpos As Integer
        Dim ypos As Integer

        xpos = 12
        ypos = 12

        ImageDir = (My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\TrinityMediaCenter\Images\")

        Me.BackgroundImage = Image.FromFile(ImageDir & "bg_main.png")

        For count = 1 To 15

            Dim btnMain(count) As System.Windows.Forms.Button
            btnMain(count) = New System.Windows.Forms.Button


            '*************Need to use the form name as the button name*************
            btnMain(count).Name = "**********"

            btnMain(count).Width = 190
            btnMain(count).Height = 190
            btnMain(count).BackgroundImage = Image.FromFile(ImageDir & "buttons\main_back.png")
            btnMain(count).Image = Image.FromFile(ImageDir & "buttons\main_front.png")
            btnMain(count).BackColor = Color.Transparent
            btnMain(count).ForeColor = Color.White
            btnMain(count).Text = "Button " & count
            btnMain(count).FlatAppearance.BorderSize = 0
            btnMain(count).FlatStyle = FlatStyle.Flat
            btnMain(count).FlatAppearance.MouseDownBackColor = Color.Transparent
            btnMain(count).FlatAppearance.MouseOverBackColor = Color.Transparent

            btnMain(count).Location = New Point(xpos, ypos)

            Me.Controls.Add(btnMain(count))

            xpos = xpos + 202

            If xpos > 822 Then
                xpos = 12
                ypos = ypos + 202
            End If

            AddHandler btnMain(count).Click, AddressOf btnMain_Click

        Next

    End Sub

    Private Sub btnMain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim buttonPressed As System.Windows.Forms.Button = sender

        '********************************************************
        '*************Not sure what needs to be here*************
        '*************I need to open the form based *************
        '*************on which button is pressed    *************
        '********************************************************

    End Sub
End Class

Hope someone can help.

Thanks
TheMightySpud

Select Case buttonPressed.Name
            Case "Button1"
                MsgBox("Button1 clicked.")
            Case "Button2"
                MsgBox("Button2 clicked.")
            Case Else
                MsgBox("Unknown Button clicked.")
        End Select

a bit more dynamic way...

Private Sub button_Clicks(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click, btn3.Click
		CType(CreateForm(CStr(CType(sender, Button).Tag)), Form).Show()
	End Sub

	Private Shared Function CreateForm(ByVal requestedForm As String) As Object
		Dim myAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
		Dim frm = (From g In myAssembly.GetTypes Where g.BaseType.FullName = "System.Windows.Forms.Form" AndAlso g.Name = requestedForm).First
		Return Activator.CreateInstance(frm)
	End Function

i named the buttons btn1, btn2 and so on and set their Tag property to the Form they should open

This question has already been answered. Start a new discussion instead.