How to make a button addition system with unique events to launch?

1

I'd like to know how to make a system in which it automatically adds a button to a file with a specific event in a FlowLayoutPanel for a specific event that, upon clicking, launches the application. The code I can do is something like this:

Try
        For Each file As String In My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.SpecialDirectories.MyDocuments)
            Dim mbtn As New MaterialSkin.Controls.MaterialFlatButton
            mbtn.Icon = (Icon.ExtractAssociatedIcon(file)).ToBitmap
            mbtn.Text = My.Computer.FileSystem.GetName(file)
            mbtn.Tag = file
            flp.Controls.Add(mbtn)
        Next
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

With this, I have already been able to do everything but the system to open the link. And I'd also like to know how to hide the extension. I await replies:)

    
asked by anonymous 18.03.2017 / 16:28

1 answer

2

You will need to create an event in your control. Use AddHandler , see:

AddHandler mbtn.Click, AddressOf mbtn_Click

Then declare your function mbtn_Click :

Private Sub mbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
   ' ...seu código
End Sub

Your role will run whenever the click event is triggered on your controls. If you want something specific for a particular control, you will have to do a treatment using the sender parameter.

Private Sub mbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Select CType(sender, MaterialFlatButton).Name
    Case "MaterialFlatButton1_Nome"
        ' seu código
    Case "MaterialFlatButton2_Nome"
        ' seu código
    End Select
End Sub

In case you do not bring the extension use:

mbtn.Text = Path.GetFileNameWithoutExtension(file)
    
24.03.2017 / 13:40